TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2784 874
SELECT
customer_id,
order_date,
amount,

ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS order_number,

SUM(amount) OVER (
PARTITION BY customer_id
ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
) AS running_spending,

LAG(amount) OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS previous_order_amount

FROM orders;


Now we have:

Order number

+

Running spending

+

Previous order amount

all while preserving the order-level rows.

🏢 26. Real-World Business Example

Suppose a company wants to analyze customer transactions.

We want:

• Transaction date

• Transaction amount

• Previous transaction

• Difference from previous transaction

• Running customer spending

SELECT
customer_id,
transaction_date,
amount,

LAG(amount) OVER (
PARTITION BY customer_id
ORDER BY transaction_date
) AS previous_amount,

amount - LAG(amount) OVER (
PARTITION BY customer_id
ORDER BY transaction_date
) AS amount_change,

SUM(amount) OVER (
PARTITION BY customer_id
ORDER BY transaction_date
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
) AS running_spending

FROM transactions;


This is a realistic analytical SQL pattern.

🎤 SQL Interview Questions

Q1. What is a Window Function?

A function that performs calculations across a set of related rows while retaining the individual rows in the result.

Q2. What does "OVER()" do?

It defines the window of rows over which the function operates.

Q3. What does "PARTITION BY" do?

It divides rows into logical groups for the Window Function.

Q4. Difference between GROUP BY and Window Functions?

"GROUP BY" collapses rows into groups.

Window Functions calculate across rows without collapsing them.

Q5. Difference between RANK() and DENSE_RANK()?

"RANK()" leaves gaps after ties.

"DENSE_RANK()" does not.

Example:

RANK → 1, 2, 2, 4

DENSE_RANK → 1, 2, 2, 3

Q6. Difference between ROW_NUMBER() and RANK()?

"ROW_NUMBER()" gives every row a unique number.

"RANK()" gives tied rows the same rank.

Q7. What does LAG() do?

Returns a value from a previous row.

Q8. What does LEAD() do?

Returns a value from a subsequent row.

Q9. How do you calculate a running total?

SUM(amount) OVER (
ORDER BY date_column
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
)


Q10. How do you find the top customer in each region?

Use a ranking Window Function with:

PARTITION BY region
ORDER BY total_spending DESC


Then filter the ranking in an outer query or CTE.

📝 Practice Questions

Practice 1

Rank employees by salary.

SELECT
employee_name,
salary,
RANK() OVER (
ORDER BY salary DESC
) AS salary_rank
FROM employees;


Practice 2

Calculate total spending for each customer while keeping every order.

SELECT
order_id,
customer_id,
amount,

SUM(amount) OVER (
PARTITION BY customer_id
) AS customer_total

FROM orders;


Practice 3

Find the previous order amount for every customer.

SELECT
customer_id,
order_date,
amount,

LAG(amount) OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS previous_amount

FROM orders;


Practice 4

Calculate a running total for each customer.
  • ❤ 1
More from @sqlanalyst
  1. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
  2. Oct 7, 2026SQL Interview Series — Part 4 📌 Question 4: Find the Highest Salary in Each Department Su…
  3. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
  4. Sep 29, 2026SQL Interview Series — Part 2 📌 Question 2: Find Duplicate Records Suppose you have an Em…
  5. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
  6. Sep 29, 2026SQL Interview Series — Part 1 Hi guys, let's start a SQL interview series covering frequen…
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 →