0
0
SQLquery~20 mins

DATE arithmetic (DATEDIFF, DATE_ADD) in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1:30remaining
Calculate days difference between two dates
Given the table Events with a column event_date, what is the output of this query?
SELECT DATEDIFF('2024-06-15', '2024-06-10') AS days_diff;
SQL
SELECT DATEDIFF('2024-06-15', '2024-06-10') AS days_diff;
A[{"days_diff": 10}]
B[{"days_diff": 5}]
C[{"days_diff": 0}]
D[{"days_diff": -5}]
Attempts:
2 left
💡 Hint
DATEDIFF returns the number of days between the first date and the second date.
query_result
intermediate
1:30remaining
Add days to a date using DATE_ADD
What is the result of this query?
SELECT DATE_ADD('2024-06-01', INTERVAL 10 DAY) AS new_date;
SQL
SELECT DATE_ADD('2024-06-01', INTERVAL 10 DAY) AS new_date;
A[{"new_date": "2024-06-10"}]
B[{"new_date": "2024-05-22"}]
C[{"new_date": "2024-06-21"}]
D[{"new_date": "2024-06-11"}]
Attempts:
2 left
💡 Hint
DATE_ADD adds the specified interval to the given date.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in DATE_ADD usage
Which option contains a syntax error in using DATE_ADD to add 3 months to '2024-01-01'?
ASELECT DATE_ADD('2024-01-01', INTERVAL 3MONTH);
BSELECT DATE_ADD('2024-01-01', INTERVAL 3 MONTH);
CSELECT DATE_ADD('2024-01-01', INTERVAL '3' MONTH);
DSELECT DATE_ADD('2024-01-01', INTERVAL 3 MONTH) AS new_date;
Attempts:
2 left
💡 Hint
Check spacing between the number and the unit in INTERVAL.
query_result
advanced
1:30remaining
Calculate difference in days with reversed dates
What is the output of this query?
SELECT DATEDIFF('2024-06-01', '2024-06-10') AS days_diff;
SQL
SELECT DATEDIFF('2024-06-01', '2024-06-10') AS days_diff;
A[{"days_diff": -9}]
B[{"days_diff": 9}]
C[{"days_diff": 0}]
D[{"days_diff": 10}]
Attempts:
2 left
💡 Hint
DATEDIFF returns positive if first date is later, negative if earlier.
🧠 Conceptual
expert
2:30remaining
Understanding DATE_ADD with mixed intervals
Which option correctly adds 1 year, 2 months, and 10 days to '2023-01-01' using DATE_ADD?
ASELECT DATE_ADD('2023-01-01', INTERVAL 1 YEAR + 2 MONTH + 10 DAY) AS new_date;
BSELECT DATE_ADD('2023-01-01', INTERVAL '1 YEAR 2 MONTH 10 DAY');
CSELECT DATE_ADD(DATE_ADD(DATE_ADD('2023-01-01', INTERVAL 1 YEAR), INTERVAL 2 MONTH), INTERVAL 10 DAY) AS new_date;
DSELECT DATE_ADD('2023-01-01', INTERVAL 1 YEAR), DATE_ADD('2023-01-01', INTERVAL 2 MONTH), DATE_ADD('2023-01-01', INTERVAL 10 DAY);
Attempts:
2 left
💡 Hint
DATE_ADD only accepts one interval at a time; chaining is needed for multiple intervals.