Beginner LevelSQL & MySQL
How do Date Functions (NOW, DATE_ADD, DATEDIFF, EXTRACT) manipulate timestamps?
❓ Interview Prompt: Explain SQL Date and Time functions: `NOW()`, `CURRENT_TIMESTAMP`, `DATE_ADD()` / `INTERVAL`, `DATEDIFF()`, and `EXTRACT()`.
SQL date and time functions handle temporal calculations and comparisons:
•
'NOW()' / 'CURRENT_TIMESTAMP': Returns the current system date and time.
•
'DATE_ADD(date, INTERVAL value unit)': Adds a time interval (e.g. 'DATE_ADD(NOW(), INTERVAL 7 DAY)').
•
'DATEDIFF(expr1, expr2)': Calculates the difference in days between two date expressions.
•
'EXTRACT(unit FROM date)': Extracts date components (YEAR, MONTH, DAY, HOUR).
Code Implementation Example
sql
SELECT
id,
created_at,
DATEDIFF(NOW(), created_at) AS account_age_days
FROM users
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY);💡Calculating account age and filtering recent registrations using date functions.
🎯 Interview Key Takeaway
Use built-in date functions and INTERVAL math to execute precise temporal range filtering.
#SQL#Date Functions#NOW#DATEDIFF#Beginner
Back to all SQL & MySQL Questions→