SQL Introduction
SQL (Structured Query Language, pronounced "S-Q-L" or "sequel") is the standard language for working with relational databases. A relational database stores data in tables — grids of rows and columns — and SQL lets you ask questions of those tables, add new rows, change existing ones and define the tables themselves.
Almost every app you use talks to a database somewhere: online shops store products and orders, banks store accounts and transactions, and your phone keeps contacts and messages in a small SQL database called SQLite.
What can SQL do?
| Task | Example statement |
|---|---|
| Read data | SELECT name, price FROM products |
| Filter and sort | ... WHERE price < 5 ORDER BY price |
| Summarize | SELECT COUNT(*) FROM orders |
| Add rows | INSERT INTO customers ... |
| Change rows | UPDATE products SET price = 9.5 ... |
| Remove rows | DELETE FROM orders ... |
| Define structure | CREATE TABLE, ALTER TABLE, DROP TABLE |
| Combine tables | JOIN, UNION, subqueries |
SQL is declarative: you describe what result you want, and the database engine works out how to fetch it efficiently. You never write loops over rows yourself.
Your first query
Every playground in this course runs against TAD Market, a small online shop database with products, customers, orders and more. Press ▶ Run to execute the query:
SELECT name, price
FROM products
WHERE category_id = 8
ORDER BY price;name | price
-----------------------+------
The SQL Handbook | 29
Python Recipes | 34.5
Learn Rust in Practice | 39
(3 rows)
Read it almost like English: select the name and price from the products table where the category is 8 (Books), ordered by price. The answer comes back as a table.
Tip: Every run starts from a fresh copy of the database. Feel free to experiment — you cannot break anything permanently.
A few more questions
You can ask several questions in one playground. Each statement ends with a semicolon ; and each result is printed separately:
SELECT COUNT(*) AS total_customers FROM customers;
SELECT name, city
FROM customers
WHERE country = 'Japan';total_customers
---------------
25
(1 row)
name | city
--------------+------
Code & Coffee | Tokyo
(1 row)
RDBMS: the software behind SQL
The program that stores the data and runs your SQL is called a Relational Database Management System (RDBMS). Popular ones:
| RDBMS | Typical use |
|---|---|
| SQLite | Embedded in apps, phones and browsers (used on this site) |
| PostgreSQL | Powerful open-source server database |
| MySQL / MariaDB | Very common for websites |
| Microsoft SQL Server | Enterprise systems on Windows/Azure |
| Oracle Database | Large enterprise systems |
All of them understand the core of standard SQL (SELECT, INSERT, JOIN, GROUP BY, …). They differ in details such as data types, date functions and how to limit the number of rows. This course uses SQLite, and whenever another system writes something differently you will see a short comparison.
Changing data
SQL is not only for reading. This playground adds a new product and then reads it back:
INSERT INTO products (name, category_id, supplier_id, unit, price, stock)
VALUES ('Matcha Powder', 1, 1, '100 g tin', 14.9, 20);
SELECT product_id, name, price
FROM products
WHERE name = 'Matcha Powder';1 row affected.
product_id | name | price
-----------+---------------+------
31 | Matcha Powder | 14.9
(1 row)
The INSERT reports how many rows it changed; the SELECT shows the new row, which received the next free product_id automatically.
Test yourself
What does SQL stand for?
- Simple Query Language
- Structured Query Language
- Sequential Query Logic
- Standard Question Language
Which statement reads data from a table?
- GET
- READ
- SELECT
- FETCH
SQL is called *declarative* because…
- you must declare every variable
- you describe the result you want, not the steps to compute it
- it only works with declared tables
- it has no loops or functions at all