1. What a database and SQL are

Before the first statement: databases, tables, and the practice shop used all through this guide.

What a database is

Think of a small shop. It has to remember what it sells, at what price, how many are left, who its customers are, and who ordered what and when. In a notebook or a spreadsheet that soon gets hard to search, easy to get wrong, and impossible for several people to change at once.

A database keeps data in order, so that programs can find, add, change and delete it quickly and correctly, even when there are millions of rows.

The most common databases keep their data in tables, like sheets in a spreadsheet:

What SQL is

SQL (say "S-Q-L" or "sequel") is the language for talking to a database. You tell the database what you want, and it works out how to find it.

SQL reads almost like English: "select the name and the price from the products":

SELECT name, price FROM product;
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

SQL is a standard that has been in use for more than 50 years. Learn it once and it works with almost every database, and it will be with us for a long time yet.

The statements in this guide are written in SQLite's SQL, which is what Phai SQL, Phai's own database, speaks. Most of it is the same in other databases; where they differ, the chapter says so.

The practice shop

Every chapter uses the data of one small shop. It has five tables:

TableWhat it holdsColumns
categorykinds of productid, name
productthe productsid, name, category_id, price, stock
customerthe customersid, name, city, phone, joined (the day they joined)
ordersordersid, customer_id, ordered (the day), status
order_itemwhat is in each orderorder_id, product_id, qty, price (the price it sold at)

Here is the whole category table:

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

The table under a statement is the real result. When this page was made, every statement in it was run on Phai SQL and checked against SQLite to give the same answer. Run it yourself and you will see exactly the same.

product has a category_id that says which category a product is in: Coffee has category_id 1, the Drinks category. Linking tables by their id numbers like this is the heart of a database; the JOIN chapters use it a lot.

SELECT * FROM product;
idnamecategory_idpricestock
1Coffee155.0120
2Green Tea145.080
3Orange Juice140.00
4Croissant235.030
5Banana Bread260.012
6Cheesecake2120.08
7Potato Chips325.0200
8Dried Mango390.045
9Cashew Nuts3150.020
10Notebook430.075
11Pen415.0300
12Highlighter425.00
12 rows

Where to run the statements

The whole shop is in practice.sql: run it in an empty database and you have the shop of this guide.

How to read this guide

Run SELECT * FROM customer; to see how many customers the shop has and where they live, then look at the orders table to see which statuses an order can have.

Show the answer
SELECT * FROM customer;
SELECT * FROM orders;
idnamecityphonejoined
1Somchai JaideeBangkok081-234-56782025-01-15
2Malee SrisukChiang Mai089-111-22222025-03-02
3Anan WongsaBangkokNULL2025-04-20
4Pim ChaiyaPhuket086-555-01012025-06-11
5Narin ThongdeeKhon Kaen082-777-88882025-08-30
6Kanya BoonmeeChiang Mai084-246-13572025-10-05
7Wichai SaelimBangkok083-999-00002026-01-09
8Siriporn KaewdeePhuketNULL2026-02-14
9Tawan RungruangKhon Kaen085-123-45672026-05-21
10Lalita MeesukBangkok087-654-32102026-08-03
10 rows
idcustomer_idorderedstatus
112026-09-02paid
222026-09-05shipped
312026-09-12shipped
432026-09-20paid
542026-10-01cancelled
652026-10-03shipped
722026-10-10paid
862026-10-15shipped
972026-10-22paid
1012026-10-28paid
1182026-11-02shipped
1242026-11-05paid
1362026-11-11paid
1432026-11-18cancelled
1572026-11-25paid
15 rows

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