0
0
SQLquery~20 mins

LEFT JOIN preserving all left rows in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LEFT JOIN Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of LEFT JOIN preserving all left rows
Given two tables, Employees and Departments, what is the output of the following SQL query?

SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.ID
ORDER BY Employees.ID;

Assume the tables contain:
Employees:
ID | Name | DepartmentID
1 | Alice | 10
2 | Bob | 20
3 | Charlie | NULL

Departments:
ID | DepartmentName
10 | HR
20 | IT
SQL
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.ID
ORDER BY Employees.ID;
A[{"Name": "Alice", "DepartmentName": "HR"}, {"Name": "Bob", "DepartmentName": null}, {"Name": "Charlie", "DepartmentName": null}]
B[{"Name": "Alice", "DepartmentName": null}, {"Name": "Bob", "DepartmentName": null}, {"Name": "Charlie", "DepartmentName": null}]
C[{"Name": "Alice", "DepartmentName": "HR"}, {"Name": "Bob", "DepartmentName": "IT"}, {"Name": "Charlie", "DepartmentName": null}]
D[{"Name": "Alice", "DepartmentName": "HR"}, {"Name": "Bob", "DepartmentName": "IT"}]
Attempts:
2 left
💡 Hint
Remember, LEFT JOIN keeps all rows from the left table even if there is no matching row in the right table.
🧠 Conceptual
intermediate
1:30remaining
Understanding LEFT JOIN behavior with NULL keys
Which statement best describes what happens when a LEFT JOIN is performed and the join key in the left table is NULL?
AThe row from the left table is included with NULLs for the right table columns.
BThe row from the left table is excluded because NULL cannot match any value.
CThe join returns an error due to NULL in the join key.
DThe row from the left table is duplicated for every row in the right table.
Attempts:
2 left
💡 Hint
Think about how LEFT JOIN preserves rows from the left table regardless of matches.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in LEFT JOIN query
Which option contains a syntax error in the LEFT JOIN SQL query?
SQL
SELECT e.Name, d.DepartmentName
FROM Employees e
LEFT JOIN Departments d ON e.DepartmentID = d.ID;
ASELECT e.Name, d.DepartmentName FROM Employees e LEFT JOIN Departments d ON e.DepartmentID = d.ID WHERE d.ID IS NOT NULL;
BSELECT e.Name, d.DepartmentName FROM Employees e LEFT JOIN Departments d ON e.DepartmentID = d.ID;
C;DI.d = DItnemtrapeD.e NO d stnemtrapeD NIOJ TFEL e seeyolpmE MORF emaNtnemtrapeD.d ,emaN.e TCELES
DSELECT e.Name, d.DepartmentName FROM Employees e LEFT JOIN Departments d e.DepartmentID = d.ID;
Attempts:
2 left
💡 Hint
Check the ON clause syntax carefully.
optimization
advanced
2:00remaining
Optimizing LEFT JOIN for large tables
You have two large tables, Employees and Departments. Which approach optimizes the LEFT JOIN query performance best?
AEnsure indexes exist on Employees.DepartmentID and Departments.ID before running the LEFT JOIN.
BUse SELECT * to retrieve all columns to avoid specifying columns.
CRun the LEFT JOIN without any indexes and add indexes after query execution.
DUse a CROSS JOIN instead of LEFT JOIN for faster results.
Attempts:
2 left
💡 Hint
Indexes help the database find matching rows faster.
🔧 Debug
expert
2:30remaining
Why does this LEFT JOIN query exclude some left rows?
Consider this query:

SELECT e.Name, d.DepartmentName
FROM Employees e
LEFT JOIN Departments d ON e.DepartmentID = d.ID
WHERE d.DepartmentName = 'HR';

Why does this query exclude employees without a department or with a department other than 'HR'?
ABecause the ON clause is incorrect and filters rows before the join.
BBecause the WHERE clause filters out rows where d.DepartmentName is NULL, removing unmatched left rows.
CBecause LEFT JOIN automatically excludes rows without matches in the right table.
DBecause the query syntax is invalid and causes an error.
Attempts:
2 left
💡 Hint
Think about how WHERE affects rows after the join.