0

I am trying to practice my SQL skills, I have this schema:

enter image description here

And i came up with this query:

SELECT vehicle_id 
FROM reservation 
WHERE NOT (pickup_date<='2017-05-20' 
AND end_date>='2017-05-15')

it will return rows of vehicle_ids, I want to be able to use those vehicle ids and return vehicle_name from vehicle table. How to go about this? I have heard of SQL joins will a JOIN achieve this goal?

edmamerto
  • 7,605
  • 11
  • 42
  • 66
  • 1
    Hint: `JOIN` the tables together. – Gordon Linoff May 12 '17 at 16:37
  • Yes, a JOIN is what you want. – Shawn May 12 '17 at 17:04
  • Also be careful working with dates. If that is a datetime, then your date isn't 2017-05-15; it's 2017-05-15 00:00:00.000... depending on the datatype of the field. So 2017-05-15 12:55:55.555 might be unintentionally missed. Also, if dealing with decimal seconds, be wary of the precision of the datatype. In some cases, 2017-05-15 23.59.59.999 can be interpreted the same as 2017-05-16 00:00:00.000. – Shawn May 12 '17 at 17:12

1 Answers1

0

I am not sure of what you ask for, but if you want one query to return both the vehicle ids and the vehicle names, you should do this :

SELECT r.vehicle_id, v.vehicle_name
FROM reservation r
LEFT JOIN vehicle v ON r.vehicle_id = v.vehicle_id
WHERE NOT (r.pickup_date<='2017-05-20'
AND r.end_date>='2017-05-15')

This will return two columns : one of vehicle ids, and one of vehicle names.

(sorry for the french word 'vehicule')

ttous
  • 336
  • 1
  • 3
  • 12
  • NOTE: Please don't use comma syntax for JOINs. http://stackoverflow.com/questions/894490/sql-left-join-vs-multiple-tables-on-from-line – Shawn May 12 '17 at 17:03
  • Or, re anti-comma arguments being specious: http://stackoverflow.com/a/25957600/3404097. – philipxy May 12 '17 at 23:13