runtime
38 / 88

lesson 38 Window functions · intermediate

Logical query processing & row grain

Follow query processing and track what one row represents.

the idea

SQL clauses are written SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY, but they are evaluated FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. That order explains which names are visible where and how each clause changes the grain. A window function computes across rows without collapsing them.

Dataset: Retail

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

orders6 columns · 6,722 rows
order_idnumber · unique idinvoice_notextcustomer_idnumber · links to customersordered_atdate & timecountrytextorder_typetext
1489438181022009-12-01 09:24:00United Kingdomsale
2489517163292009-12-01 11:34:00United Kingdomsale
3C489528167142009-12-01 11:50:00United Kingdomcancellation
4489529179842009-12-01 11:51:00United Kingdomsale
5489532133942009-12-01 11:58:00United Kingdomsale
6489540129132009-12-01 12:18:00United Kingdomsale
order_idnumber · unique id
1
invoice_notext
489438
customer_idnumber · links to customers
18102
ordered_atdate & time
2009-12-01 09:24:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
2
invoice_notext
489517
customer_idnumber · links to customers
16329
ordered_atdate & time
2009-12-01 11:34:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
3
invoice_notext
C489528
customer_idnumber · links to customers
16714
ordered_atdate & time
2009-12-01 11:50:00
countrytext
United Kingdom
order_typetext
cancellation
order_idnumber · unique id
4
invoice_notext
489529
customer_idnumber · links to customers
17984
ordered_atdate & time
2009-12-01 11:51:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
5
invoice_notext
489532
customer_idnumber · links to customers
13394
ordered_atdate & time
2009-12-01 11:58:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
6
invoice_notext
489540
customer_idnumber · links to customers
12913
ordered_atdate & time
2009-12-01 12:18:00
countrytext
United Kingdom
order_typetext
sale

first 6 of 6,722 rows · your queries use all of them

order_items7 columns · 125,613 rows
order_item_idnumber · unique idorder_idnumber · links to ordersline_numbernumberstock_codetext · links to productsdescriptiontextquantitynumberunit_pricedecimal
11121329DINOSAURS WRITING SET280.98
21221252SET OF MEADOW FLOWER STICKERS301.69
31321100CHARLIE AND LOLA CHARLOTTE BAG301.15
41421033JUMBO BAG CHARLIE AND LOLA TOYS302
51520711JUMBO BAG TOYS601.3
61621410COUNTRY COTTAGE DOORSTOP GREEN322.5
order_item_idnumber · unique id
1
order_idnumber · links to orders
1
line_numbernumber
1
stock_codetext · links to products
21329
descriptiontext
DINOSAURS WRITING SET
quantitynumber
28
unit_pricedecimal
0.98
order_item_idnumber · unique id
2
order_idnumber · links to orders
1
line_numbernumber
2
stock_codetext · links to products
21252
descriptiontext
SET OF MEADOW FLOWER STICKERS
quantitynumber
30
unit_pricedecimal
1.69
order_item_idnumber · unique id
3
order_idnumber · links to orders
1
line_numbernumber
3
stock_codetext · links to products
21100
descriptiontext
CHARLIE AND LOLA CHARLOTTE BAG
quantitynumber
30
unit_pricedecimal
1.15
order_item_idnumber · unique id
4
order_idnumber · links to orders
1
line_numbernumber
4
stock_codetext · links to products
21033
descriptiontext
JUMBO BAG CHARLIE AND LOLA TOYS
quantitynumber
30
unit_pricedecimal
2
order_item_idnumber · unique id
5
order_idnumber · links to orders
1
line_numbernumber
5
stock_codetext · links to products
20711
descriptiontext
JUMBO BAG TOYS
quantitynumber
60
unit_pricedecimal
1.3
order_item_idnumber · unique id
6
order_idnumber · links to orders
1
line_numbernumber
6
stock_codetext · links to products
21410
descriptiontext
COUNTRY COTTAGE DOORSTOP GREEN
quantitynumber
32
unit_pricedecimal
2.5

first 6 of 125,613 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Filter, group, then sort. Run this. WHERE limits to sales, GROUP BY counts per customer, ORDER BY ranks by order count.the pattern

    example.sql
    SELECT customer_id, COUNT(*) AS orders
    FROM orders
    WHERE order_type = 'sale'
    GROUP BY customer_id
    ORDER BY orders DESC
    LIMIT 5;
  2. Keep every line and add a window total. Run this. COUNT(*) OVER (PARTITION BY order_id) adds how many lines each order has without collapsing rows.

    step-2.sql
    SELECT
      order_item_id,
      order_id,
      COUNT(*) OVER (PARTITION BY order_id) AS lines_on_order
    FROM order_items
    WHERE order_id <= 3;
  3. Filter groups after counting. Run this. HAVING keeps only customers with at least 50 sale orders after GROUP BY.

    step-3.sql
    SELECT customer_id, COUNT(*) AS orders
    FROM orders
    WHERE order_type = 'sale'
    GROUP BY customer_id
    HAVING COUNT(*) >= 50
    ORDER BY orders DESC;

common mistake

GROUP BY collapses rows to one per group. To keep every line and still show a group total, use SUM(…) OVER (PARTITION BY …).

challenge 1

For sale orders placed in 2011 by identified customers, return customer_id and units (total quantity) for customers with more than 5,000 units.

Sort by units descending, then customer_id.

what to return

Columns
customer_idunits in this order
Rows
in the order asked
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Logical query processing & row grain — Window functions · Runtime