There is a table called Car with columns CarID, Brand, Model, ProdYear, Color
and a table Rent with columns RentID, CarID, RentDate, ReturnDate.
How to SELECT Brand and Model for cars which have been rented in December 2000?
RentDate and ReturnDate are stored in DATETIME format (YYYY-MM-DD)
Advertisement
Answer
For a simplistic scenario:
SELECT DISTINCT Brand, Model FROM Car WHERE (MONTH(RentDate) = 'December' AND YEAR(RentDate)) = 2000 or (MONTH(ReturnDate) = 'December'Year(ReturnDate) = 2000)
If you want to dig further and the following will cover four scenarios:
- Rented earlier but returned in the year of Dec 2000
- rented in 2000 and returned after Dec 2000
- Rented in 2000 and returned also in Dec 2000
SELECT DISTINCT Brand, Model FROM Car WHERE RentDate <='2020-12-31' AND ReturnDate >= '2020-12-01