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
SELECT Employees.name, Departments.department_name FROM Employees LEFT JOIN Departments ON Employees.department_id = Departments.id ORDER BY Employees.id;
Remember, LEFT JOIN keeps all rows from the left table and matches rows from the right table if possible.
The LEFT JOIN returns all employees. For those with a matching department_id, it shows the department name. For Charlie, who has NULL department_id, the department_name is NULL.
Choose the correct description of what a LEFT JOIN does in SQL.
Think about which table's rows are always kept and what happens when there is no match.
LEFT JOIN keeps all rows from the left table. If there is no matching row in the right table, the right table columns show NULL.
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;
Look for missing keywords or punctuation in the JOIN clause.
Option A is missing the ON keyword before the join condition, causing a syntax error.
You have two large tables, Orders and Customers. You want to LEFT JOIN them on customer_id. Which option will improve query performance?
Indexes help speed up lookups in join conditions.
Adding indexes on the join columns helps the database find matching rows faster, improving LEFT JOIN performance.
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?
Think about how WHERE affects rows with NULL values after a LEFT JOIN.
The WHERE clause filters rows after the join. Since unmatched Sales rows have NULL quantity, the condition Sales.quantity > 0 excludes them, reducing rows.