Given two tables Employees and Departments:
Employees(id, name, dept_id) Departments(id, dept_name)
What is the output of this query?
SELECT e.name, d.dept_name FROM Employees e LEFT JOIN Departments d ON e.dept_id = d.id ORDER BY e.id;
CREATE TABLE Employees (id INT, name TEXT, dept_id INT); CREATE TABLE Departments (id INT, dept_name TEXT); INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', NULL); INSERT INTO Departments VALUES (10, 'HR'), (30, 'Finance');
LEFT JOIN keeps all rows from the left table even if there is no matching row in the right table.
The LEFT JOIN returns all employees. For Bob, there is no matching department with id 20, so dept_name is NULL. Charlie has NULL dept_id, so no match, dept_name is NULL.
Using the same tables Employees and Departments, what is the output of this query?
SELECT e.name, d.dept_name FROM Employees e RIGHT JOIN Departments d ON e.dept_id = d.id ORDER BY d.id;
CREATE TABLE Employees (id INT, name TEXT, dept_id INT); CREATE TABLE Departments (id INT, dept_name TEXT); INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', NULL); INSERT INTO Departments VALUES (10, 'HR'), (30, 'Finance');
RIGHT JOIN keeps all rows from the right table even if there is no matching row in the left table.
The RIGHT JOIN returns all departments. Department 30 (Finance) has no matching employee, so employee name is NULL.
Which statement correctly describes the difference between LEFT JOIN and RIGHT JOIN?
Think about which table's rows are always kept in the result.
LEFT JOIN keeps all rows from the left table, RIGHT JOIN keeps all rows from the right table, matching rows from the other table if any.
Which option contains a syntax error in the JOIN clause?
Check the placement of the ON and WHERE keywords in JOIN syntax.
The ON clause must immediately follow the JOIN keyword to specify join condition. Using WHERE instead causes syntax error.
You want to find all employees and their departments, but only those employees who belong to departments with name starting with 'S'. Which query is most efficient?
Consider where to put the filtering condition to avoid filtering after the join.
Placing the filter in the ON clause limits rows joined, improving efficiency. Filtering in WHERE after LEFT JOIN converts it effectively to INNER JOIN.