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;
  1. Nisha, 3; Om, NULL
  2. Nisha, 3
  3. Manoj, 1; Manoj, 2; Nisha, 3; Om, NULL
  4. 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

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