SQL: JOINs
Computer Science · Databases
Question 1 of 10
0customers id | name 1 | Jada 2 | Lena 3 | Ava 4 | Cara orders id | customer_id | amount 101 | 3 | 82 102 | 2 | 19 103 | 2 | 32 104 | 4 | 18 105 | 5 | 32 106 | 5 | 89 How many rows does this query return? SELECT * FROM customers INNER JOIN orders ON customers.id = orders.customer_id;
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
SQL: JOINs explained
A JOIN combines rows from two tables in one query by matching a column in one table with a column in the other. This lets a database store related data in separate tables and still answer questions that need both. The matching condition is written after ON.
The main types differ in what happens to rows that have no match:
- INNER JOIN keeps only the rows where a match exists in both tables.
- LEFT JOIN keeps every row from the left table, leaving the right-hand columns empty where there is no match.
- RIGHT JOIN does the same for the right table. Some databases also offer a FULL JOIN, which keeps unmatched rows from both tables.
A common mistake is to use INNER JOIN when a LEFT JOIN is needed. Students with no enrolments then disappear from the results without any error message.
Worked example
The table Students has three rows: Ali (StudentID 1), Bea (StudentID 2) and Cem (StudentID 3). The table Enrolments has three rows: StudentID 1 with course C1, StudentID 1 with course C2, and StudentID 3 with course C1. How many rows does an INNER JOIN return, and how many does a LEFT JOIN return?
SELECT Students.Name, Enrolments.CourseID
FROM Students
INNER JOIN Enrolments ON Students.StudentID = Enrolments.StudentID;
- Ali matches 2 enrolments, Bea matches none, and Cem matches 1.
- INNER JOIN keeps only the matched pairs: 2 + 0 + 1 = 3 rows.
- LEFT JOIN keeps every student, so Bea appears once with an empty CourseID: 2 + 1 + 1 = 4 rows.
Check: 4 is the 3 matched rows plus the 1 unmatched student, Bea.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
customers id | name 1 | Ava 2 | Kai 3 | Cara 4 | Ben
orders id | customer_id | amount 101 | 1 | 21 102 | 5 | 88 103 | 3 | 28 104 | 3 | 54 105 | 1 | 18
How many rows does this query return? SELECT * FROM customers INNER JOIN orders ON customers.id = orders.customer_id;
Medium
customers id | name 1 | Jada 2 | Kai 3 | Lena 4 | Hana 5 | Dev
orders id | customer_id | amount 101 | 3 | 58 102 | 2 | 30 103 | 4 | 39 104 | 2 | 33 105 | 1 | 12 106 | 3 | 61 107 | 5 | 84
How many rows does this query return? SELECT * FROM customers LEFT JOIN orders ON customers.id = orders.customer_id;
Hard
customers id | name 1 | Cara 2 | Eli 3 | Ben
orders id | customer_id | amount 101 | 3 | 64 102 | 1 | 41 103 | 4 | 37 104 | 2 | 45 105 | 2 | 70 106 | 4 | 48 107 | 1 | 71
How many rows does this query return? SELECT customers.name FROM customers LEFT JOIN orders ON customers.id = orders.customer_id WHERE orders.id IS NULL;