SQL

The standard language for querying and changing data in relational databases.

SQL (Structured Query Language) is how you read and change data in relational databases such as PostgreSQL, MySQL and SQLite. Almost every application stores its data this way, so SQL is useful whichever other language you work in.

  • Beginner
  • 12 lessons
  • 15 exercises
  • Runs in your browser
Not started yet0 of 15 exercises solved
Start lesson 1

Lessons

  1. Tables & SELECTA relational database stores data in tables made of rows and columns. SELECT is how you read from them, and it is the statement you will use most.2 exercises
  2. CREATE, INSERT, UPDATE, DELETEThis lesson covers creating tables and changing the data in them. Together with SELECT, these statements make up CRUD: Create, Read, Update, Delete.1 exercise
  3. ORDER BY, LIMIT & DISTINCTRows in a table have no guaranteed order. ORDER BY sorts your results, LIMIT keeps only the first few, and DISTINCT removes duplicates.2 exercises
  4. COUNT, SUM & GROUP BYAggregate functions turn many rows into a single answer, such as a count or an average. GROUP BY gives you that answer for each group, for example per age or per country.2 exercises
  5. JOINs — Combining TablesReal databases split data across several tables: students in one, courses in another, and enrollments linking them. JOIN combines related rows from those tables in one query.2 exercises
  6. Primary Keys, Foreign Keys & IndexesKeys keep your data consistent, and indexes keep queries fast as tables grow. A slow query is very often a missing index.1 exercise
  7. Transactions & SafetyTransactions make several statements succeed or fail together. For example, moving $100 between two accounts should never leave the money taken from one but not added to the other.1 exercise
  8. Subqueries & CTEsWhen one query needs the answer to another query, you have two options: nest it as a subquery, or name it up front as a CTE. CTEs read top to bottom, which makes longer queries easier to follow.2 exercises
  9. Window FunctionsGROUP BY collapses rows. A window function computes across a set of rows while keeping every row — which is how you get running totals, rankings, and "compare this row to the previous one" in plain SQL.2 exercises
  10. Schema Design & ConstraintsConstraints are rules the database enforces on every write, whatever program makes it. They keep bad data out even when application code has bugs or someone edits the table by hand.
  11. Query Performance & EXPLAINA query that is fast on a small test database can be slow on real data. EXPLAIN shows how the database plans to run a query, so you can see why it is slow and fix it.
  12. SQL CheatsheetQueries, joins, aggregates, and DDL on one page.

Install it on your own computer

The easiest way to start is SQLite: a complete SQL database stored in a single file, with no server to set up. It comes with Python and most operating systems. You do not need this to follow the lessons: the examples run in your browser.

Bash
# SQLite comes with Python. Or download the CLI:
# https://www.sqlite.org/download.html (sqlite-tools zip)

# Then open a database (creates the file if missing):
sqlite3 mydata.db

# Or install PostgreSQL for the full experience:
winget install PostgreSQL.PostgreSQL

What people build with SQL

Learn the basics first, then pick up these tools when a project needs them.

Practical uses

  • Query operational and analytical data
  • Design reliable application storage
  • Build reports and business dashboards
  • Transform data in warehouses and pipelines

Projects to try

  1. Library database
  2. Sales KPI report
  3. Event analytics schema
  4. Search and recommendation dataset

Common tools and packages

PostgreSQLSQLiteMySQLDuckDBDBeaverDataGripdbtPrisma

Well-known software

WordPressGitLabMetabaseSupabaseAirflow metadata DBMost business applications