#1 India's Top IT Training Institute

On Demand Courses

9 programs

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

SQL Analytics Functions
PDF Downloads

Download the SQL analytics functions PDF from Uncodemy: ranking, LAG and LEAD, running totals and moving averages with tested queries.

Prepared by: Uncodemy trainer team Reviewed by: Uncodemy Last reviewed: 27 Oct 2026
Question-to-Pattern Map
1
Growth rateHow did we change month on month?
2
Running totalHow are we doing so far this year?
3
Percent of totalWhat share does each item contribute?
4
Top-N per groupWho are the top N in each group?
5
Pareto or ABC analysisWhich items drive most of the revenue?
6
Moving averageIs performance smooth or noisy?

Quick Answer

This page offers Uncodemy's SQL analytics functions guide as a PDF download. It is a cookbook of patterns that analysts use to answer business questions: growth over time, running totals, shares, rankings, top-N per group and a Pareto view of what drives revenue. Every pattern is shown with SQL you can run in SQLite.

Download

SQL Analytics Functions PDF

Download the guide below. Each download is its own file, and everything in it is also explained on this page.

1 PDF Available
PDF 01 Main Guide

SQL Analytics Functions Guide

A cookbook of business metric recipes: month-on-month growth, running totals, percent of total, top-N per group, Pareto analysis and moving averages, with a window function chooser and common pitfalls.

PDF Free Oct 2026
Download PDF

A short form asks for your details before the download starts. Your details are used only to share study material and course information.

01

From functions to business questions

Learning that SUM, LAG and RANK exist is only the start. Analysts earn their keep by knowing which pattern answers which business question. How did revenue change from last month? Which products make up eighty percent of sales? Who are the top three customers in each city? What is the running total against target? Each of these questions has a standard recipe in SQL, built from aggregation, window functions and a CTE or two. This guide collects the most common recipes so that you can recognise them, adapt them and check them.

The examples use these tables: customers(customer_id, name, city, signup_date), orders(order_id, customer_id, order_date, status, order_total), order_items(order_id, product_id, quantity, unit_price) and products(product_id, product_name, category). Dates are text in YYYY-MM-DD form, so the examples run in SQLite and need small changes in other databases.

02

Question-to-pattern map

Start from the business question, then pick the pattern and the tools it needs.

Business questionPatternKey tools
How did we change month on month?Growth rateGROUP BY month, LAG
How are we doing so far this year?Running totalSUM() OVER (ORDER BY ...)
What share does each item contribute?Percent of totalSUM() OVER ()
Who are the top N in each group?Top-N per groupROW_NUMBER or RANK with PARTITION BY
Which items drive most of the revenue?Pareto or ABC analysisCumulative share window
Is performance smooth or noisy?Moving averageAVG() OVER with a frame
03

Month-on-month growth

Monthly revenue and the percentage change from the previous month.

WITH monthly AS (
  SELECT strftime('%Y-%m', order_date) AS month, SUM(order_total) AS revenue
  FROM orders WHERE status <> 'cancelled'
  GROUP BY strftime('%Y-%m', order_date)
)
SELECT month, revenue,
       ROUND((revenue - LAG(revenue) OVER (ORDER BY month)) * 100.0
             / LAG(revenue) OVER (ORDER BY month), 1) AS mom_pct
FROM monthly
ORDER BY month;
04

Running total and percent of total

Cumulative revenue by month and each month's share of the overall total.

WITH monthly AS (
  SELECT strftime('%Y-%m', order_date) AS month, SUM(order_total) AS revenue
  FROM orders WHERE status <> 'cancelled'
  GROUP BY strftime('%Y-%m', order_date)
)
SELECT month, revenue,
       SUM(revenue) OVER (ORDER BY month)                        AS running_total,
       ROUND(revenue * 100.0 / SUM(revenue) OVER (), 1)          AS pct_of_total
FROM monthly
ORDER BY month;
05

Top 3 customers in each city

ROW_NUMBER gives a unique rank inside each partition. Use RANK instead if ties should share a place.

