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:
- A table holds one kind of thing: products, customers.
- A column is one piece of information: a name, a price, how many are in stock.
- A row is one item: one product, one customer.
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;| 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 |
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:
| Table | What it holds | Columns |
|---|---|---|
category | kinds of product | id, name |
product | the products | id, name, category_id, price, stock |
customer | the customers | id, name, city, phone, joined (the day they joined) |
orders | orders | id, customer_id, ordered (the day), status |
order_item | what is in each order | order_id, product_id, qty, price (the price it sold at) |
Here is the whole category table:
SELECT * FROM category;| id | name |
|---|---|
| 1 | Drinks |
| 2 | Bakery |
| 3 | Snacks |
| 4 | Stationery |
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;| id | name | category_id | price | stock |
|---|---|---|---|---|
| 1 | Coffee | 1 | 55.0 | 120 |
| 2 | Green Tea | 1 | 45.0 | 80 |
| 3 | Orange Juice | 1 | 40.0 | 0 |
| 4 | Croissant | 2 | 35.0 | 30 |
| 5 | Banana Bread | 2 | 60.0 | 12 |
| 6 | Cheesecake | 2 | 120.0 | 8 |
| 7 | Potato Chips | 3 | 25.0 | 200 |
| 8 | Dried Mango | 3 | 90.0 | 45 |
| 9 | Cashew Nuts | 3 | 150.0 | 20 |
| 10 | Notebook | 4 | 30.0 | 75 |
| 11 | Pen | 4 | 15.0 | 300 |
| 12 | Highlighter | 4 | 25.0 | 0 |
Where to run the statements
- Phai Database Studio: open Help > SQL Lessons (or press F1). It has this same practice shop, ready to type into and run, with a button that checks your answer.
- Phai SQL in a Rust program:
Database::queryandDatabase::execute(see Phai SQL's README). - Any other program that opens SQLite files: a Phai SQL file is a SQLite file, byte for byte.
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
- Read the chapters in order; each builds on the ones before. Skip what you already know.
- Type every statement yourself instead of copying it: your hands learn faster than your eyes.
- Change the examples and see what changes in the result. Mistakes are fine: the error message tells you where.
- Each chapter ends with exercises, Try it. Do them yourself first, then open the answer.
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;| id | name | city | phone | joined |
|---|---|---|---|---|
| 1 | Somchai Jaidee | Bangkok | 081-234-5678 | 2025-01-15 |
| 2 | Malee Srisuk | Chiang Mai | 089-111-2222 | 2025-03-02 |
| 3 | Anan Wongsa | Bangkok | NULL | 2025-04-20 |
| 4 | Pim Chaiya | Phuket | 086-555-0101 | 2025-06-11 |
| 5 | Narin Thongdee | Khon Kaen | 082-777-8888 | 2025-08-30 |
| 6 | Kanya Boonmee | Chiang Mai | 084-246-1357 | 2025-10-05 |
| 7 | Wichai Saelim | Bangkok | 083-999-0000 | 2026-01-09 |
| 8 | Siriporn Kaewdee | Phuket | NULL | 2026-02-14 |
| 9 | Tawan Rungruang | Khon Kaen | 085-123-4567 | 2026-05-21 |
| 10 | Lalita Meesuk | Bangkok | 087-654-3210 | 2026-08-03 |
| id | customer_id | ordered | status |
|---|---|---|---|
| 1 | 1 | 2026-09-02 | paid |
| 2 | 2 | 2026-09-05 | shipped |
| 3 | 1 | 2026-09-12 | shipped |
| 4 | 3 | 2026-09-20 | paid |
| 5 | 4 | 2026-10-01 | cancelled |
| 6 | 5 | 2026-10-03 | shipped |
| 7 | 2 | 2026-10-10 | paid |
| 8 | 6 | 2026-10-15 | shipped |
| 9 | 7 | 2026-10-22 | paid |
| 10 | 1 | 2026-10-28 | paid |
| 11 | 8 | 2026-11-02 | shipped |
| 12 | 4 | 2026-11-05 | paid |
| 13 | 6 | 2026-11-11 | paid |
| 14 | 3 | 2026-11-18 | cancelled |
| 15 | 7 | 2026-11-25 | paid |
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