SQL Joins · Question 17 · Hard
SQL Joins question 17
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
The ON clause has an extra condition on the left table. What does this query return?
SELECT r.name, t.trip_id
FROM Riders r
LEFT JOIN Trips t
ON r.rider_id = t.rider_id AND r.name <> 'Manoj'
ORDER BY r.name;
- Nisha, 3; Om, NULL
- Nisha, 3
- Manoj, 1; Manoj, 2; Nisha, 3; Om, NULL
- Manoj, NULL; Nisha, 3; Om, NULL
Answer and explanation
Answer: D. Manoj, NULL; Nisha, 3; Om, NULL
Explanation: ON only decides which right rows match; it can never remove a left row. For Manoj the condition is false for every trip, so he is kept once with NULL. To drop Manoj the test would have to be in WHERE.
Previous: Table Teams: team_id | team | city 1 | CSK | Chennai 2 | MI | Mumbai 3 | RCB |… · Next: Table Invoices: inv_id | customer | total 1 | Zoya | 1000 2 | Arun | 500 Table…
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 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 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