lesson 10 Calculations & data quality · intermediate
COUNT, SUM & AVG
Count rows and calculate totals and averages.
the idea
COUNT(*) counts rows; COUNT(column) counts rows where that column is known. SUM adds values and AVG divides the sum by the number of known values. Without GROUP BY, an aggregate query returns one row for the whole table. ROUND(value, 2) rounds a numeric result to two decimal places, which is how money and averages are shown from here on.
Dataset: Mobility
trips19 columns · 128,620 rows
| trip_idnumber · unique id | vendor_idnumber · links to vendors | pickup_atdate & time | dropoff_atdate & time | passenger_countnumber | trip_distance_milesdecimal | rate_code_idnumber · links to rate_codes | store_and_fwd_flagtext | pickup_zone_idnumber · links to zones | dropoff_zone_idnumber · links to zones | payment_type_idnumber · links to payment_types | fare_amountdecimal | tip_amountdecimal | tolls_amountdecimal | surcharge_amountdecimal | congestion_surchargedecimal | airport_feedecimal | cbd_congestion_feedecimal | total_amountdecimal |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 2024-12-31 21:33:43 | 2024-12-31 21:39:00 | 2 | 1.12 | 1 | N | 179 | 7 | 2 | 7.9 | 0 | 0 | 2.5 | 0 | 0 | 0 | 10.4 |
| 2 | 2 | 2025-01-01 00:01:28 | 2025-01-01 00:21:12 | 1 | 5.51 | 1 | N | 249 | 112 | 1 | 25.4 | 7.47 | 6.94 | 2.5 | 2.5 | 0 | 0 | 44.81 |
| 3 | 2 | 2025-01-01 00:02:46 | 2025-01-01 00:21:27 | 1 | 2.98 | 1 | N | 158 | 162 | 1 | 18.4 | 5.85 | 0 | 2.5 | 2.5 | 0 | 0 | 29.25 |
| 4 | 2 | 2025-01-01 00:04:29 | 2025-01-01 00:55:58 | 9 | 31.97 | 5 | N | 132 | 265 | 2 | 90 | 0 | 20.32 | 1 | 0 | 0 | 0 | 111.32 |
| 5 | 2 | 2025-01-01 00:05:49 | 2025-01-01 00:20:00 | 2 | 3.45 | 1 | N | 263 | 107 | 1 | 17.7 | 4.54 | 0 | 2.5 | 2.5 | 0 | 0 | 27.24 |
| 6 | 2 | 2025-01-01 00:06:44 | 2025-01-01 00:32:07 | 1 | 8.04 | 1 | N | 132 | 191 | 2 | 35.9 | 0 | 0 | 2.5 | 0 | 0 | 0 | 38.4 |
| 7 | 2 | 2025-01-01 00:09:17 | 2025-01-01 00:11:53 | 4 | 1.15 | 1 | N | 238 | 166 | 1 | 6.5 | 1.5 | 0 | 2.5 | 2.5 | 0 | 0 | 13 |
| 8 | 1 | 2025-01-01 00:09:52 | 2025-01-01 00:26:40 | 1 | 3 | 1 | N | 141 | 50 | 1 | 16.3 | 5.35 | 0 | 5 | 2.5 | 0 | 0 | 26.65 |
| 9 | 2 | 2025-01-01 00:10:31 | 2025-01-01 00:21:05 | 1 | 5.96 | 1 | N | 138 | 74 | 1 | 24.7 | 7.83 | 6.94 | 7.5 | 0 | 1.75 | 0 | 48.72 |
| 10 | 2 | 2025-01-01 00:11:51 | 2025-01-01 00:28:06 | 1 | 2.39 | 1 | N | 137 | 158 | 4 | -16.3 | 4 | 0 | -2.5 | -2.5 | 0 | 0 | -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.
Look at trip-level numbers. Run this. Each row is one trip with fare, tip, and passenger count.
step-1.sqlSELECT trip_id, fare_amount, tip_amount, passenger_count FROM trips LIMIT 10;
Collapse the table to one summary row. Run this. COUNT and AVG over all rows give totals and averages for the whole trips table.the pattern
example.sqlSELECT COUNT(*) AS trips, ROUND(AVG(trip_distance_miles), 2) AS avg_miles FROM trips;
common mistake
COUNT(passenger_count) is smaller than COUNT(*) because flex-fare trips have no recorded passenger count. AVG ignores those trips rather than treating them as zero.