Sub topic 10
Reading and writing data with SQL
This chapter assumes you understand tables, rows, columns, primary keys, and foreign keys. You should also be comfortable reading short code snippets and thinking of a table like a spreadsheet with stricter rules.
SQL, or Structured Query Language, is the language used to work with relational databases. You use SQL to define tables, add rows, change rows, remove rows, and read data.
The main SQL operations
SQL statements usually describe the result you want rather than the steps for finding it. The database management system chooses how to execute the statement.
| Operation | SQL statement | Purpose |
|---|---|---|
| Create structure | CREATE TABLE | Defines a table and its rules |
| Add data | INSERT | Adds one or more rows |
| Read data | SELECT | Returns matching rows |
| Change data | UPDATE | Modifies existing rows |
| Remove data | DELETE | Removes rows |
A WHERE clause limits which rows an operation affects. Without a WHERE clause, UPDATE or DELETE can affect every row in the table.
Create tables and insert rows
The following example can run in SQLite. It creates customers and orders, then inserts related rows.
PRAGMA foreign_keys = ON;
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
order_date TEXT NOT NULL,
total_amount REAL NOT NULL CHECK (total_amount >= 0)
);
INSERT INTO customers (customer_id, name, email)
VALUES
(1, 'Ada Lovelace', '[email protected]'),
(2, 'Grace Hopper', '[email protected]');
INSERT INTO orders (order_id, customer_id, order_date, total_amount)
VALUES
(100, 1, '2026-01-15', 59.90),
(101, 1, '2026-02-03', 24.50),
(102, 2, '2026-02-08', 80.00);The column list after INSERT names the destination columns. Naming columns explicitly makes the statement safer when the table structure changes.
Select and filter rows
A SELECT statement names the columns to return and the table to read. WHERE keeps only rows that satisfy a condition. ORDER BY sorts the result.
SELECT order_id, order_date, total_amount
FROM orders
WHERE total_amount >= 50
ORDER BY order_date DESC;To test for missing data, use IS NULL or IS NOT NULL. Do not compare with = NULL, because NULL means an unknown or absent value rather than an ordinary value.
You can change or remove selected rows as follows:
UPDATE orders
SET total_amount = 25.00
WHERE order_id = 101;
DELETE FROM orders
WHERE order_id = 101;Always inspect the WHERE condition before running UPDATE or DELETE. A missing or incorrect condition can change far more data than intended.
Join related tables
A JOIN combines rows from different tables. This query matches each order's customerid with the customers table's customerid.
SELECT
c.name,
o.order_id,
o.order_date,
o.total_amount
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.customer_id
ORDER BY o.order_date;The aliases c and o are short names for the two tables. An inner JOIN, written as JOIN, returns only rows with a match in both tables. A LEFT JOIN keeps every row from the table on the left, even when no matching row exists.
A reliable query workflow
- Start with SELECT and confirm which rows match.
- Add or adjust the WHERE condition.
- Add ORDER BY if result order matters.
- For updates or deletes, use the tested condition.
- Check the affected-row count after the change.
Exercise
Which rows does the join query return for order 100?
Show answer
It returns Ada Lovelace, order 100, date 2026-01-15, and total 59.90 because order 100 has customerid 1, which matches Ada's customerid.