Programming and IT

How to learn SQL from scratch

SQL is the query language of relational databases. Developers, analysts, testers and managers who want to pull numbers themselves all need it. Basic queries take a few weeks to learn; working confidently with complex reports takes two or three months of practice. Below is the order of topics and ways to train on a real database.

Updated
In this article

Where to practise#

Learning SQL from pictures is pointless — you need a database where you can run queries and make mistakes. Three options, from least to most effort:

  • an online sandbox or trainer with ready tables and answer checking;
  • SQLite — a database in a single file, nothing to configure;
  • PostgreSQL on your own computer — the closest to real working conditions.

For PostgreSQL, take a sample database with a dozen related tables (orders, customers, products) — demo databases like that are easy to find. As a client, use psql in the terminal or any graphical client.

Weeks 1–2: selecting and filtering#

Start by reading data — read queries cannot break anything:

SELECT name, city
FROM customers
WHERE city = 'Boston' AND created_at >= '2025-01-01'
ORDER BY name
LIMIT 10;

Work through comparison operators, IN, BETWEEN, LIKE, IS NULL. The main rule about NULL: it means "unknown", so NULL = NULL is not true, and to test for it you need IS NULL.

Weeks 3–4: aggregates and grouping#

COUNT, SUM, AVG, MIN, MAX, then GROUP BY and HAVING. Remember the logical order in which a query runs: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY. That explains the difference between WHERE (filters rows before grouping) and HAVING (filters groups after it). Note that COUNT(*) counts all rows while COUNT(column) skips NULLs.

Weeks 5–6: joining tables#

INNER JOIN, LEFT JOIN, less often RIGHT and FULL. Draw two small tables of three rows each on paper and write out the result of every kind of join by hand — after that the confusion goes away. A typical mistake is a join without a complete condition: rows multiply and the total comes out several times larger than it really is.

Weeks 7–8: subqueries, CTEs, window functions#

Subqueries in WHERE and FROM, common table expressions with WITH for readability, then window functions — ROW_NUMBER, RANK, LAG, a running total with SUM(...) OVER (ORDER BY ...). This is the topic data analyst interviews test. The article on SQL queries with examples collects ready query patterns for each of these.

Next: changing data and designing schemas#

INSERT, UPDATE, DELETE, transactions (BEGIN, COMMIT, ROLLBACK), primary and foreign keys, normalisation, indexes and reading a query plan with EXPLAIN. Try queries that change data inside a transaction first and roll them back — that way a mistake ruins nothing.

How to check your queries#

  • Before running, write down the result you expect: roughly how many rows and what total. A big difference is a reason to look for a bug.
  • After a JOIN, compare count(*) with the number of rows in the source table.
  • Solve one task two ways — with a subquery and with a JOIN — and compare the answers.
  • Keep your queries in a Git repository with a comment saying which question each one answers. It is both your notes and your portfolio.

A practice project: design a database for a personal library or budget, fill it with data and write ten reports — from a simple select to a ranking with a window function.

Step-by-step plan

  1. Weeks 1–2 — SELECT and WHERESelecting, filtering, sorting and handling NULL on a practice database.
  2. Weeks 3–4 — aggregatesCOUNT, SUM, AVG, GROUP BY, HAVING and the order a query runs in.
  3. Weeks 5–6 — JOINInner and outer joins, checking the row count after a join.
  4. Weeks 7–8 — subqueries and windowsWITH, ROW_NUMBER, RANK, LAG, running totals.
  5. Weeks 9–10 — your own databaseTables with keys, transactions, indexes and ten reports in a repository.

Start learning this in your own space

The plan goes into your repository: tick off stages, keep notes — the change history shows how far you have come.

Start the plan

Check yourself

1.The orders table has three rows with amount 100, 200 and 300. What does SELECT SUM(amount) FROM orders return?

2.The users table has 4 rows, and one of them has email set to NULL. What does SELECT COUNT(email) FROM users return?

3.Which clause filters rows before grouping?

Sources

Was this helpful?

More articles

Programming and IT How to learn Python from scratch Python is a good first programming language: code reads almost like text, and the standard library covers most everyday tasks. This plan takes you from installing the interpreter to your own scripts covered by tests in about four months, at roughly an hour a day. Programming and IT How to learn Linux from scratch Linux runs most servers, containers and countless devices, so developers, testers, analysts and system administrators all need the command line. The easiest way to learn it is not by reading lists of commands but by working in the terminal every day and solving small practical tasks. Below is a sequence of topics for two to three months. Programming and IT How to learn JavaScript from scratch JavaScript runs in every browser and, through Node.js, on the server too. It is easy to start — you can run code right in the browser console — but it has plenty of surprising corners, from type coercion to asynchronous code. The plan below takes three to four months and assumes you already know basic HTML and CSS or are learning them alongside. Programming and IT How to learn Java from scratch Java is a strictly typed language behind banking systems, the servers of large services and Android apps. The strictness slows you down at first, but the compiler catches many mistakes before the program ever runs. This plan takes about six months at an hour a day and leads from your first program to a small backend application. Programming and IT How to learn machine learning from scratch Machine learning means building models that find patterns in data and make predictions from them. Starting with neural networks is tempting, but it is safer to go from the bottom up — from Python and math to classic algorithms, honest evaluation and only then deep learning. Without a technical background, expect six months to a year of regular study. Programming and IT C++ from scratch C++ is a compiled language used wherever speed and direct access to memory matter — game engines, browsers, databases, firmware. It is harder to get into than Python, but you can build your first working program on the very first evening.

More solutions