TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2787 890
๐Ÿš€ SQL Roadmap 2026 โ€” Part 15

๐Ÿ“… SQL Date & Time Functions โ€” Working with Dates, Time & Time-Based Analytics

Almost every real-world analytics project involves dates.

Examples:

โ€ข When was the order placed?

โ€ข How many days did delivery take?

โ€ข What was the revenue in January?

โ€ข Which month had the highest sales?

โ€ข How many customers purchased this year?

โ€ข How long has an account been inactive?

โ€ข What is the month-over-month growth?

โ€ข Which transactions breached the SLA?

SQL provides powerful date and time functions to answer these questions.

๐Ÿง  1. Common Date & Time Data Types

Different databases support different date/time types, but commonly you'll encounter:

โ€ข DATE

โ€ข TIME

โ€ข TIMESTAMP

โ€ข DATETIME

DATE stores only the date: 2026-09-18

TIME stores only time: 14:30:25

TIMESTAMP stores both date and time: 2026-09-18 14:30:25

The exact data types and behavior vary by database.

๐Ÿ“… 2. Current Date

Many databases provide a function for the current date.

For example:

SELECT CURRENT_DATE;


Result: 2026-09-18

The exact function can vary by SQL dialect.

โฐ 3. Current Date and Time

A common SQL expression is:

SELECT CURRENT_TIMESTAMP;


It returns the current date and time.

For example: 2026-09-18 21:39:00

Again, exact formatting depends on the database.

๐Ÿ” 4. Extracting Year, Month & Day

Suppose: order_date = 2026-09-18

We may want: Year โ†’ 2026, Month โ†’ 9, Day โ†’ 18

A common SQL approach is:

SELECT
EXTRACT(YEAR FROM order_date) AS year,
EXTRACT(MONTH FROM order_date) AS month,
EXTRACT(DAY FROM order_date) AS day
FROM orders;


Some databases use functions such as YEAR(order_date), MONTH(order_date), DAY(order_date).

So always check the syntax for your SQL dialect.

๐Ÿ“Š 5. Revenue by Year

Suppose we want annual revenue.

SELECT
EXTRACT(YEAR FROM order_date) AS order_year,
SUM(amount) AS revenue
FROM orders
GROUP BY EXTRACT(YEAR FROM order_date)
ORDER BY order_year;


Result:

โ€ข 2024 | 850000

โ€ข 2025 | 1120000

โ€ข 2026 | 1380000

This lets us analyze year-over-year business performance.

๐Ÿ“† 6. Revenue by Month

We can group transactions by month.

SELECT
EXTRACT(YEAR FROM order_date) AS year,
EXTRACT(MONTH FROM order_date) AS month,
SUM(amount) AS revenue
FROM orders
GROUP BY
EXTRACT(YEAR FROM order_date),
EXTRACT(MONTH FROM order_date)
ORDER BY
year,
month;


Important: Don't group only by month number if your data spans multiple years.

For example January 2025 and January 2026 both have month = 1. Grouping only by month could incorrectly combine them.

๐Ÿ—“๏ธ 7. Month-Level Grouping

Some databases provide functions that truncate a date to the beginning of a month.

For example, PostgreSQL:

SELECT
DATE_TRUNC('month', order_date) AS month,
SUM(amount) AS revenue
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY month;


This produces values such as 2026-01-01, 2026-02-01 representing each month. This is often convenient for time-series analysis.

๐Ÿ“ˆ 8. Month-over-Month Analysis

Suppose we have monthly revenue: Jan 100000, Feb 120000, Mar 110000. We can use LAG():

WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', order_date) AS month,
SUM(amount) AS revenue
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
)
SELECT
month,
revenue,
LAG(revenue) OVER (ORDER BY month) AS previous_month_revenue
FROM monthly_sales;


๐Ÿ“Š 9. Month-over-Month Growth %

We can extend the previous query:
More from @sqlanalyst
  1. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  2. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  3. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  4. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  5. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
  6. Sep 28, 2026๐Ÿง  Real-World SQL Scenario-Based Questions & Answers 1. Get the 2nd highest salary from thโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’