Excel to SQL · Lesson 4 of 12

SUM, COUNT and AVERAGE

In Excel, you'd reach for
fx=SUM(D:D), =COUNT(D:D), =AVERAGE(D:D), =MIN, =MAX

In Excel you put =SUM(D2:D601) under a column. In SQL you ask for the total directly, and get one row back.

The names are nearly the same: SUM, COUNT, AVG (not AVERAGE), MIN, MAX. Give each answer a readable name with AS, like a column header.

Try itEdit the query, then Run
ordersunits_soldavg_units_per_orderbiggest_order
60018433.076

1 row

COUNT(*) counts rows. COUNT(column) counts rows where that column isn't empty. COUNT(DISTINCT column) counts different values, which is the one Excel makes painful.

Your turn

How many different customers have placed an order? Call the answer buyers.

This page on one card

SUM, COUNT and AVERAGE: the Excel way (=SUM(D:D)) and the SQL query, side by side
SUM, COUNT and AVERAGE, on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows