Learn SQL: where to start?
Updated on
Want to learn SQL but not sure where to start? This guide gives you a clear order for the 16 topics you need, from your first SELECT query to window functions, each with a reference card and exercises with solutions. Nothing to install: you practice everything in your browser.
What is SQL?
SQL (Structured Query Language) is the language used to query relational databases: data stored in tables, with rows and columns, like a spreadsheet but much bigger. You use it to read, filter, group and combine that data. PostgreSQL, MySQL, SQLite, SQL Server and Oracle all speak SQL, with small syntax differences.
It is a skill in demand far beyond developers: data analysts, product managers, marketing, finance… As soon as you work with data, being able to write a query saves hours.
How long does it take to learn SQL?
The basics (reading, filtering, sorting, grouping, joining two tables) take a few weeks with a little practice every day: twenty regular minutes beat three hours once a month. Advanced topics, like CTEs and window functions, then take a few more weeks. What matters most is not reading, but writing queries.
Step 1: read data
Start by learning to pick columns, filter rows and sort the result. It is the basis of every other query.
- SELECT, aliases, DISTINCT: pick the columns. Card · exercises with solutions
- WHERE: filter rows (=, >, BETWEEN, IN, LIKE). Card · exercises with solutions
- ORDER BY, LIMIT: sort and keep the first rows. Card · exercises with solutions
Step 2: summarise data
Next, learn to compute totals, averages and counts, overall and then per group.
- COUNT, SUM, AVG, MIN, MAX: aggregates. Card · exercises with solutions
- GROUP BY: one summary per group. Card · exercises with solutions
- HAVING: filter groups. Card · exercises with solutions
Step 3: combine several tables
This is the step that really makes the difference: useful data is almost always spread across several tables.
- JOIN: link two tables. Card · exercises with solutions
- Subqueries: a query inside a query. Card · exercises with solutions
- NULL, COALESCE: handle missing values. Card · exercises with solutions
- CASE: row-by-row conditions. Card · exercises with solutions
- UNION, INTERSECT, EXCEPT: combine results. Card · exercises with solutions
- EXISTS: test whether related rows exist. Card · exercises with solutions
Step 4: go further
These topics set experienced profiles apart and often come up in interviews.
- Text and dates: UPPER, SUBSTR, LIKE, strftime… Card · exercises with solutions
- CTEs (WITH): break a complex query into steps. Card · exercises with solutions
- Window functions (OVER): rankings, running totals, comparisons with the previous row. Card · exercises with solutions
- Recursive CTEs: sequences and hierarchies. Card · exercises with solutions
Practice a little every day
Reading a course is not enough: you remember by writing queries, getting them wrong, then fixing them. Three ways to practice with SpeedQL:
- the daily SQL exercise: one question a day, to keep the rhythm and a day streak;
- the custom exercises: no timer, on the topics you choose, with hints;
- the timed game: to gain speed once you know the basics.
The most common beginner mistakes
- Writing
column = NULLinstead ofcolumn IS NULL: a comparison with NULL is never true. - Showing a column in SELECT without putting it in GROUP BY or in an aggregate.
- Putting a condition on COUNT or SUM in WHERE instead of HAVING.
- Forgetting the ON condition of a join, which combines every row with every other row.
- Using NOT IN with a subquery that can return NULL: prefer NOT EXISTS.
What next?
Once you have practiced the 16 topics, install a real database (SQLite or PostgreSQL, both free) and query a dataset you care about: sport, films, your own spending… If you are preparing for an interview, review the SQL interview questions with answers, and keep the SQL cheat sheet at hand, with its table of differences between SQLite, PostgreSQL and MySQL.
Start now
Your first query is waiting: free, no sign-up, right in your browser.