Add up and count

INPUT · Slides

Seeing only the kinds of value

01 / 05

The table for this chapter

From here on you are using the takings from a craft market. The table is called sales, and one row is one record of "which stall sold what, and how many".

  • item … the name of the thing
  • stall … the stall that sold it
  • price … the price of one
  • sold … how many went
  • day … the day it sold
  • rating … what the customers thought

Eight rows in all. A small table, but you are going to read a trend out of it.

SELECT * FROM sales;

Result

1 | mug | Komorebi Studio | 1000 | 3 | 2026-05-02 | 5
2 | wooden spoon | Komorebi Studio | 400 | 9 | 2026-05-02 | 4
3 | flower brooch | Komorebi Studio | 700 | 6 | 2026-05-03 | 
4 | cloth bag | Tsumugi Goods | 1800 | 4 | 2026-05-02 | 
5 | coaster | Tsumugi Goods | 600 | 14 | 2026-05-03 | 3
6 | glass dish | Tsumugi Goods | 900 | 6 | 2026-05-03 | 5
7 | ceramic vase | Yurari Workshop | 2400 | 2 | 2026-05-02 | 4
8 | leather bookmark | Yurari Workshop | 1200 | 4 | 2026-05-03 | 3

02 / 05

The same value keeps coming back

Wonder which stalls were there, pull out stall, and the same names come back over and over. One row is one sale, so the more a stall sold, the more often it appears.

What you wanted was the kinds of value, and this is hard to count.

SELECT stall FROM sales;

Result

Komorebi Studio
Komorebi Studio
Komorebi Studio
Tsumugi Goods
Tsumugi Goods
Tsumugi Goods
Yurari Workshop
Yurari Workshop

03 / 05

DISTINCT strips the repeats

Write DISTINCT immediately after SELECT and rows with the same value get folded into one.

Read it as "from sales, select stall, but no repeats". Now you can see at a glance that three stalls were trading.

SELECT DISTINCT stall FROM sales;

Result

Komorebi Studio
Tsumugi Goods
Yurari Workshop

04 / 05

Two columns means combinations

Line up two columns with DISTINCT and you get the kinds of combination. It does not fold each column separately — it compares whole rows.

The example below is a list of which stall traded on which day.

SELECT DISTINCT stall, day  FROM sales;

Result

Komorebi Studio | 2026-05-02
Komorebi Studio | 2026-05-03
Tsumugi Goods | 2026-05-02
Tsumugi Goods | 2026-05-03
Yurari Workshop | 2026-05-02
Yurari Workshop | 2026-05-03

05 / 05

Empty is a kind too

Some rows had nothing in rating. DISTINCT keeps that as one of the kinds. It does not quietly disappear.

You can set the order with ORDER BY. The empty one comes first. Let us write some.

SELECT DISTINCT rating FROM sales  ORDER BY rating;

Result

3
4
5