2. SELECT: reading a table

SQL's most used statement: the columns you want, from a table.

Choosing columns

SELECT reads data. After SELECT come the columns you want, separated by commas; after FROM, the table:

SELECT name, city FROM customer;
namecity
Somchai JaideeBangkok
Malee SrisukChiang Mai
Anan WongsaBangkok
Pim ChaiyaPhuket
Narin ThongdeeKhon Kaen
Kanya BoonmeeChiang Mai
Wichai SaelimBangkok
Siriporn KaewdeePhuket
Tawan RungruangKhon Kaen
Lalita MeesukBangkok
10 rows

The columns come out in the order you write them, so you can put them in any order you like:

SELECT city, name FROM customer;
cityname
BangkokSomchai Jaidee
Chiang MaiMalee Srisuk
BangkokAnan Wongsa
PhuketPim Chaiya
Khon KaenNarin Thongdee
Chiang MaiKanya Boonmee
BangkokWichai Saelim
PhuketSiriporn Kaewdee
Khon KaenTawan Rungruang
BangkokLalita Meesuk
10 rows

Every column with *

* means "every column", in the table's own order:

SELECT * FROM category;
idname
1Drinks
2Bakery
3Snacks
4Stationery
4 rows

SELECT * is handy while you look around. In a real program, name the columns: the statement then says what it uses, and when the table gets a new column one day, the program still gets what it got before.

Repeated rows away with DISTINCT

Several customers live in the same city. To see only which cities there are, put DISTINCT after SELECT:

SELECT city FROM customer;
city
Bangkok
Chiang Mai
Bangkok
Phuket
Khon Kaen
Chiang Mai
Bangkok
Phuket
Khon Kaen
Bangkok
10 rows
SELECT DISTINCT city FROM customer;
city
Bangkok
Chiang Mai
Phuket
Khon Kaen
4 rows

DISTINCT looks at the whole row: with several columns, a row goes only when every column repeats:

SELECT DISTINCT customer_id, status FROM orders;
customer_idstatus
1paid
2shipped
1shipped
3paid
4cancelled
5shipped
2paid
6shipped
7paid
8shipped
4paid
6paid
3cancelled
13 rows

SELECT without a table

SELECT can just work something out, with no FROM, like a calculator:

SELECT 7 * 6, 'hello', 10 / 4, 10 / 4.0;
7 * 6'hello'10 / 410 / 4.0
42hello22.5
1 row

10 / 4 gives 2: a whole number divided by a whole number gives a whole number (the rest is dropped). For a fraction, give one of them a decimal point: 10 / 4.0.

Writing SQL that reads well

-- Every customer: the name and the phone
SELECT
  name,
  phone      -- some have none (shown as NULL)
FROM customer;
namephone
Somchai Jaidee081-234-5678
Malee Srisuk089-111-2222
Anan WongsaNULL
Pim Chaiya086-555-0101
Narin Thongdee082-777-8888
Kanya Boonmee084-246-1357
Wichai Saelim083-999-0000
Siriporn KaewdeeNULL
Tawan Rungruang085-123-4567
Lalita Meesuk087-654-3210
10 rows

NULL in a result means "no value" or "unknown". It is not zero and not empty text; chapter 5 tells its story.

When you mistype

Mistype a column or a table and the database says it doesn't know that name:

SELECT nmae FROM customer;
Error: no such column: nmae
SELECT name FROM customers;
Error: no such table: customers

Read the error: it says exactly which name is wrong. Here the table is customer, without an s.

  1. Show the name (name) and the price (price) of every product.
  2. Show the statuses (status) of the orders, each one once.
  3. Work out what 3 items at 55 baht come to, without a table.
Show the answer
SELECT name, price FROM product;
SELECT DISTINCT status FROM orders;
SELECT 55 * 3;
nameprice
Coffee55.0
Green Tea45.0
Orange Juice40.0
Croissant35.0
Banana Bread60.0
Cheesecake120.0
Potato Chips25.0
Dried Mango90.0
Cashew Nuts150.0
Notebook30.0
Pen15.0
Highlighter25.0
12 rows
status
paid
shipped
cancelled
3 rows
55 * 3
165
1 row

Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made