runtime
11 / 88

lesson 11 Calculations & data quality · intermediate

Group into summaries

Summarize rows that share the same group.

the idea

GROUP BY changes the grain of the result from one row per input row to one row per group. Every non-aggregated column in SELECT must be a grouping column; each aggregate is computed within its group.

Dataset: Mobility

trips19 columns · 128,620 rows
trip_idnumber · unique idvendor_idnumber · links to vendorspickup_atdate & timedropoff_atdate & timepassenger_countnumbertrip_distance_milesdecimalrate_code_idnumber · links to rate_codesstore_and_fwd_flagtextpickup_zone_idnumber · links to zonesdropoff_zone_idnumber · links to zonespayment_type_idnumber · links to payment_typesfare_amountdecimaltip_amountdecimaltolls_amountdecimalsurcharge_amountdecimalcongestion_surchargedecimalairport_feedecimalcbd_congestion_feedecimaltotal_amountdecimal
122024-12-31 21:33:432024-12-31 21:39:0021.121N179727.9002.500010.4
222025-01-01 00:01:282025-01-01 00:21:1215.511N249112125.47.476.942.52.50044.81
322025-01-01 00:02:462025-01-01 00:21:2712.981N158162118.45.8502.52.50029.25
422025-01-01 00:04:292025-01-01 00:55:58931.975N132265290020.321000111.32
522025-01-01 00:05:492025-01-01 00:20:0023.451N263107117.74.5402.52.50027.24
622025-01-01 00:06:442025-01-01 00:32:0718.041N132191235.9002.500038.4
722025-01-01 00:09:172025-01-01 00:11:5341.151N23816616.51.502.52.50013
812025-01-01 00:09:522025-01-01 00:26:40131N14150116.35.35052.50026.65
922025-01-01 00:10:312025-01-01 00:21:0515.961N13874124.77.836.947.501.75048.72
1022025-01-01 00:11:512025-01-01 00:28:0612.391N1371584-16.340-2.5-2.500-17.3
trip_idnumber · unique id
1
vendor_idnumber · links to vendors
2
pickup_atdate & time
2024-12-31 21:33:43
dropoff_atdate & time
2024-12-31 21:39:00
passenger_countnumber
2
trip_distance_milesdecimal
1.12
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
179
dropoff_zone_idnumber · links to zones
7
payment_type_idnumber · links to payment_types
2
fare_amountdecimal
7.9
tip_amountdecimal
0
tolls_amountdecimal
0
surcharge_amountdecimal
2.5
congestion_surchargedecimal
0
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
10.4
trip_idnumber · unique id
2
vendor_idnumber · links to vendors
2
pickup_atdate & time
2025-01-01 00:01:28
dropoff_atdate & time
2025-01-01 00:21:12
passenger_countnumber
1
trip_distance_milesdecimal
5.51
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
249
dropoff_zone_idnumber · links to zones
112
payment_type_idnumber · links to payment_types
1
fare_amountdecimal
25.4
tip_amountdecimal
7.47
tolls_amountdecimal
6.94
surcharge_amountdecimal
2.5
congestion_surchargedecimal
2.5
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
44.81
trip_idnumber · unique id
3
vendor_idnumber · links to vendors
2
pickup_atdate & time
2025-01-01 00:02:46
dropoff_atdate & time
2025-01-01 00:21:27
passenger_countnumber
1
trip_distance_milesdecimal
2.98
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
158
dropoff_zone_idnumber · links to zones
162
payment_type_idnumber · links to payment_types
1
fare_amountdecimal
18.4
tip_amountdecimal
5.85
tolls_amountdecimal
0
surcharge_amountdecimal
2.5
congestion_surchargedecimal
2.5
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
29.25
trip_idnumber · unique id
4
vendor_idnumber · links to vendors
2
pickup_atdate & time
2025-01-01 00:04:29
dropoff_atdate & time
2025-01-01 00:55:58
passenger_countnumber
9
trip_distance_milesdecimal
31.97
rate_code_idnumber · links to rate_codes
5
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
132
dropoff_zone_idnumber · links to zones
265
payment_type_idnumber · links to payment_types
2
fare_amountdecimal
90
tip_amountdecimal
0
tolls_amountdecimal
20.32
surcharge_amountdecimal
1
congestion_surchargedecimal
0
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
111.32
trip_idnumber · unique id
5
vendor_idnumber · links to vendors
2
pickup_atdate & time
2025-01-01 00:05:49
dropoff_atdate & time
2025-01-01 00:20:00
passenger_countnumber
2
trip_distance_milesdecimal
3.45
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
263
dropoff_zone_idnumber · links to zones
107
payment_type_idnumber · links to payment_types
1
fare_amountdecimal
17.7
tip_amountdecimal
4.54
tolls_amountdecimal
0
surcharge_amountdecimal
2.5
congestion_surchargedecimal
2.5
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
27.24
trip_idnumber · unique id
6
vendor_idnumber · links to vendors
2
pickup_atdate & time
2025-01-01 00:06:44
dropoff_atdate & time
2025-01-01 00:32:07
passenger_countnumber
1
trip_distance_milesdecimal
8.04
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
132
dropoff_zone_idnumber · links to zones
191
payment_type_idnumber · links to payment_types
2
fare_amountdecimal
35.9
tip_amountdecimal
0
tolls_amountdecimal
0
surcharge_amountdecimal
2.5
congestion_surchargedecimal
0
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
38.4
trip_idnumber · unique id
7
vendor_idnumber · links to vendors
2
pickup_atdate & time
2025-01-01 00:09:17
dropoff_atdate & time
2025-01-01 00:11:53
passenger_countnumber
4
trip_distance_milesdecimal
1.15
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
238
dropoff_zone_idnumber · links to zones
166
payment_type_idnumber · links to payment_types
1
fare_amountdecimal
6.5
tip_amountdecimal
1.5
tolls_amountdecimal
0
surcharge_amountdecimal
2.5
congestion_surchargedecimal
2.5
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
13
trip_idnumber · unique id
8
vendor_idnumber · links to vendors
1
pickup_atdate & time
2025-01-01 00:09:52
dropoff_atdate & time
2025-01-01 00:26:40
passenger_countnumber
1
trip_distance_milesdecimal
3
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
141
dropoff_zone_idnumber · links to zones
50
payment_type_idnumber · links to payment_types
1
fare_amountdecimal
16.3
tip_amountdecimal
5.35
tolls_amountdecimal
0
surcharge_amountdecimal
5
congestion_surchargedecimal
2.5
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
26.65
trip_idnumber · unique id
9
vendor_idnumber · links to vendors
2
pickup_atdate & time
2025-01-01 00:10:31
dropoff_atdate & time
2025-01-01 00:21:05
passenger_countnumber
1
trip_distance_milesdecimal
5.96
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
138
dropoff_zone_idnumber · links to zones
74
payment_type_idnumber · links to payment_types
1
fare_amountdecimal
24.7
tip_amountdecimal
7.83
tolls_amountdecimal
6.94
surcharge_amountdecimal
7.5
congestion_surchargedecimal
0
airport_feedecimal
1.75
cbd_congestion_feedecimal
0
total_amountdecimal
48.72
trip_idnumber · unique id
10
vendor_idnumber · links to vendors
2
pickup_atdate & time
2025-01-01 00:11:51
dropoff_atdate & time
2025-01-01 00:28:06
passenger_countnumber
1
trip_distance_milesdecimal
2.39
rate_code_idnumber · links to rate_codes
1
store_and_fwd_flagtext
N
pickup_zone_idnumber · links to zones
137
dropoff_zone_idnumber · links to zones
158
payment_type_idnumber · links to payment_types
4
fare_amountdecimal
-16.3
tip_amountdecimal
4
tolls_amountdecimal
0
surcharge_amountdecimal
-2.5
congestion_surchargedecimal
-2.5
airport_feedecimal
0
cbd_congestion_feedecimal
0
total_amountdecimal
-17.3

first 10 of 128,620 rows · your queries use all of them

worked example

The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.

  1. Start with one row per trip. Run this. Before GROUP BY, every trip is its own row.

    step-1.sql
    SELECT trip_id, vendor_id, fare_amount
    FROM trips
    LIMIT 10;
  2. Roll up to one row per vendor. Run this. GROUP BY vendor_id counts how many trips each vendor recorded.the pattern

    example.sql
    SELECT vendor_id, COUNT(*) AS trips
    FROM trips
    GROUP BY vendor_id;

common mistake

Grouping by pickup zone and payment type produces one row per combination, which is a finer grain than one row per zone.

challenge 1

Find every pickup zone.

Return pickup_zone_id and trip_count, where trip_count is the number of trips that started there.

challenge-1.sql
 
Output
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Group into summaries — Calculations & data quality · Runtime