I had two mySql tables of CARS
and DRIVERS
with the attribute licenseNumber
as a foreign key.
What would be the query to find the names of the drivers who did not have a car?
I was thinking:
SELECT DISTINCT D.name
FROM drivers D, cars C
WHERE
D.licenseNumber = C.licenseNumber AND
D.licenseNumber NOT IN (SELECT licenseNumber FROM cars)
But that doesn't return anything.
I have a feeling I'm overthinking it a bit.
Any help?
SELECT D.name
FROM drivers D
WHERE NOT EXISTS(SELECT * FROM cars c
WHERE D.licenseNumber = C.licenseNumber)
It is not possible for any row to match that query (if licenseNumber was not in the cars table, how would cars.licenseNumber = drivers.licenseNumber?).
SELECT DISTINCT name FROM drivers WHERE licenseNumber NOT IN (SELECT licenseNumber FROM cars)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With