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;| name | city |
|---|---|
| Somchai Jaidee | Bangkok |
| Malee Srisuk | Chiang Mai |
| Anan Wongsa | Bangkok |
| Pim Chaiya | Phuket |
| Narin Thongdee | Khon Kaen |
| Kanya Boonmee | Chiang Mai |
| Wichai Saelim | Bangkok |
| Siriporn Kaewdee | Phuket |
| Tawan Rungruang | Khon Kaen |
| Lalita Meesuk | Bangkok |
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;| city | name |
|---|---|
| Bangkok | Somchai Jaidee |
| Chiang Mai | Malee Srisuk |
| Bangkok | Anan Wongsa |
| Phuket | Pim Chaiya |
| Khon Kaen | Narin Thongdee |
| Chiang Mai | Kanya Boonmee |
| Bangkok | Wichai Saelim |
| Phuket | Siriporn Kaewdee |
| Khon Kaen | Tawan Rungruang |
| Bangkok | Lalita Meesuk |
Every column with *
* means "every column", in the table's own order:
SELECT * FROM category;| id | name |
|---|---|
| 1 | Drinks |
| 2 | Bakery |
| 3 | Snacks |
| 4 | Stationery |
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 |
SELECT DISTINCT city FROM customer;| city |
|---|
| Bangkok |
| Chiang Mai |
| Phuket |
| Khon Kaen |
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_id | status |
|---|---|
| 1 | paid |
| 2 | shipped |
| 1 | shipped |
| 3 | paid |
| 4 | cancelled |
| 5 | shipped |
| 2 | paid |
| 6 | shipped |
| 7 | paid |
| 8 | shipped |
| 4 | paid |
| 6 | paid |
| 3 | cancelled |
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 / 4 | 10 / 4.0 |
|---|---|---|---|
| 42 | hello | 2 | 2.5 |
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
- SQL's words don't care about capitals:
select,SELECTandSelectare the same word. The custom is SQL's words in capitals and the names of tables and columns in small letters, so they stand apart. - Break lines and add spaces as you like; the database ignores them. Long statements read better one part to a line.
- End a statement with
;. Several statements one after another must be separated by;. - Text after
--to the end of the line is a comment: the database skips it; it is there for people.
-- Every customer: the name and the phone
SELECT
name,
phone -- some have none (shown as NULL)
FROM customer;| name | phone |
|---|---|
| Somchai Jaidee | 081-234-5678 |
| Malee Srisuk | 089-111-2222 |
| Anan Wongsa | NULL |
| Pim Chaiya | 086-555-0101 |
| Narin Thongdee | 082-777-8888 |
| Kanya Boonmee | 084-246-1357 |
| Wichai Saelim | 083-999-0000 |
| Siriporn Kaewdee | NULL |
| Tawan Rungruang | 085-123-4567 |
| Lalita Meesuk | 087-654-3210 |
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;SELECT name FROM customers;Read the error: it says exactly which name is wrong. Here the table is customer, without an s.
- Show the name (
name) and the price (price) of every product. - Show the statuses (
status) of the orders, each one once. - 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;| name | price |
|---|---|
| Coffee | 55.0 |
| Green Tea | 45.0 |
| Orange Juice | 40.0 |
| Croissant | 35.0 |
| Banana Bread | 60.0 |
| Cheesecake | 120.0 |
| Potato Chips | 25.0 |
| Dried Mango | 90.0 |
| Cashew Nuts | 150.0 |
| Notebook | 30.0 |
| Pen | 15.0 |
| Highlighter | 25.0 |
| status |
|---|
| paid |
| shipped |
| cancelled |
| 55 * 3 |
|---|
| 165 |
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