Applying Analytical SQL and Types of Analysis
This lesson explains the analytical operations analysts perform, which data engineers prepare data for, and the trade-off between provisioned and serverless analytics capacity.
Learning outcomes
After completing this lesson, you will be able to:
- Use aggregate functions, GROUP BY and window functions.
- Distinguish descriptive, diagnostic, predictive and prescriptive analysis.
- Choose between provisioned and serverless analytics.
Analytical SQL operations
Most analytical questions are answered with a few SQL constructs. Aggregate functions such as SUM, AVG and COUNT summarise many rows into one value. GROUP BY computes those values per group, such as sales per region, and its ROLLUP and CUBE options add subtotals and grand totals.
Window functions calculate across related rows while keeping every row: RANK and ROW_NUMBER number rows within a group, for example to find each customer's latest order, and SUM() OVER produces running totals and moving averages. Pivoting turns rows into columns, such as one column per month. The following table summarises the operations.
| Operation | SQL |
|---|---|
| Aggregation | SUM, AVG, COUNT, MIN, MAX |
| Grouping and subtotals | GROUP BY, with ROLLUP or CUBE |
| Rankings and running totals | Window functions: RANK, ROW_NUMBER, SUM() OVER |
| Pivoting | Turning rows into columns |
Example. A seven-day moving average is calculated as follows.
SELECT day,
AVG(sales) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS avg_7d
FROM daily_sales;The following diagram shows the row numbers and the rows the query keeps.

Types of analysis
Analysis is commonly classified by the question it answers, and each type builds on the previous one. Descriptive analysis reports what happened, such as monthly sales by region. Diagnostic analysis explains why, for example by drilling into a sales drop to find the products and stores responsible. Predictive analysis estimates what will happen, usually with machine learning, such as demand forecasts. Prescriptive analysis recommends what to do, such as the stock level to order. The following table summarises the types.
| Type | Question | Example |
|---|---|---|
| Descriptive | What happened? | Monthly sales by region |
| Diagnostic | Why did it happen? | Drill-down into a sales drop |
| Predictive | What will happen? | Demand forecasts with ML |
| Prescriptive | What should we do? | Recommended stock levels |
The following diagram shows the four types in order.

Types of analysis
Provisioned versus serverless analytics
Analytics services come in provisioned and serverless forms, and the choice is mainly about the usage pattern. Provisioned capacity, such as Redshift clusters, EMR on EC2 or Athena provisioned capacity, is paid for while it exists, whether busy or not. For steady, heavy, predictable use it is cheaper per unit of work and gives consistent performance. Serverless options, such as Athena on demand, Redshift Serverless, EMR Serverless and Glue, charge only for the work done and cost nothing when idle, which suits spiky, occasional or unpredictable use. The following table summarises the choice.
| Usage | Choose | Examples |
|---|---|---|
| Steady, heavy, predictable | Provisioned | Redshift clusters, EMR on EC2, Athena provisioned capacity |
| Spiky, occasional or unpredictable | Serverless (no cost when idle) | Athena on demand, Redshift Serverless, EMR Serverless, Glue |
Provisioned or serverless?
The latest record per customer
A table receives every change to customer records, and an analysis needs only the latest version of each customer. A window function numbers each customer's rows by recency, as follows.
SELECT * FROM (
SELECT c.*, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) AS rn
FROM customer_changes c
) WHERE rn = 1;The same pattern removes duplicates delivered at least once by a stream. A running total uses SUM(amount) OVER (PARTITION BY region ORDER BY day), and subtotals per region with a grand total use GROUP BY ROLLUP (region, product).
Which SQL features produce subtotals and grand totals?
Show answer
ROLLUP or CUBE in the GROUP BY clause.
Exam Tips
Remember for the exam
- A seven-day moving average uses a window function with ROWS BETWEEN 6 PRECEDING AND CURRENT ROW.
- Subtotals and grand totals come from ROLLUP or CUBE.
- Rankings use RANK or ROW_NUMBER window functions.
- Spiky, occasional analytics suit serverless options; steady, heavy use suits provisioned options.
- Know the four types of analysis: descriptive, diagnostic, predictive and prescriptive.