0
0
MySQLquery~20 mins

Relational database concepts in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Relational Database Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of JOIN with NULL values
Consider two tables Employees and Departments. Employees may or may not belong to a department. What is the output of this query?
SELECT e.name, d.department_name FROM Employees e LEFT JOIN Departments d ON e.department_id = d.id ORDER BY e.name;
MySQL
CREATE TABLE Employees (id INT, name VARCHAR(20), department_id INT);
INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', NULL), (3, 'Charlie', 20);
CREATE TABLE Departments (id INT, department_name VARCHAR(20));
INSERT INTO Departments VALUES (10, 'HR'), (20, 'IT');

SELECT e.name, d.department_name FROM Employees e LEFT JOIN Departments d ON e.department_id = d.id ORDER BY e.name;
A[{"name": "Alice", "department_name": "HR"}, {"name": "Charlie", "department_name": "IT"}]
B[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": ""}, {"name": "Charlie", "department_name": "IT"}]
C[{"name": "Alice", "department_name": null}, {"name": "Bob", "department_name": null}, {"name": "Charlie", "department_name": null}]
D[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": null}, {"name": "Charlie", "department_name": "IT"}]
Attempts:
2 left
💡 Hint
LEFT JOIN keeps all rows from the left table even if no matching row in the right table.
🧠 Conceptual
intermediate
1:30remaining
Understanding Primary Key Constraints
Which statement best describes the role of a primary key in a relational database table?
AIt uniquely identifies each row and cannot contain NULL values.
BIt allows duplicate values but must be indexed for faster queries.
CIt is used to store large text data in the table.
DIt automatically encrypts the data in the table.
Attempts:
2 left
💡 Hint
Think about what makes each row unique and how databases enforce uniqueness.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in the SQL query
Which option contains a syntax error when creating a table with a foreign key constraint?
MySQL
CREATE TABLE Orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES Customers(id)
);
ACREATE TABLE Orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY customer_id REFERENCES Customers(id));
BCREATE TABLE Orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES Customers(id));
C;))di(sremotsuC SECNEREFER )di_remotsuc( YEK NGIEROF ,TNI di_remotsuc ,YEK YRAMIRP TNI di_redro( sredrO ELBAT ETAERC
DCREATE TABLE Orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES Customers(id) ON DELETE CASCADE);
Attempts:
2 left
💡 Hint
Check the syntax for declaring foreign keys in SQL.
optimization
advanced
2:30remaining
Optimizing a query with multiple JOINs
Given three tables: Customers, Orders, and Products, which query is optimized to retrieve customer names and product names for all orders without unnecessary data duplication?
ASELECT c.name, p.product_name FROM Customers c, Orders o, Products p WHERE c.id = o.customer_id AND o.product_id = p.id;
BSELECT DISTINCT c.name, p.product_name FROM Customers c JOIN Orders o ON c.id = o.customer_id JOIN Products p ON o.product_id = p.id;
CSELECT c.name, p.product_name FROM Customers c LEFT JOIN Orders o ON c.id = o.customer_id LEFT JOIN Products p ON o.product_id = p.id;
DSELECT c.name, p.product_name FROM Customers c JOIN Orders o ON c.id = o.customer_id JOIN Products p ON o.product_id = p.id;
Attempts:
2 left
💡 Hint
Consider how to avoid duplicate rows when joining multiple tables.
🔧 Debug
expert
3:00remaining
Debugging a foreign key constraint failure
You try to insert a row into the Orders table with customer_id = 5, but get a foreign key constraint error. Which is the most likely cause?
AThe Orders table does not have a primary key defined.
BThe customer_id column in Orders allows NULL values.
CThere is no customer with id = 5 in the Customers table.
DThe Products table is missing a foreign key constraint.
Attempts:
2 left
💡 Hint
Foreign key constraints require the referenced value to exist in the parent table.