0
0
SQLquery~20 mins

How the join engine matches rows in SQL - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Join Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of INNER JOIN with multiple matching rows

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;
SQL
SELECT Employees.name, Departments.dept_name
FROM Employees
INNER JOIN Departments ON Employees.dept_id = Departments.dept_id;
A[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "Marketing"}]
B[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "Marketing"}, {"name": "Carol", "dept_name": "Sales"}]
C[{"name": "Alice", "dept_name": "Sales"}, {"name": "Carol", "dept_name": "Sales"}]
D[{"name": "Alice", "dept_name": "Sales"}]
Attempts:
2 left
💡 Hint

INNER JOIN returns rows where the join condition matches in both tables.

🧠 Conceptual
intermediate
1:30remaining
How does a LEFT JOIN match rows?

Which statement best describes how a LEFT JOIN matches rows between two tables?

AIt returns all rows from the left table and matching rows from the right table; unmatched right rows are filled with NULLs.
BIt returns only rows where both tables have matching values in the join condition.
CIt returns all rows from the right table and matching rows from the left table; unmatched left rows are excluded.
DIt returns all rows from the left table and matching rows from the right table; unmatched right rows are excluded.
Attempts:
2 left
💡 Hint

Think about which table's rows always appear in the result.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in JOIN condition

Which option contains a syntax error in the JOIN condition?

SELECT * FROM A JOIN B ON A.id = B.id;
ASELECT * FROM A JOIN B ON A.id = B.id;
BSELECT * FROM A JOIN B ON A.id = B.id
CSELECT * FROM A JOIN B ON A.id == B.id;
DSELECT * FROM A JOIN B ON A.id = B.id WHERE;
Attempts:
2 left
💡 Hint

Check the operator used in the ON clause.

🔧 Debug
advanced
2:00remaining
Why does this JOIN return fewer rows than expected?

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?

AThe WHERE clause filters out orders with inactive customers after the join, reducing rows.
BSome Orders have customer_id values that do not match any Customers.id, so those orders are excluded.
CThe INNER JOIN includes all orders regardless of customer status, so the WHERE clause has no effect.
DThe query syntax is invalid and causes an error.
Attempts:
2 left
💡 Hint

Consider how WHERE filters rows after the join.

optimization
expert
2:30remaining
Optimizing JOIN performance with indexes

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?

ANo indexes are needed; the database automatically optimizes joins.
BCreate an index on Sales.product_id only.
CCreate an index on Products.product_id only.
DCreate indexes on both Products.product_id and Sales.product_id.
Attempts:
2 left
💡 Hint

Think about how indexes help the database find matching rows quickly.