Practice SQL online

SQL Practice Questions with Answers

30 SQL exercises from your first SELECT to joins, subqueries and window functions. Write the query, run it in your browser on a real SQLite database, and press Check to know at once if it is right. Hints and solutions for every question, free and with no sign-up.

SELECT basics

SELECT, WHERE, ORDER BY, LIMIT and DISTINCT.

  1. Select every column from a table Show every column and every row of the employees table. EasyEmployees
  2. Select specific columns List the name and salary of every employee. EasyEmployees
  3. Filter rows with WHERE Show the name and salary of employees who earn more than 7000. EasyEmployees
  4. Top 3 highest salaries Show the name and salary of the three best-paid employees, highest salary first. EasyEmployees
  5. Remove duplicates with DISTINCT List each product category once, in alphabetical order. EasyShop

Filtering rows

AND, BETWEEN, LIKE, IN and IS NULL.

  1. Combine conditions with AND Show the name and salary of employees in department 1 (Engineering) who earn less than 8000. EasyEmployees
  2. Dates in a range with BETWEEN Show the name and hired_on date of employees hired in 2020 or 2021. EasyEmployees
  3. Match text patterns with LIKE Show the name of every employee whose name starts with the letter L. EasyEmployees
  4. Match a list of values with IN Show the name, category and price of products in the Video or Displays category. EasyShop
  5. Find missing values with IS NULL Show the name of employees who have no manager. EasyEmployees

Aggregates and GROUP BY

COUNT, AVG, MIN, MAX, GROUP BY and HAVING.

  1. Count rows with COUNT How many employees are there? Return one number. EasyEmployees
  2. Average salary with AVG Show the average salary of all employees, rounded to a whole number. EasyEmployees
  3. Count per group with GROUP BY For each department_id, show the department id and the number of employees in it. MediumEmployees
  4. Filter groups with HAVING Show the department_id and the number of employees for departments that have more than 3 employees. MediumEmployees
  5. Cheapest and most expensive per category For each product category, show the category, the lowest price and the highest price. MediumShop

SQL joins practice

INNER JOIN, LEFT JOIN, self joins, three-table joins and anti-joins.

  1. Join two tables with INNER JOIN Show each employee's name next to the name of their department. MediumEmployees
  2. Keep unmatched rows with LEFT JOIN List every employee's name with the name of their manager. Employees without a manager must still appear, with an empty (NULL) manager. MediumEmployees
  3. Compare rows in the same table Find employees who earn more than their own manager. Show the employee's name, their salary, and the manager's name. MediumEmployees
  4. Join three tables For every order item, show the order id, the customer's name, the product's name and the quantity. MediumShop
  5. Total quantity sold per product Show each product's name and the total quantity ordered across all orders, highest total first. MediumShop
  6. Customers with a pending order Show the name of every customer who has at least one order with status 'pending'. Each customer once. MediumShop
  7. Find rows with no match (anti-join) Show the name of customers who have never had an order with status 'shipped'. HardShop

Subqueries and CTEs

Subqueries in WHERE, IN, EXISTS, correlated subqueries and WITH.

  1. Compare with an average using a subquery Show the name and salary of employees who earn more than the average salary of all employees. MediumEmployees
  2. Filter with IN and a subquery Show the name of customers who ordered a Monitor. MediumShop
  3. Highest paid employee in each department For each department, show the name, department_id and salary of the employee with the highest salary. HardEmployees
  4. Use a CTE (WITH clause) Using a WITH clause, calculate the average salary of each department, then show the department name and its average (rounded) for departments whose average is above 6000. MediumEmployees
  5. Check for related rows with EXISTS Show the name of departments that have at least one employee hired in 2022 or later. MediumEmployees

Window functions and CASE

RANK() OVER, running totals and CASE WHEN.

  1. Rank within groups with a window function Show each employee's name, department_id and salary, plus their salary rank inside their own department (1 = highest paid). HardEmployees
  2. Running total with SUM() OVER List the orders by date and show each order's id, ordered_on, and a running count of orders up to and including that one. HardShop
  3. Label rows with CASE WHEN Show each employee's name, salary and a salary band - 'High' for 8000 or more, 'Medium' for 6000 to 7999, and 'Low' for the rest. MediumEmployees

How the SQL practice works

  1. Open a question. It says what the result should contain and shows the tables you can use.
  2. Write your query in the editor and press Run to see its result, as often as you like.
  3. Press Check. Your result is compared with the model solution's: the values must match, and the order too when the question asks for sorted rows.
  4. Stuck? Open the hint, ask the AI to Explain or Fix your query, or look at the solution with its explanation and the most common mistake.

The two practice databases

Every question uses one of the sample databases of the SQL Online Compiler, so you can also explore them freely there.

Employees

employeesid, name, salary, department_id, manager_id, hired_on
departmentsid, name, city

Shop

customersid, name, country, joined_on
productsid, name, category, price
ordersid, customer_id, ordered_on, status
order_itemsorder_id, product_id, quantity

Frequently asked questions

Do I need to install anything to practice SQL?

No. Every exercise runs SQLite inside your browser, so you can write and run real SQL queries without installing a database or creating an account.

How is my answer checked?

Check runs your query and the model solution on the same sample database and compares the result tables. Column names do not matter, the values do; the row order only matters when the question asks for a sorted result.

Which SQL is used, SQLite or MySQL?

The exercises run on SQLite. The questions only use standard SQL (SELECT, JOIN, GROUP BY, subqueries, window functions), so the same queries work in MySQL and PostgreSQL too.

What should I practice for an SQL interview?

Joins (especially LEFT JOIN and anti-joins), GROUP BY with HAVING, subqueries and window functions such as RANK() come up most. The joins and window function sections here cover exactly those.

Is my progress saved?

Yes, in this browser. Solved exercises get a check mark on this page, and your last query for each exercise is kept until you clear it.

Is it free?

Yes. All 30 SQL practice questions, the checker, the hints and the solutions are free.