Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dates
Time
Timestamps
Extract functions
Timezone conversion
Current date and time
37. Current date & time with intervals
Review

Instruction

All right. How can we use these functions in our queries? We can use them to find facts which took place in the last week, last month etc. Take a look:

SELECT id
FROM flight
WHERE date > CURRENT_DATE - INTERVAL '7' DAY;

The above query will find all flights which took place in the last 7 days.

We use the structure INTERVAL 'x' UNIT where x is the number and UNIT is SECOND, MINUTE, HOUR, DAY, MONTH or YEAR. Our database takes the current date and puts it back 7 days.

Exercise

Find the id of all the aircraft which were produced earlier than 3 months ago.

Stuck? Here's a hint!

Use CURRENT_DATE - INTERVAL '3' MONTH to get the date 3 months ago.