0
0
SQLquery~10 mins

DATE arithmetic (DATEDIFF, DATE_ADD) in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the number of days between two dates.

SQL
SELECT DATEDIFF([1], '2024-01-01') AS days_diff;
Drag options to blanks, or click blank then click option'
A'2023-12-31'
B'2024-01-10'
C'2024-02-01'
D'2024-01-01'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dates in the wrong order, which changes the sign of the result.
Using a date format not recognized by SQL.
2fill in blank
medium

Complete the code to add 7 days to a given date.

SQL
SELECT DATE_ADD('2024-03-01', INTERVAL [1] DAY) AS new_date;
Drag options to blanks, or click blank then click option'
A1
B0
C7
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong interval unit like MONTH instead of DAY.
Adding zero days which returns the same date.
3fill in blank
hard

Fix the error in the code to subtract 10 days from a date.

SQL
SELECT DATE_ADD('2024-05-15', INTERVAL [1] DAY) AS new_date;
Drag options to blanks, or click blank then click option'
A-10
B10
CDAY -10
DINTERVAL -10 DAY
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the negative sign outside the INTERVAL keyword.
Using incorrect syntax like 'DAY -10'.
4fill in blank
hard

Fill both blanks to calculate the difference in days between two dates stored in columns.

SQL
SELECT DATEDIFF([1], [2]) AS days_between FROM orders;
Drag options to blanks, or click blank then click option'
Aorder_date
Bdelivery_date
C'2024-01-01'
D'2024-12-31'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the columns and getting negative results.
Using fixed dates instead of column names.
5fill in blank
hard

Fill all three blanks to create a dictionary of event names and their day differences from today.

SQL
SELECT JSON_OBJECT([1], DATEDIFF([2], CURRENT_DATE)) AS days_diff FROM events WHERE [3] > CURRENT_DATE;
Drag options to blanks, or click blank then click option'
Aevent_date
Devent_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same column for key and filter incorrectly.
Not filtering future dates properly.