Complete the code to find the number of days between two dates.
SELECT DATEDIFF([1], '2024-01-01') AS days_diff;
The DATEDIFF function calculates the difference in days between two dates. Here, we find days between '2024-01-10' and '2024-01-01', which is 9 days.
Complete the code to add 7 days to a given date.
SELECT DATE_ADD('2024-03-01', INTERVAL [1] DAY) AS new_date;
The DATE_ADD function adds a specified interval to a date. Adding 7 days to '2024-03-01' results in '2024-03-08'.
Fix the error in the code to subtract 10 days from a date.
SELECT DATE_ADD('2024-05-15', INTERVAL [1] DAY) AS new_date;
To subtract days using DATE_ADD, use a negative number in the INTERVAL. So, INTERVAL -10 DAY subtracts 10 days.
Fill both blanks to calculate the difference in days between two dates stored in columns.
SELECT DATEDIFF([1], [2]) AS days_between FROM orders;
To find how many days passed from order to delivery, subtract order_date from delivery_date. So, DATEDIFF(delivery_date, order_date).
Fill all three blanks to create a dictionary of event names and their day differences from today.
SELECT JSON_OBJECT([1], DATEDIFF([2], CURRENT_DATE)) AS days_diff FROM events WHERE [3] > CURRENT_DATE;
This query selects event names as keys and calculates days until the event by subtracting current date from event_date. It filters future events only.