Lesson 5 of 8 Amazon Athena and Data Analysis

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.

OperationSQL
AggregationSUM, AVG, COUNT, MIN, MAX
Grouping and subtotalsGROUP BY, with ROLLUP or CUBE
Rankings and running totalsWindow functions: RANK, ROW_NUMBER, SUM() OVER
PivotingTurning 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.

Keeping the latest record per customer with ROW_NUMBER. The window function partitions the change rows by customer_id and numbers them by updated_at, newest first, so each customer's latest row has rn = 1. The outer query keeps only those rows.
The rows are illustrative.

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.

TypeQuestionExample
DescriptiveWhat happened?Monthly sales by region
DiagnosticWhy did it happen?Drill-down into a sales drop
PredictiveWhat will happen?Demand forecasts with ML
PrescriptiveWhat should we do?Recommended stock levels

The following diagram shows the four types in order.

The four types of analysis, each building on the previous one. Descriptive analysis reports what happened, diagnostic analysis explains why, predictive analysis estimates what will happen, and prescriptive analysis recommends what to do.
Examples are illustrative.
Match the pairs

Types of analysis

Exam skills 3.2.6

Match each question with the type of analysis.

Log in to try this activity Interactive activities are free with an account.

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.

UsageChooseExamples
Steady, heavy, predictableProvisionedRedshift clusters, EMR on EC2, Athena provisioned capacity
Spiky, occasional or unpredictableServerless (no cost when idle)Athena on demand, Redshift Serverless, EMR Serverless, Glue
Sort into groups

Provisioned or serverless?

Exam skills 3.2.5

Choose provisioned or serverless analytics for each usage pattern.

Log in to try this activity Interactive activities are free with an account.
Worked example

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).

Check yourself

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.