SQL Exercises
Practice queries on real PostgreSQL and SQLite in your browser, with instant checking of your results.
● Runs 100% in your browser — nothing you type leaves this page.
0 of 6 topics finished
- SELECT, WHERE and ORDER BY6 exercisesEvery query starts here: which columns, from which table, which rows, and in what order. The tables hold a small company's staff.
- Filtering: AND, IN, LIKE and NULL7 exercisesReal questions have several conditions. This is also where you'll meet NULL, which trips up nearly everyone once.
- Counting and grouping: GROUP BY and HAVING6 exercisesSo far, each row of the result was one row of the table. Now you'll sum them up: how many people per department, and the average pay.
- Joining tables: INNER, LEFT and self joins6 exercisesGood databases split data across tables, and joins bring it back together. The most useful trick here is finding what's missing, with a LEFT JOIN.
- Subqueries, CTEs and window functions6 exercisesThese are what make long questions readable. The tables here hold a small shop: customers, products and orders.
- Changing data: INSERT, UPDATE, DELETE and transactions6 exercisesQueries so far only looked. These change things, and on a real server, a missing WHERE is how people lose data. Here it costs nothing: Start again brings the data back.
🔒 Your queries run in this browser, and your progress is kept here too. Nothing is sent anywhere.
How it works
Pick a topic, and write a query for each task. The result is compared with the right answer, so any query that gets it right passes, however you wrote it.
- Real databases: PostgreSQL and SQLite run in your browser, in two tabs. Nothing here emulates SQL, so the errors you see are the real ones.
- Stuck? Each exercise has a hint, then the solution. The tables and their columns are listed under the task.
- You can’t break anything. It’s all in your browser, and Start again brings the data back.
- No account. Finished topics are remembered in this browser only.
What the checks look at
They compare the rows and columns you got with those of the solution. Column names and their order don’t count. The order of rows only counts when a task asks for it, as a query with no ORDER BY has no guaranteed order.
If the result is wrong, you’re told how: too many rows, too few columns, or the right shape with different values. You see your own result too, so you can work out why.
FAQ
Which tab should I use?
Use the database you work with, or PostgreSQL if you’re not sure. Most of what’s here works the same on both, and differences are pointed out where they matter. To compare with MySQL, SQL Server or Oracle, see the SQL Dialect Reference.
Where can I just try things?
In the SQL Playground, with the same sample data and a full editor.
My query works in MySQL. Why does it fail here?
Databases differ more than you’d expect, in quoting, dates and text functions. If you’re getting an error on your own server, see MySQL error 1064 or column must appear in the GROUP BY clause.
Is my SQL sent anywhere?
No. It runs on the database in your browser, and never leaves it.
Articles
Learn more
PostgreSQL "relation does not exist": when the table is right there
Fix PostgreSQL "ERROR: relation does not exist": check the schema and search_path, quoted names with capitals, which database you are in, and whether migrations have run.
Beginner · 4 min read
MySQL "ERROR 1064: You have an error in your SQL syntax": how to read it and fix it
Fix MySQL ERROR 1064: read the "near" text, then check reserved words like rank and groups, quotes, commas, old GRANT syntax and queries written for another database.
Beginner · 4 min read
PostgreSQL "column must appear in the GROUP BY clause or be used in an aggregate function"
Fix the GROUP BY error in PostgreSQL: understand why it happens, then aggregate the column, group by it or by the primary key, or pick one row per group with DISTINCT ON.
Beginner · 3 min read