WITH customer_revenue AS (
  SELECT c.city, c.customer_id, c.name, SUM(o.order_total) AS revenue
  FROM customers c
  JOIN orders o ON o.customer_id = c.customer_id AND o.status <> 'cancelled'
  GROUP BY c.city, c.customer_id, c.name
)
SELECT *
FROM (
  SELECT cr.*,
         ROW_NUMBER() OVER (PARTITION BY city ORDER BY revenue DESC) AS rn
  FROM customer_revenue cr
)
WHERE rn <= 3
ORDER BY city, rn;
06

Pareto analysis of products

Ranks products by revenue and shows the cumulative share, so you can see how few products produce most of the revenue.

WITH product_revenue AS (
  SELECT p.product_name, SUM(oi.quantity * oi.unit_price) AS revenue
  FROM order_items oi
  JOIN products p ON p.product_id = oi.product_id
  GROUP BY p.product_name
)
SELECT product_name, revenue,
       ROUND(SUM(revenue) OVER (ORDER BY revenue DESC) * 100.0
             / SUM(revenue) OVER (), 1) AS cumulative_pct
FROM product_revenue
ORDER BY revenue DESC;
07

Three-month moving average

Smooths month-to-month noise using a window frame of the current and two previous months.

WITH monthly AS (
  SELECT strftime('%Y-%m', order_date) AS month, SUM(order_total) AS revenue
  FROM orders WHERE status <> 'cancelled'
  GROUP BY strftime('%Y-%m', order_date)
)
SELECT month, revenue,
       ROUND(AVG(revenue) OVER (ORDER BY month
             ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS moving_avg_3m
FROM monthly
ORDER BY month;
08

Checks that keep recipes honest — and common mistakes

Checks that keep recipes honest

  • Confirm the grain before applying window functions: one row per month, per customer, and so on.
  • Handle the first period, where LAG returns NULL.
  • Watch ties: RANK, DENSE_RANK and ROW_NUMBER treat them differently.
  • State the frame explicitly for moving averages.
  • Reconcile totals with a simple GROUP BY query.

Common mistakes

  • Applying a window function before aggregating, so the result is per row, not per month.
  • Dividing integers and getting zero; multiply by 1.0.
  • Forgetting PARTITION BY so ranks run across all groups.
  • Treating a small sample as a trend: check how many rows sit behind a percentage.
09

Building your own cookbook

Keep a file of queries that you have written and tested, with a sentence about the question each answers and the grain of the result. Over time it becomes a reference that speeds up new work and helps you explain your approach in interviews.

10

Choosing a window function

GoalFunction
Rank with ties and gapsRANK()
Rank with ties and no gapsDENSE_RANK()
Unique row numberROW_NUMBER()
Previous or next row valueLAG() and LEAD()
Running total or moving averageSUM() or AVG() with OVER and a frame
Split into equal groupsNTILE(n)
First or last value in a groupFIRST_VALUE() and LAST_VALUE()
11

Window function pitfalls

  • You cannot filter on a window function in WHERE; use a CTE or subquery, then filter.
  • Frames matter: the default frame with ORDER BY may not be the one you expect, so state it explicitly when it matters.
  • Always include a tie-breaker in ORDER BY for repeatable results.

Learn with a trainer

Want to answer business questions with SQL the way analysts do at work? Explore Uncodemy's Data Analytics course and learn from industry trainers.

View Course
About these PDFs: These PDFs are study material for learning and revision. They are not a certificate, a degree or a promise of a job, and technology changes, so check official documentation for the latest details. Spot an error? Write to info@uncodemy.com and the trainer team will review it.
FAQ

Frequently Asked Questions

What are SQL analytics functions?
They are SQL features, mainly window functions and aggregates, used to calculate running totals, rankings, growth and shares without collapsing rows.
How do I calculate month-on-month growth in SQL?
Aggregate revenue by month in a CTE, then use LAG to get the previous month's value and compute the percentage difference.
How do I get the top N rows per group?
Use ROW_NUMBER or RANK with PARTITION BY the group and ORDER BY the measure, then filter on the rank.
What is a Pareto analysis in SQL?
It ranks items by contribution and calculates the cumulative share, showing which few items drive most of the total.
What is the difference between RANK and ROW_NUMBER?
ROW_NUMBER gives every row a unique number, while RANK gives tied rows the same number and then skips values.
Are these PDFs free to download?
Yes, the SQL analytics functions PDF is free. A short form asks for your details before the download starts.