Challenge - 5 Problems
Relational Database Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
LEFT JOIN keeps all rows from the left table even if no matching row in the right table.
✗ Incorrect
The LEFT JOIN returns all employees. For Bob, who has no department_id, the department_name is NULL.
🧠 Conceptual
intermediate1:30remaining
Understanding Primary Key Constraints
Which statement best describes the role of a primary key in a relational database table?
Attempts:
2 left
💡 Hint
Think about what makes each row unique and how databases enforce uniqueness.
✗ Incorrect
A primary key uniquely identifies each row and does not allow NULLs to ensure every record is distinct.
📝 Syntax
advanced2: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) );
Attempts:
2 left
💡 Hint
Check the syntax for declaring foreign keys in SQL.
✗ Incorrect
Option A misses parentheses around the foreign key column name, causing a syntax error.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Consider how to avoid duplicate rows when joining multiple tables.
✗ Incorrect
Using DISTINCT removes duplicate rows caused by multiple orders of the same product by the same customer.
🔧 Debug
expert3: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?
Attempts:
2 left
💡 Hint
Foreign key constraints require the referenced value to exist in the parent table.
✗ Incorrect
A foreign key error on insert usually means the referenced key does not exist in the parent table.