Consider two tables, Employees and Departments:
Employees:
id | name | dept_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 10
Departments:
dept_id | dept_name
10 | Sales
20 | Marketing
What is the output of this query?
SELECT Employees.name, Departments.dept_name
FROM Employees
INNER JOIN Departments ON Employees.dept_id = Departments.dept_id;
SELECT Employees.name, Departments.dept_name FROM Employees INNER JOIN Departments ON Employees.dept_id = Departments.dept_id;
INNER JOIN returns rows where the join condition matches in both tables.
Each employee with a matching department id is joined with that department. Alice and Carol both have dept_id 10 (Sales), Bob has dept_id 20 (Marketing). So all three employees appear with their departments.
Which statement best describes how a LEFT JOIN matches rows between two tables?
Think about which table's rows always appear in the result.
LEFT JOIN returns all rows from the left table. If there is no matching row in the right table, the right table columns are filled with NULL.
Which option contains a syntax error in the JOIN condition?
SELECT * FROM A JOIN B ON A.id = B.id;
Check the operator used in the ON clause.
SQL uses a single equals sign (=) for comparison, not double equals (==). Option C uses '==' which is invalid syntax.
Given tables Orders and Customers, this query returns fewer rows than expected:
SELECT Orders.id, Customers.name
FROM Orders
INNER JOIN Customers ON Orders.customer_id = Customers.id
WHERE Customers.status = 'active';
What is the most likely reason?
Consider how WHERE filters rows after the join.
The INNER JOIN matches orders with customers, but the WHERE clause filters only customers with status 'active'. Orders linked to inactive customers are removed after the join, reducing rows.
You have two large tables, Products and Sales, joined on Products.product_id = Sales.product_id. Which indexing strategy will most improve the join performance?
Think about how indexes help the database find matching rows quickly.
Indexes on both join columns allow the database to quickly locate matching rows in both tables, greatly improving join performance.