How to Learn SQL From Scratch, Step by Step
By Owen Middleton · Updated October 2026 · We ran the examples on PostgreSQL 17
To learn SQL, learn it in this order: queries on one table, then GROUP BY, then joins, then subqueries, then CTEs and window functions. Practice from memory: write each query with no example in front of you. Do the practice on several different days. Check each result.
We make SQLMaxx Learn, the free SQL course that this page links to. We gain when you start with it.
What order should you learn SQL in?
This is how to learn SQL for beginners, in five stages:
- Stage 1, queries on one table: SELECT, WHERE and ORDER BY. To filter rows, start with WHERE, with live examples. That page has live examples.
- Stage 2, aggregates, GROUP BY and HAVING.
- Stage 3, joins, except self-joins: queries on two or more tables. To start, learn INNER JOIN.
- Stage 4, subqueries, then self-joins.
- Stage 5, CTEs and window functions.
We put stages 1 to 4 in the order of difficulty from a study of almost 1,000 students over eight years (Ahadi and others, 2015). The researchers ranked seven types of SQL SELECT query by how hard the students found them to write.
- The students found queries on one table the easiest.
- GROUP BY, with and without HAVING, was next.
- Joins were next. The researchers used natural joins.
- Simple and correlated subqueries were next.
- The students found self-joins harder than subqueries. Self-joins were the most difficult type.
The researchers ranked difficulty. They did not test the best order to learn SQL in. We found no difficulty data for CTEs or window functions, so the place of stage 5 is our own choice.
What is the best way to learn SQL?
Use three habits. The first two habits come from memory research. The third habit is our inference from a study of SQL students.
- Practice from memory. Read the lesson one time. Then write each query with no example in front of you. Why: Dunlosky and others, 2013. Study subjects: Students. The materials included simple concepts, math problems and science texts. Not SQL.
- Space the practice. Practice a topic again on a different day. Do not do all the practice in one session. Why: Cepeda and others, 2006. Study subjects: People in verbal memory tasks, for example paired-associate recall and list recall. Not SQL.
- Check each result. Compare the result with a number that you know, for example a row count. Why: Taipalus and Perälä, 2019. Study subjects: 744 students in a database course. SQL. The study did not test this check.
Dunlosky and others (2013) reviewed 10 study techniques. They rated practice testing and distributed practice "high utility". They rated rereading, highlighting and summarization "low utility". The Association for Psychological Science wrote a news release about the review. It gives flash cards and end-of-chapter questions as examples of practice testing. It describes distributed practice as "spreading out your studying over time".
In Roediger and Karpicke (2006), students read short prose passages about science. After one week, the students who practiced recall remembered 56% of a passage. The students who read it again remembered 42%. After 5 minutes, the result was the opposite: 81% for the students who read it again, and 75% for recall.
Cepeda and others (2006) combined 839 assessments of spaced practice from 317 experiments. The people in the experiments did verbal memory tasks, for example paired-associate recall and list recall. In the section "Educational Implications of Findings", the authors write that study sessions at least 1 day apart are "extremely useful for maximizing long-term retention".
Taipalus and Perälä (2019) studied the final answers of 744 students in a database course. With a syntax error, the database shows an error message. With a logical error, you get a result table with the wrong data. In the final answers, the students left logical errors more often than syntax errors.
With some logical errors, you get the wrong number of rows. With other logical errors, you get the correct number of rows and the wrong values. With a row count, you find only the first kind. The study did not test a check on results. The students saw the correct result table while they wrote each query, and they still left logical errors. Our advice to check each result is an inference from the study.
What is the easiest way to learn SQL from scratch?
Learn SQL online, with a database in your web browser, so that you do not install anything. Learn one SQL dialect first.
In the SQL standard, ISO/IEC 9075, the "Core" features are mandatory for conformance (PostgreSQL 17 documentation). The basic query, GROUP BY, HAVING and the basic join are Core features (PostgreSQL 17 feature list).
Run your first SQL query
The orders table has one row for each order of a small sample store, with the
status of the order. The store has 200 orders.
Schema · ecommerce1 table? = nullable
With this query, you get the pending orders. Press Run. The result has 11 rows. Find the row count under the result.
SELECT id, status, total_amount FROM orders WHERE status = 'pending' ORDER BY id;
| id | status | total_amount |
|---|---|---|
| 19 | pending | 109.99 |
| 43 | pending | 189.00 |
| 61 | pending | 249.00 |
| 98 | pending | 99.99 |
| 114 | pending | 249.00 |
| 137 | pending | 449.00 |
| 145 | pending | 799.00 |
| 161 | pending | 799.00 |
| 176 | pending | 1099.00 |
| 187 | pending | 449.00 |
| 195 | pending | 799.00 |
Correct a SQL query with the wrong rows
The store has 11 orders with the status 'pending' and 11 orders with the status 'cancelled'. So a correct result for this task has 22 rows. This query is in the editor:
SELECT id, status
FROM orders
WHERE status = 'pending' AND status = 'cancelled'
ORDER BY id; PostgreSQL runs it with no error message. Press "Check task 1" with this query, and you get the message "Expected 22 rows, got 0." Hint: with this query, you ask for orders with both statuses at the same time. Correct the query, and press "Check task 1" again.
Where should you learn SQL?
Use a course or a practice site with these features:
- You write each SQL query yourself.
- You run each query on a real database.
- You get a check on the result of your query, not only on the text of your query.
- You can start free, with no installation.
We scored free SQL courses with tests like these. See where you can learn SQL free.
In SQLMaxx Learn, you get a grade on the result of each practice problem in all 66 lessons, but you get no video and only PostgreSQL.
Can you learn SQL with AI tools like ChatGPT?
Yes, but do not let an AI tool write the query for you while you practice. If you copy the query from an AI tool, you do not practice from memory.
- Write the query yourself first.
- If PostgreSQL shows an error message that you do not understand, ask the AI tool to explain it.
- Check a query from an AI tool the same way as your own query. Compare the result with a number that you know.
Start SQLMaxx Learn lesson 1
We call a lesson mastered when we estimate from your answers that you know it. In Learn mode, you get your next lesson from the lessons whose prerequisites you have mastered. In Review mode, you practice lessons that you have mastered. We predict your recall of each lesson from your answers in Learn and Review and from the time since you last practiced it. The automatic Review list is the lessons with a predicted recall below 0.80. You can also make your own list. You need an account for Review.
Lesson 1 is "SELECT and Column Expressions". You can start it in your browser with no account. For all 66 lessons in order, see the SQLMaxx Learn lessons.
More questions about how to learn SQL
Can I learn SQL by myself?
Yes. With no teacher, you must find your own errors, so check each result. For more, read our answer to the question can you learn SQL on your own.
How far apart should you space SQL practice sessions?
We found no SQL study about this. In a review of memory research with verbal recall tasks, the best gap between study sessions was longer when people had to remember for a longer time. Source: Cepeda and others, 2006. For time figures, read our page on how long it takes to learn SQL.
Which SQL dialect should I learn first?
Learn the dialect of the database that you will use at work or in your class. SQLMaxx Learn uses PostgreSQL. PostgreSQL conforms to at least 170 of the 177 mandatory Core features of SQL:2023. In the same documentation, the PostgreSQL authors also write: "at the time of writing, no current version of any database management system claims full conformance to Core SQL:2023." Source: PostgreSQL 17 documentation, SQL Conformance.