Part 01 / Stage 1
Getting started
Goal: Understand what a relational database is, install PostgreSQL 17, learn your way around psql, and load shopdb — the store database every lesson in this course runs against. Time: ~1.5 hours
What you will learn
- What a database is, and why apps don't just use files
- What "relational" and "SQL" mean, and where PostgreSQL fits
- How a PostgreSQL server is organised: a cluster holds databases, databases hold schemas, and schemas hold tables
- How to install PostgreSQL 17 on Windows, macOS and Linux
- How to connect with psql and read its prompt
- Your first queries, and the anatomy of a SQL statement
- GUI clients: pgAdmin and DBeaver
- Loading shopdb, the course's practice database
- The psql survival kit:
\l,\c,\dt,\d,\xand friends
Topics in this part
Practice
SQL workbench — write and run queries here
Press Run and PostgreSQL loads right in your browser (a one-time download of about 16 MB; after that it works offline). It comes seeded with shopdb, and statements share one session per page — like a psql window. Reset data rebuilds shopdb. Your queries never leave this page. Lessons were captured on PostgreSQL 17; the in-browser server is PostgreSQL 19 (via PGlite), so the rare output detail can differ.
Try each one yourself first, in psql connected to shopdb. Then check the solution.
Exercise 1: Checkout math
Scenario: A customer is buying 2 × Laptop Air 13 at 950.00 and 3 × USB-C Cable at 15.00.
What to do: In one SELECT (no table needed), show the laptop cost, the cable cost and the grand total — each with a readable column name.
Sample output:
laptops | cables | total
---------+--------+-------
1900 | 45 | 1945
(1 row)
💡 Hint: three expressions separated by commas, each with AS.
Show the solution to Exercise 1
-- Exercise 1: Checkout math
SELECT 2 * 950.00 AS laptops,
3 * 15.00 AS cables,
2 * 950.00 + 3 * 15.00 AS total;
Exercise 2: Fix the broken statement
Scenario: A friend sent you this and swears "SQL is broken":
SELCT "good morning' AS greeting
What to do: Fix all three problems so it returns one row. Run your friend's version first and read where the ^ points.
Sample output:
greeting
--------------
good morning
(1 row)
💡 Hint: a typo, the wrong kind of quotes, and a missing ender.
Show the solution to Exercise 2
-- Exercise 2: Fix the broken statement
-- The three problems: SELCT is a typo for SELECT, text needs single
-- quotes (double quotes name columns), and the statement needs a ;
SELECT 'good morning' AS greeting;
Exercise 3: The average problem
Scenario: Your store sold 7 items across 2 orders, and you want items per order.
What to do: Show 7 / 2 and the real average, side by side with clear names. Then show the remainder too.
Sample output:
truncated | real_average | remainder
-----------+--------------+-----------
3 | 3.5 | 1
(1 row)
💡 Hint: integer division truncates; make one side a decimal. round(..., 1) tidies the decimal — or cast and divide by 2.0.
Show the solution to Exercise 3
-- Exercise 3: The average problem
-- 7 / 2 is integer division. Making one side decimal (2.0) gives the
-- real answer, and round(..., 1) trims the long decimal tail.
SELECT 7 / 2 AS truncated,
round(7 / 2.0, 1) AS real_average,
7 % 2 AS remainder;
Exercise 4: Count everything
Scenario: First day with shopdb — how big is this thing?
What to do: Using one query per table, count the rows in products, orders and order_items. Name each count clearly.
Sample output (first query):
products
----------
10
(1 row)
💡 Hint: SELECT count(*) AS ... FROM ...; — your first real use of FROM.
Show the solution to Exercise 4
-- Exercise 4: Count everything
SELECT count(*) AS products FROM products;
SELECT count(*) AS orders FROM orders;
SELECT count(*) AS order_items FROM order_items;
Exercise 5: Read a table's rules
Scenario: You need to understand the orders table before Part 2.
What to do: Using \d orders, answer in your head: which columns refuse NULL? What is the primary key? Which table does customer_id point at? What values does status allow?
💡 Hint: everything is in the \d output — Nullable column, Indexes, Foreign-key constraints, Check constraints.
Show the solution to Exercise 5
-- Exercise 5: Read a table's rules (run \d orders and compare)
-- From the \d orders output:
-- * NOT NULL columns: order_id, customer_id, order_date, status
-- * Primary key: order_id (the "orders_pkey" index)
-- * customer_id REFERENCES customers(customer_id)
-- * status allows only: pending, shipped, delivered, cancelled
-- (the "orders_status_check" CHECK constraint)
\d orders
Exercise 6: The nine-table tour
Scenario: The classic first-day-on-the-job task: learn the schema.
What to do: Run \d on each of the nine tables and write a one-line note for each, in your own words, describing what one row of it represents.
💡 Hint: there is no wrong answer that fits the columns. The solution file has reference notes to compare against.
Show the solution to Exercise 6
-- Exercise 6: The nine-table tour — reference notes.
-- categories one product category; parent_id makes it a tree (Laptops under Electronics)
-- suppliers one company we buy stock from, with its country
-- products one item we sell: its category, supplier, price and stock on hand
-- customers one registered shopper: name, unique email, city, signup date
-- orders one order a customer placed on a date, with its current status
-- order_items one line of an order: which product, how many, at what price
-- payments one payment against an order: amount, method, exact moment
-- employees one member of staff; manager_id points at another employee
-- events one thing a customer did on the site (view/cart/purchase), with JSON details
\dt
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Forgetting the ; |
Nothing runs; prompt shows -# |
Type ; and press Enter |
| Double quotes for text | ERROR: column "hello" does not exist |
Text takes single quotes: 'hello' |
10 / 100 for percentages |
0 — integer division |
Make one side decimal: 10 / 100.0 |
Typing ; after \ commands |
psql waits or complains | \ commands take no semicolon |
Running createdb inside psql |
ERROR: syntax error |
createdb is a shell command; inside psql use CREATE DATABASE |
Ignoring the error's ^ marker |
Long confused staring | The ^ points at the exact problem spot |
Quick recap
- A database stores data safely for many users at once; tables hold rows of typed columns; primary keys identify rows and foreign keys connect tables.
- SQL is the language; PostgreSQL is the RDBMS that speaks it; psql is your terminal to it.
- One server holds many databases, each database holds schemas (
publicby default), and schemas hold the tables. SELECTasks for values;ASnames them; text takes single quotes; integer division truncates.- shopdb is loaded, and re-running
shopdb.sqlresets it any time. - Keep the psql survival kit within reach:
\q \l \c \dt \d \x \timing \h \? \i.
Checklist
Before Part 2, make sure you can:
- Explain to a friend why apps use a database instead of a file.
- Sketch the hierarchy from memory: the server, the databases inside it, the schemas inside each database, and the tables inside each schema.
- Connect with psql, and read what the prompt is telling you.
- Run a
SELECTwith arithmetic and a named (AS) result. - Load shopdb from the script and list its nine tables.
- Use
\l,\dt,\d,\x,\timingand\iwithout looking them up.
Read more: the psql reference and the official tutorial's first steps.