SQLSQL

Learn SQL – tutorial, cheatsheet & exercises

SQL (Structured Query Language) is the standard language for relational databases. You describe WHICH data you want – not how the database finds it. This declarative way of thinking is the biggest difference from languages like Python or Java: you state a result, and the query planner chooses the route.

SQL is everywhere: backends (PostgreSQL, MySQL, SQLite), analytics (BigQuery, Snowflake), mobile apps (SQLite is inside every smartphone), and reporting. The basics are the same in all dialects; differences exist in data types, functions, auto-increment, and special features. Throughout this course we use a small shop schema (kunden, produkte, bestellungen, bestellpositionen) and point out deviations for PostgreSQL, MySQL, and SQLite.

DatabasesReportingData analyticsBackends

Hello World in SQL

SELECT 'Hello World' AS gruss;

SQL has no print: output is result rows. A SELECT without FROM returns one row with the given expressions (not in Oracle, which needs FROM dual).

How to get started with SQL

The easiest way to get started is SQLite: no server to install, the whole database is a single file. On macOS, sqlite3 is preinstalled; on Linux use sudo apt install sqlite3; on Windows use winget install SQLite.SQLite. If you prefer clicking, use DB Browser for SQLite (free, sqlitebrowser.org) or the SQL extensions for VS Code.

sqlite3 shop.db          # create or open the database file
.read schema.sql        # run an SQL file
.headers on
.mode column            # nicely formatted table output
.tables                 # all tables
.schema kunden          # show the CREATE statement
.quit

For server features (real user permissions, JSON, ILIKE, RETURNING, window functions in full) PostgreSQL is worth it. The quickest way to start it is with Docker – without installing anything on your machine. With no setup at all, you can use online playgrounds such as sqliteonline.com, db-fiddle.com, or dbfiddle.uk (dialect selectable).

docker run --name pg -e POSTGRES_PASSWORD=geheim -p 5432:5432 -d postgres:16
docker exec -it pg psql -U postgres     # open the SQL console
# In psql:  \dt (tables)  \d kunden (structure)  \q (quit)

All examples in the lessons run on this sample schema. Copy it into a file schema.sql and load it (SQLite syntax; in PostgreSQL, replace INTEGER PRIMARY KEY with INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, or keep inserting the ids explicitly – that works there too):

CREATE TABLE kunden (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  stadt TEXT,
  registriert TEXT NOT NULL          -- ISO date 'YYYY-MM-DD'
);
CREATE TABLE produkte (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  kategorie TEXT NOT NULL,
  preis REAL NOT NULL CHECK (preis >= 0)
);
CREATE TABLE bestellungen (
  id INTEGER PRIMARY KEY,
  kunde_id INTEGER NOT NULL REFERENCES kunden(id),
  datum TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'offen'
);
CREATE TABLE bestellpositionen (
  id INTEGER PRIMARY KEY,
  bestellung_id INTEGER NOT NULL REFERENCES bestellungen(id),
  produkt_id INTEGER NOT NULL REFERENCES produkte(id),
  menge INTEGER NOT NULL CHECK (menge > 0)
);
INSERT INTO kunden VALUES
 (1,'Ada','Berlin','2023-01-15'), (2,'Ben','Hamburg','2023-03-02'),
 (3,'Cem','Berlin','2023-06-20'), (4,'Dana','München','2024-01-10'),
 (5,'Eva',NULL,'2024-02-05');
INSERT INTO produkte VALUES
 (1,'Laptop','Elektronik',999.00), (2,'Maus','Elektronik',25.00),
 (3,'Buch SQL','Bücher',30.00), (4,'Kaffee','Lebensmittel',8.50),
 (5,'Stuhl','Möbel',150.00);
INSERT INTO bestellungen VALUES
 (1,1,'2024-01-05','bezahlt'), (2,1,'2024-02-10','bezahlt'),
 (3,2,'2024-02-11','offen'),   (4,3,'2024-03-01','storniert'),
 (5,1,'2024-03-15','offen'),   (6,4,'2024-03-20','bezahlt');
INSERT INTO bestellpositionen VALUES
 (1,1,1,1),(2,1,2,2),(3,2,3,1),(4,3,4,10),(5,4,5,2),(6,5,2,1),(7,5,4,4),(8,6,1,1),(9,6,3,3);

SQL learning path

Beginner
Intermediate
Advanced

At a glance

First released1974
CreatorChamberlin & Boyce (IBM)
ParadigmDeclarative
TypingStatic (column types)
ExecutionExecuted by database engines
File extension.sql

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

SQL compared

Hello World: SQL comparedVariables: SQL comparedConditionals: SQL comparedLoops: SQL comparedFunctions: SQL comparedLists and Arrays: SQL comparedDictionaries and Maps: SQL comparedClasses and Objects: SQL comparedError Handling: SQL comparedReading Files: SQL compared