Excellent ๐
Now Date & Time SQL commands in the same exam-perfect pattern you liked:
Question
SQL
Output shown
✔ clear takeaway
(For CURRENT_*, output is example / sample, because it depends on system date & time.)
๐งพ Summary Table (Date & Time Questions)
| No. | Command | Question |
|---|---|---|
| 1 | CURRENT_DATE() | Get today’s date |
| 2 | CURRENT_TIME() | Get current time |
| 3 | CURRENT_TIMESTAMP() | Get current date & time |
| 4 | DATE_PART() | Extract year from a date |
| 5 | DATE_ADD() | Add days to a date |
| 6 | DATE_SUB() | Subtract days from a date |
| 7 | EXTRACT() | Extract year from date |
| 8 | TO_CHAR() | Format a date |
| 9 | TIMESTAMPDIFF() | Difference between two timestamps |
| 10 | DATEDIFF() | Difference between two dates |
1️⃣ CURRENT_DATE()
Question: Show today’s date.
SELECT CURRENT_DATE() AS current_date;
✅ Output (Example)
| current_date |
|---|
| 2025-12-17 |
✔ Returns only the date
✔ No time included
2️⃣ CURRENT_TIME()
Question: Show current system time.
SELECT CURRENT_TIME() AS current_time;
✅ Output (Example)
| current_time |
|---|
| 00:05:30 |
✔ Returns only time
✔ Format depends on database
3️⃣ CURRENT_TIMESTAMP()
Question: Show current date and time.
SELECT CURRENT_TIMESTAMP() AS current_timestamp;
✅ Output (Example)
| current_timestamp |
|---|
| 2025-12-17 00:05:30 |
✔ Returns date + time together
4️⃣ DATE_PART()
Question: Extract year from a given date.
SELECT DATE_PART('year', '2024-04-11') AS extracted_part;
✅ Output
| extracted_part |
|---|
| 2024 |
✔ Extracts a specific part of date
✔ Common parts: year, month, day
5️⃣ DATE_ADD()
Question: Add 1 day to a date.
SELECT DATE_ADD('2024-04-11', INTERVAL 1 DAY) AS new_date;
✅ Output
| new_date |
|---|
| 2024-04-12 |
✔ Used to add days / months / years
6️⃣ DATE_SUB()
Question: Subtract 1 day from a date.
SELECT DATE_SUB('2024-04-11', INTERVAL 1 DAY) AS new_date;
✅ Output
| new_date |
|---|
| 2024-04-10 |
✔ Used to subtract time from a date
7️⃣ EXTRACT()
Question: Extract year from a date.
SELECT EXTRACT(YEAR FROM '2024-04-11') AS extracted_part;
✅ Output
| extracted_part |
|---|
| 2024 |
✔ Similar to DATE_PART
✔ ANSI-SQL standard
8️⃣ TO_CHAR()
Question: Convert date into a specific format.
SELECT TO_CHAR('2024-04-11', 'YYYY-MM-DD') AS formatted_date;
✅ Output
| formatted_date |
|---|
| 2024-04-11 |
✔ Converts date/time to text
✔ Used heavily in reports
9️⃣ TIMESTAMPDIFF()
Question: Find difference in days between two timestamps.
SELECT TIMESTAMPDIFF(
DAY,
'2024-04-10',
'2024-04-11'
) AS difference;
✅ Output
| difference |
|---|
| 1 |
✔ Can calculate in DAY, HOUR, MINUTE, etc.
๐ DATEDIFF()
Question: Find difference in days between two dates.
SELECT DATEDIFF('2024-04-11', '2024-04-10') AS difference_in_days;
✅ Output
| difference_in_days |
|---|
| 1 |
✔ Returns number of days only
✔ Very common in exams
๐ฅ DATE_PART vs EXTRACT (Exam Trap)
| Feature | DATE_PART | EXTRACT |
|---|---|---|
| Standard | PostgreSQL | ANSI SQL |
| Syntax | 'year', date | YEAR FROM date |
| Purpose | Same | Same |
๐ง One-Line Memory Trick
| Function | Use |
|---|---|
| CURRENT_DATE | Today’s date |
| CURRENT_TIME | Current time |
| CURRENT_TIMESTAMP | Date + time |
| DATE_ADD | Add time |
| DATE_SUB | Subtract time |
| DATEDIFF | Date difference |
| TIMESTAMPDIFF | Timestamp difference |
If you want next:
Date-based interview MCQs
Real salary / job portal use-cases
All SQL functions in one mega revision table
Hindi explanation for exams
Just say ๐
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.