0
0
MySQLquery~5 mins

DATE_ADD and DATE_SUB in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the MySQL function DATE_ADD() do?
DATE_ADD() adds a specified time interval to a date and returns the new date.
Click to reveal answer
beginner
How does DATE_SUB() differ from DATE_ADD()?
DATE_SUB() subtracts a specified time interval from a date, while DATE_ADD() adds it.
Click to reveal answer
beginner
Write a simple example of DATE_ADD() to add 5 days to '2024-06-01'.
SELECT DATE_ADD('2024-06-01', INTERVAL 5 DAY); -- Result: '2024-06-06'
Click to reveal answer
intermediate
What types of intervals can you use with DATE_ADD() and DATE_SUB()?
You can use intervals like DAY, MONTH, YEAR, HOUR, MINUTE, SECOND, and more.
Click to reveal answer
beginner
Why would you use DATE_SUB() in a database query?
To find a date before a given date, such as finding records older than a certain number of days.
Click to reveal answer
What will this query return? SELECT DATE_ADD('2024-06-10', INTERVAL 3 DAY);
A'2024-06-03'
B'2024-06-07'
C'2024-06-13'
D'2024-07-10'
Which function subtracts time from a date in MySQL?
ADATE_SUB()
BDATE_ADD()
CDATE_DIFF()
DDATE_REMOVE()
What interval keyword would you use to add 2 months using DATE_ADD()?
ADAY
BMONTH
CYEAR
DHOUR
If you want to find the date 10 days before today, which query is correct?
ASELECT DATE_SUB(CURDATE(), INTERVAL -10 DAY);
BSELECT DATE_ADD(CURDATE(), INTERVAL 10 DAY);
CSELECT DATE_ADD(CURDATE(), INTERVAL -10 DAY);
DSELECT DATE_SUB(CURDATE(), INTERVAL 10 DAY);
Can DATE_ADD() and DATE_SUB() be used with time intervals like HOUR and MINUTE?
AYes, they support various time units including HOUR and MINUTE.
BNo, only DAY and MONTH are supported.
COnly DATE_ADD() supports time intervals.
DOnly DATE_SUB() supports time intervals.
Explain how DATE_ADD() and DATE_SUB() work in MySQL and give an example of each.
Think about adding or subtracting time intervals from a date.
You got /4 concepts.
    Describe a real-life situation where you might use DATE_SUB() in a database query.
    Consider situations like finding expired items or past events.
    You got /3 concepts.