ShareYourGroupShareYourGroupSQL

SQL exercises with answers

All 78 exercises from the lessons on one page, grouped by topic. Tap Solve to open an exercise in the practice box, write your query and press Check my answer. Exercises you have solved get a tick here, kept in your own browser.

1. What SQL is, and your first query

  1. Show everything in the orders table. Solve
  2. Show everything in the employees table. Solve
  3. Show everything in the order_items table. How many columns does it have? Solve

2. Choosing columns

  1. Show only the name and salary of every employee. Solve
  2. Show each product's name and its stock, with the stock column renamed to in_stock. Solve
  3. Show each employee's name and yearly salary. The table holds the monthly salary. Name the new column yearly. Solve

3. Filtering rows with WHERE

  1. Show the name and city of the customers who live in Mumbai. Solve
  2. Show the name and price of the products that cost 150 rupees or less. Solve
  3. Show the id and date of the orders that were cancelled. Solve

4. AND, OR, NOT, IN and BETWEEN

  1. Show the name, category and price of Electronics products that cost less than 450 rupees. Solve
  2. Show the name and state of the customers who live in Punjab or Kerala. Use IN. Solve
  3. Show the name and stock of the products whose stock is between 20 and 50, both included. Solve

5. Searching text with LIKE

  1. Find the customers whose name ends with the surname Khan or Kumar. Show the names. Solve
  2. Find the products whose name contains 1 L. Show name and price. Solve
  3. Find the employees whose name starts with a letter followed by a as the second letter. Show the names. Solve

6. NULL: missing values

  1. Show the names of the customers who have an email address. Solve
  2. Show the name and manager_id of the employees who have a manager. Solve
  3. Show every employee's name and manager_id, with 0 shown in place of a missing manager_id. Name the second column manager. Solve

7. Sorting with ORDER BY

  1. Show the name and salary of all employees, highest salary first. Solve
  2. Show the name and city of all customers, sorted by city and then by name. Solve
  3. Show the name and stock of Grocery products, lowest stock first. Solve

8. Top rows with LIMIT

  1. Show the name and stock of the four products with the most stock. Solve
  2. Show the name and joining date of the two customers who joined most recently. Solve
  3. Show the name and hiring date of the employee who has been with the shop the longest. Solve

9. Unique values with DISTINCT

  1. List the different departments in the employees table, in alphabetical order. Solve
  2. List the different states the customers come from, in alphabetical order. Solve
  3. How many different products appear in order_items? Return one number. Solve

10. Functions for text and numbers

  1. Show each customer's name in capital letters. Solve
  2. Show each product's name and its price with a 10 percent discount, rounded to a whole number. Name the column sale_price. Solve
  3. Show the name and the year of joining (the first four characters of joined) for every customer. Name the second column year. Solve

11. COUNT, SUM, AVG, MIN and MAX

  1. How many products are in the products table? Solve
  2. What is the highest salary and the lowest salary among the employees? Return two columns. Solve
  3. What is the total quantity of all items ever ordered, from order_items? Solve

12. Grouping rows with GROUP BY

  1. Count the employees in each department. Show the department and the count. Solve
  2. Show the total stock in each product category. Solve
  3. Count the orders for each status, with the largest count first. Show the status and the count. Solve

13. Filtering groups with HAVING

  1. Show the departments that have more than one employee, with the number of employees in each. Solve
  2. Show the states that have at least two customers. Show the state and the count. Solve
  3. Show the order ids that contain three or more different lines in order_items, with the number of lines. Solve

14. Joining two tables

  1. Show each order's id and status together with the city of the customer who placed it. Solve
  2. Show the name of each product in order number 1, with the quantity ordered. Solve
  3. Show the names of the customers who have a cancelled order. Solve

15. LEFT JOIN and missing matches

  1. Show every product's name with the number of order lines it appears in, including products with zero. Name the count lines. Solve
  2. Show the names of the customers who have no orders, in alphabetical order. Solve
  3. Show every customer's name and the date of their most recent order, with NULL for customers who have none. Sort by name. Solve

16. Joining three or more tables, and self joins

  1. List the different product names that Ravi Kumar has ordered, in alphabetical order. Solve
  2. Show the total value (quantity times price) of order number 20 as one number. Solve
  3. Show the names of the employees whose manager is Rohit Bansal. Solve

