Start here
A query says which table, which rows and which columns.
SQL is a language for asking a database questions. A database keeps data in tables. A question written in SQL is called a query.
The database answers with a new, small table. PostgreSQL is a popular free database. We use it in this chapter.
For example: “Which orders are paid, and how much was each one?” Text goes in single quotes, like 'paid'. In SQL, you write it like this:
| order_id | customer_id | status | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | paid | 40.00 | 2026-01-03 |
| 102 | 1 | paid | 25.00 | 2026-01-10 |
| 103 | 2 | refunded | 60.00 | 2026-01-11 |
| 104 | 3 | paid | 15.50 | 2026-01-15 |
| 105 | 3 | pending | NULL | 2026-01-20 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
SELECT order_id, amount
FROM orders
WHERE status = 'paid';| order_id | amount |
|---|---|
| 101 | 40.00 |
| 102 | 25.00 |
| 104 | 15.50 |
| 106 | 80.00 |
The shop we use in every example
All examples use one small online shop with three tables.
Here is all the data. It is small on purpose, so you can check every answer with your eyes.
| customer_id | name | country |
|---|---|---|
| 1 | Asha | IN |
| 2 | Ben | GB |
| 3 | Carla | US |
| 4 | Dev | NULL |
| 5 | Omar | IN |
| order_id | customer_id | status | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | paid | 40.00 | 2026-01-03 |
| 102 | 1 | paid | 25.00 | 2026-01-10 |
| 103 | 2 | refunded | 60.00 | 2026-01-11 |
| 104 | 3 | paid | 15.50 | 2026-01-15 |
| 105 | 3 | pending | NULL | 2026-01-20 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
| order_id | product | quantity |
|---|---|---|
| 101 | Pen | 2 |
| 101 | Notebook | 1 |
| 102 | Bag | 1 |
| 103 | Lamp | 1 |
| 104 | Pen | 3 |
| 106 | Chair | 1 |
| 106 | Lamp | 1 |
country is a two-letter code: IN is India, GB is Great Britain, US is the United States.
Look at customer_id and order_id. These columns connect the tables:
One customer can have many orders. One order can have many products.
orders.customer_id says who placed the order. order_items.order_id says which order the product belongs to.
NULL means a value is missing, so SQL does not know it. Dev has no country. Order 105 has no amount yet.
Omar is a customer, but Omar has not ordered anything yet. You need Omar in chapter 2.
What you will learn
- Read a table: rows, columns and what one row means.
- Write a query: choose columns, keep rows, sort, and cut the list.
- Handle missing values (NULL) without losing rows.
- Work with numbers, text and dates.
- Count rows correctly.
Why this matters in interviews
Most data engineering interviews start with a SQL test. The first questions check these basics. People often fail them on small things, like NULL or a wrong count.
The examples use PostgreSQL. Most databases work the same way. When one is different, we tell you.
Type every query yourself in the SQL workspace. Before you run it, guess the result. Then compare.
