Lesson 1 of 24 in SQL for Beginners · 12m
What a relational database is
Every time you check an online order, look at a bank statement or search a product catalogue, a program is asking a database a question. Very often that database is relational, and the question is written in SQL (Structured Query Language). SQL has been the standard language for relational databases since the 1980s, and it is still one of the most useful technical skills you can learn, whether you are a developer, an analyst, a product manager, a marketer or a founder who wants to answer their own questions without waiting for someone else.
This course teaches SQL from the very beginning. You do not need any programming experience. Every lesson uses the same small database for an imaginary online shop, so you can see exactly what each query returns and why.
From spreadsheets to databases
Imagine running a small online shop from a spreadsheet. You might start with one sheet of orders, with a row per order and columns for the customer's name, email, address, the products bought and the total. It works for the first fifty orders. Then problems appear:
-
Repetition. A loyal customer's name and email are typed again on every order. When they change their email, you have to find and fix every copy, and you will miss some.
-
Inconsistency. One row says "London", another says "london " with a trailing space, a third says "Londn". Nothing stops you.
-
Awkward questions. "Which customers have not ordered since March?" or "What is our best-selling product in each category?" become long sessions of filtering, copying and pivoting.
-
Sharing. Two people editing the same file at once overwrite each other's changes.
A relational database fixes these problems by splitting data into several tables, each about one kind of thing, and connecting them with shared values called keys. Customers live in one table, products in another, orders in a third. An order does not repeat the customer's name and email; it stores the customer's ID, and the database can look up the rest whenever you ask. The database system also enforces rules you set (every order must belong to a real customer, a price cannot be negative), and it lets many people and programs read and write at the same time without corrupting the data.
The relational model in one paragraph
In 1970 Edgar F. Codd, a researcher at IBM, published a paper proposing that data should be organised as relations: what we now call tables. Each table has a fixed set of named columns, and holds any number of rows. Each row describes one thing (one customer, one order), and each column holds one fact about it (an email, a date). You get information out by describing what you want in terms of those tables, not by writing step-by-step instructions for walking through files. That idea became the foundation of almost every business database built since.
A short history of SQL
- 1970: The relational model Edgar F. Codd publishes "A Relational Model of Data for Large Shared Data Banks" while working at IBM.
- 1974: SEQUEL Donald Chamberlin and Raymond Boyce at IBM describe SEQUEL, a query language for relational data. It is later renamed SQL.
- 1979: First commercial SQL databases Relational Software, Inc. (later Oracle Corporation) releases Oracle V2, one of the first commercially available SQL databases.
- 1986–1987: SQL becomes a standard ANSI publishes the first SQL standard in 1986, and ISO adopts it in 1987.
- 1992: SQL-92 A major revision of the standard. It adds the explicit JOIN ... ON syntax used throughout this course.
- 1995–2000: Open-source databases arrive MySQL is first released in 1995, Postgres95 is renamed PostgreSQL in 1996, and SQLite appears in 2000.
- 2003: Window functions SQL:2003 standardises window functions such as ROW_NUMBER, which you will use later in this course.
- 2023: SQL:2023 The latest edition of the standard adds a JSON data type and property graph queries. SQL keeps growing, but the core you learn here has been stable for decades.
SQL is declarative
Most programming languages are imperative: you tell the computer each step. "Open the file, read a line, check the city, if it is London add the name to a list, move to the next line…"
SQL is declarative: you describe the result you want and let the database work out how to produce it. "Give me the first and last names of customers whose city is London."
Behind the scenes, the database system parses your statement, checks that the tables and columns exist, and then its query planner chooses a strategy: which table to read first, whether an index can help, how to match rows from different tables. Two databases can run the same query in completely different ways and still return the same answer. That is why SQL you learn on one system mostly works on another, and why the last section of this course shows you how to peek at the plan the database chose.
- You send a statement Your tool (a terminal client such as psql, a desktop app, or your application code) sends the SQL text to the database server.
- Parse and check The database checks the grammar and confirms that every table and column you named exists and that you are allowed to read it. Typos fail here with an error message.
- Plan The query planner considers different ways to get the answer and estimates the cost of each, then picks the cheapest one it can find.
- Execute The executor reads rows from tables or indexes, filters, joins, sorts and groups them according to the plan.
- Return the result The result comes back as a table: named columns and zero or more rows. Every SELECT, however complex, returns a table.
Database systems you will meet
A database management system (DBMS) is the software that stores the data and runs your SQL. The most common relational ones are:
-
PostgreSQL: open source, very standards-minded and feature-rich. This course uses it for every example.
-
MySQL and its fork MariaDB: open source and extremely widely used, especially behind websites.
-
SQLite: a small library that stores a whole database in a single file. It runs inside phones, browsers and countless apps.
-
Microsoft SQL Server and Oracle Database: commercial systems common in large organisations.
They all speak SQL, and the core of the language (SELECT, WHERE, JOIN, GROUP BY, INSERT, UPDATE, DELETE, CREATE TABLE) works the same way in each. They differ in details: how you limit the number of rows, which date functions exist, how text is compared. Whenever those differences matter, this course points them out.
Your first query
Here is the products table from the sample shop. The query reads every column (* means "all columns") from every row of products:
SELECT * FROM products;
| product_id | name | category | price |
|---|---|---|---|
| 1 | Notebook | Stationery | 4.50 |
| 2 | Pen set | Stationery | 12.00 |
| 3 | Desk lamp | Home | 35.00 |
| 4 | Coffee mug | Home | 9.50 |
| 5 | Headphones | Electronics | 79.00 |
| 6 | USB-C cable | Electronics | 15.00 |
| 7 | Phone stand | Electronics | 22.00 |
Now a real question: which customers live in London? You name the columns you want, the table they come from, and a condition:
SELECT first_name, last_name
FROM customers
WHERE city = 'London';
| first_name | last_name |
|---|---|
| Ben | Carter |
| David | Kim |
Read it aloud and it is almost English: select the first name and last name from customers where the city is London. Keywords such as SELECT and FROM are not case-sensitive, but writing them in capitals is a common convention that makes queries easier to scan. Text values go in single quotes. The semicolon marks the end of a statement; most tools need it when you run several statements at once.
Key takeaways
- A relational database stores data in tables of rows and columns, connected by key values instead of repeated copies.
- The database system enforces rules, keeps data consistent and lets many users work at once, which spreadsheets struggle with.
- SQL is declarative: you describe the result and the query planner decides how to produce it.
- PostgreSQL, MySQL, SQLite, SQL Server and Oracle share the same core SQL and differ in details.
- Every SELECT returns a table, and rows have no guaranteed order unless you sort them.
Sources
- PostgreSQL documentation: Tutorial, Concepts
- SQL (Wikipedia)
- Relational model (Wikipedia)
- Codd, E. F. (1970). A Relational Model of Data for Large Shared Data Banks. Communications of the ACM 13(6).
This lesson also has interactive exercises. Open the course to try them and track your progress.