17. Subqueries

  1. Show the names and salaries of the employees who earn less than the average salary. Solve
  2. Using IN with a subquery, show the names of the products that were part of order 20. Solve
  3. Show the name of the product, or products, with the lowest price. Solve

18. UNION and other set operations

  1. Make one list of all the cities of customers who live in Maharashtra together with the text 'Ludhiana', without repeats. Solve
  2. Using EXCEPT, list the ids of the products that do not appear in order_items. Solve
  3. Using INTERSECT, list the customer ids that have both a delivered order and a cancelled order. Solve

19. Adding rows with INSERT

  1. Add a new product: name Pencil Box, category Stationery, price 85, stock 60. Let the database choose the id. See the lesson
  2. Add a new employee with id 9: Sara Paul, department Support, salary 31000, manager 7, hired on 2026-11-10. See the lesson
  3. Add two customers in one statement, with ids 20 and 21, any names, both in Jaipur, Rajasthan, joined on 2026-11-11. See the lesson

20. Changing and removing rows: UPDATE and DELETE

  1. Change the price of the product with id 9 to 125. See the lesson
  2. Give every employee in the Warehouse department a raise of 2000 rupees. See the lesson
  3. Delete all the cancelled orders from the orders table. See the lesson

21. Creating tables

  1. Create a table categories with an integer primary key id and a required, unique text column name. See the lesson
  2. Create a table payments with: id (integer primary key), order_id (required integer that refers to orders), amount (required integer that must be more than 0) and paid_on (required text). See the lesson
  3. Create a table cheap_products from a query: the name and price of every product that costs less than 150. See the lesson

22. Changing tables, dropping them, and indexes

  1. Add a text column called notes to the orders table. See the lesson
  2. Create an index called idx_products_category on the category column of products. See the lesson
  3. Create a table called temp_list with one text column item, and then drop it. Two statements. See the lesson

23. CASE: if, then, else in a query

  1. Show each employee's name and a column level that says senior if the salary is 45000 or more and junior otherwise. Solve
  2. In one row, show how many products are in stock (stock above 0) and how many are sold out. Two columns. Solve
  3. Show each product's name and a column stock_note: none when stock is 0, low when it is below 25, and fine otherwise. Solve

24. Working with dates

  1. Show the id and date of the orders placed in March 2026. Solve
  2. Count how many customers joined in each month. Show the month as year-month (for example 2025-01) and the count, in month order. Solve
  3. Show each order's id, its date, and the date 10 days later as due_date, for orders with status pending. Solve

25. Views and WITH (common table expressions)

  1. Using a WITH step that counts products per category, show the categories that have exactly four products. Show the category only, in alphabetical order. Solve
  2. Create a view called in_stock that shows the id, name and stock of every product whose stock is above 0. See the lesson
  3. Use a recursive WITH to list the numbers 1 to 10. Solve

26. Window functions

  1. Show each product's name, category and price, together with the highest price in its category as category_max. Sort by product id. Solve
  2. Rank the products by stock, highest first, using RANK(). Show the name, the stock and the rank, sorted by rank and then name. Solve
  3. Show the name, department and salary of the highest-paid employee in each department, sorted by department. Use ROW_NUMBER() in a WITH step. Solve

How the checking works

When you press Check my answer, the practice box runs your query on a fresh copy of the practice database and compares the rows it returns with the rows our answer returns.

A few exercises in the lessons on INSERT, UPDATE, DELETE and creating tables change the database instead of returning rows. Those cannot be checked by comparing rows, so they link to the lesson, where one answer is shown.

Getting the most from them

Do not open the answer too early. Being stuck for a few minutes is where the learning happens. Look at the practice database to check the table and column names, and reread the question for words such as "each", "only" and "at least", which usually point to GROUP BY, WHERE and HAVING.

Build the query in steps. Start with a plain SELECT * from the main table. Add the WHERE. Add the join. Add the grouping last. Run it after each step. A query that grows this way is nearly always right, and when it is wrong you know which step broke it.

After solving, compare. The lesson shows one answer. If yours differs, work out why both are correct. Seeing a second route to the same result is one of the fastest ways to improve.

Come back later. Exercises you solved a week ago are a good test of what has stayed. Mixing topics in one sitting is harder than doing one topic at a time, and makes the knowledge last longer.

This is an independent learning site. It is not connected with the makers of any database product. MySQL, PostgreSQL, SQL Server, Oracle and SQLite are named only so that you know where a piece of SQL works. The shop, its customers and its staff in the practice database are made up.