0
0
MySQLquery~20 mins

LEFT JOIN in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LEFT JOIN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this LEFT JOIN query?

Given two tables, Employees and Departments, what will be the result of the following query?

SELECT Employees.name, Departments.department_name
FROM Employees
LEFT JOIN Departments ON Employees.department_id = Departments.id
ORDER BY Employees.id;

Tables:

Employees
id | name | department_id
1 | Alice | 1
2 | Bob | 2
3 | Charlie | NULL

Departments
id | department_name
1 | HR
2 | IT
3 | Marketing

MySQL
SELECT Employees.name, Departments.department_name
FROM Employees
LEFT JOIN Departments ON Employees.department_id = Departments.id
ORDER BY Employees.id;
A[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": "IT"}, {"name": "Charlie", "department_name": null}]
B[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": "IT"}]
C[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": "IT"}, {"name": "Charlie", "department_name": "Marketing"}]
D[{"name": "Alice", "department_name": null}, {"name": "Bob", "department_name": null}, {"name": "Charlie", "department_name": null}]
Attempts:
2 left
💡 Hint

Remember, LEFT JOIN keeps all rows from the left table and matches rows from the right table if possible.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes a LEFT JOIN?

Choose the correct description of what a LEFT JOIN does in SQL.

AReturns all rows from the right table and matching rows from the left table; unmatched left table rows are excluded.
BReturns only rows that have matching values in both tables.
CReturns all rows from the left table and matching rows from the right table; unmatched right table rows show NULL values.
DReturns all rows from the left table and matching rows from the right table; unmatched right table rows are excluded.
Attempts:
2 left
💡 Hint

Think about which table's rows are always kept and what happens when there is no match.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this LEFT JOIN query

Which option contains a syntax error in the LEFT JOIN SQL query?

SELECT e.name, d.department_name
FROM Employees e
LEFT JOIN Departments d ON e.department_id = d.id;
ASELECT e.name, d.department_name FROM Employees e LEFT JOIN Departments d e.department_id = d.id;
B;di.d = di_tnemtraped.e NO d stnemtrapeD NIOJ TFEL e seeyolpmE MORF eman_tnemtraped.d ,eman.e TCELES
CSELECT e.name, d.department_name FROM Employees e LEFT JOIN Departments d ON e.department_id = d.id;
DSELECT e.name, d.department_name FROM Employees e LEFT JOIN Departments d ON e.department_id = d.id
Attempts:
2 left
💡 Hint

Look for missing keywords or punctuation in the JOIN clause.

optimization
advanced
2:00remaining
How to optimize a LEFT JOIN query with large tables?

You have two large tables, Orders and Customers. You want to LEFT JOIN them on customer_id. Which option will improve query performance?

AUse SELECT * to get all columns from both tables.
BAdd an index on Orders.customer_id and Customers.id columns.
CRemove the ON clause and use WHERE Orders.customer_id = Customers.id.
DUse a CROSS JOIN instead of LEFT JOIN.
Attempts:
2 left
💡 Hint

Indexes help speed up lookups in join conditions.

🔧 Debug
expert
2:30remaining
Why does this LEFT JOIN query return fewer rows than expected?

Consider these tables:

Products
id | name
1 | Pen
2 | Pencil
3 | Eraser

Sales
id | product_id | quantity
1 | 1 | 10
2 | 2 | 5

Query:

SELECT Products.name, Sales.quantity
FROM Products
LEFT JOIN Sales ON Products.id = Sales.product_id
WHERE Sales.quantity > 0;

Why does this query return only 2 rows instead of 3?

AThe ON condition is incorrect and excludes some rows.
BLEFT JOIN does not include unmatched rows from the left table.
CThe Products table has missing rows.
DThe WHERE clause filters out rows where Sales.quantity is NULL, excluding products without sales.
Attempts:
2 left
💡 Hint

Think about how WHERE affects rows with NULL values after a LEFT JOIN.