SQL Joins · Question 14 · Medium
SQL Joins question 14
Table Riders:
rider_id | name
1 | Manoj
2 | Nisha
3 | Om
Table Trips:
trip_id | rider_id | fare | status
1 | 1 | 120 | DONE
2 | 1 | 80 | CANCELLED
3 | 2 | 200 | CANCELLED
What does this query return?
SELECT r.name, COUNT(t.trip_id)
FROM Riders r
LEFT JOIN Trips t ON r.rider_id = t.rider_id
GROUP BY r.name
ORDER BY r.name;
- Manoj, 2; Nisha, 1; Om, 1
- Manoj, 2; Nisha, 1
- Manoj, 2; Nisha, 1; Om, NULL
- Manoj, 2; Nisha, 1; Om, 0
Answer and explanation
Answer: D. Manoj, 2; Nisha, 1; Om, 0
Explanation: The left join keeps Om with a NULL trip_id. COUNT(t.trip_id) ignores that NULL, so Om shows 0; COUNT(*) would have counted the row and shown 1.
Previous: Table Riders: rider_id | name 1 | Manoj 2 | Nisha 3 | Om Table Trips: trip_id… · Next: Tables Authors(author_id, name) and Books(book_id, title, author_id) exist…
Practise sql joins
Try a fresh set of sql joins questions with an explanation after every answer, free and without signing up: SQL Joins practice. Or sit a full placement mock test.
More sql joins questions
- Table Learners: name | city Isha | Delhi Jatin | Delhi Kiran | Pune Leela |…
- Table Staff: emp_id | name | manager_id 1 | Nandini | NULL 2 | Omkar | 1 3 |…
- Table Riders: rider_id | name 1 | Manoj 2 | Nisha 3 | Om Table Trips: trip_id…
- Tables Authors(author_id, name) and Books(book_id, title, author_id) exist…
- Table Teams: team_id | team | city 1 | CSK | Chennai 2 | MI | Mumbai 3 | RCB |…
- Table Riders: rider_id | name 1 | Manoj 2 | Nisha 3 | Om Table Trips: trip_id…
- Table Invoices: inv_id | customer | total 1 | Zoya | 1000 2 | Arun | 500 Table…
- Table Staff: emp_id | name | manager_id 1 | Nandini | NULL 2 | Omkar | 1 3 |…
- Table A has m rows. Table B has n rows and B.k is its primary key. How many…
More sql practice
Aggregates & GROUP BY · Subqueries & CTEs · Window Functions · NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge