DSA Tracker

SQL

SQL FOR INTERVIEWS

SQL interview questions, run in your browser

Forty questions, easiest first. Write a query, run it on real tables in this tab, and check it against the expected result. Work in order: the simple filters first, then joins, subqueries and window functions.

All 40 questions

  1. 001SELECT and WHEREeasyCompany: employees and departments

    Departments and their cities

    Show the name and city of every department. Every department should appear, including any with no employees.

  2. 002SELECT and WHEREeasyCompany: employees and departments

    Employees earning over 100000

    List the name of each employee who earns more than 100000.

  3. 003SELECT and WHEREeasyShop: customers, products and orders

    Delivered orders

    Show the id and customer_id of every order whose status is delivered.

  4. 004SELECT and WHEREeasyCampus: students, courses and enrollments

    Students from Jaipur

    List the name of every student who lives in Jaipur.

  5. 005SELECT and WHEREeasyCompany: employees and departments

    Hired on or after 2020

    Show the name and hire date of every employee hired on or after 1 January 2020.

  6. 006SELECT and WHEREeasyShop: customers, products and orders

    Products priced between 500 and 3000

    Show the name and price of each product priced from 500 to 3000, with both ends included.

  7. 007SELECT and WHEREeasyCompany: employees and departments

    Employees with no manager

    List the name of every employee who has no manager.

  8. 008ORDER BY and LIMITeasyCompany: employees and departments

    Highest salary first, names as tie-breaker

    List every employee's name and salary, highest salary first. When two salaries are equal, sort those names A to Z.

  9. 009ORDER BY and LIMITeasyShop: customers, products and orders

    Three most recent orders

    Show the id and order_date of the three most recent orders, newest first.

  10. 010ORDER BY and LIMITeasyCampus: students, courses and enrollments

    Top two scores, pending ones ignored

    Show the student_id, course_id and score of the two highest scores in enrollments. Ignore rows with no score yet.

  11. 011GROUP BY and HAVINGeasyCompany: employees and departments

    Employees per department_id

    Count the employees in each department_id. Employees with no department form their own group.

  12. 012GROUP BY and HAVINGeasyShop: customers, products and orders

    Average quantity per customer, rounded

    For each customer, find the average quantity per order, rounded to 2 decimal places. Show customer_id and the average.

  13. 013GROUP BY and HAVINGeasyShop: customers, products and orders

    Orders per status

    Count the orders in each status. Show the status and the count.

  14. 014GROUP BY and HAVINGmediumShop: customers, products and orders

    Customers with more than two orders

    Find the customers who placed more than two orders. Show customer_id and the number of orders.

  15. 015DuplicatesmediumShop: customers, products and orders

    Emails used on more than one row

    Find every email address that appears on more than one customer row. Show the email and how many rows use it.

  16. 016JOINmediumCompany: employees and departments

    Employees with their department name

    Show each employee's name with the name of their department. Leave out employees who have no department.

  17. 017JOINmediumCompany: employees and departments

    Department sizes, including empty ones

    Show every department name with the number of employees in it. A department with nobody in it must show 0.

  18. 018JOINmediumCompany: employees and departments

    Employees beside their managers

    Show each employee's name next to their manager's name. Employees with no manager still appear, with NULL in the manager column.

  19. 019JOINmediumCompany: employees and departments

    Same department and same salary

    Find pairs of different employees who work in the same department and earn exactly the same salary. Show both names, and list each pair once.

  20. 020JOINmediumShop: customers, products and orders

    Delivered orders with customer and product

    For every delivered order, show the order id, the customer's name and the product's name.

  21. 021JOINmediumShop: customers, products and orders

    Customers who never ordered

    List the names of customers who have never placed an order.

  22. 022JOINmediumShop: customers, products and orders

    Units sold per product, zero if none

    For each product, show its name and the total quantity across all of its orders. A product that was never ordered shows 0.

  23. 023JOINmediumCampus: students, courses and enrollments

    Scores of 80 or more, with names

    List the student name and course title for each enrollment with a score of 80 or more.

  24. 024SubqueriesmediumCompany: employees and departments

    Above the company average salary

    Show the name and salary of every employee who earns more than the average salary of all employees.

  25. 025SubqueriesmediumCompany: employees and departments

    Employees in Bengaluru departments

    List employees who work in a department located in Bengaluru. (Hint: use an IN subquery rather than a join.)

  26. 026SubqueriesmediumShop: customers, products and orders

    Products nobody ordered

    List the names of products that nobody has ever ordered. (Hint: try a subquery with NOT IN.)

  27. 027SubqueriesmediumShop: customers, products and orders

    Quantity above the product's average

    Show the id, product_id and quantity of each order whose quantity is above the average quantity for that same product. Include all orders, whatever their status.

  28. 028EXISTSmediumCampus: students, courses and enrollments

    Students in a Maths course, with EXISTS

    List the students who are enrolled in at least one Maths course. (Hint: try EXISTS.)

  29. 029EXISTSmediumShop: customers, products and orders

    Customers who never bought Home products

    List the customers who have never ordered a product in the Home category. Use NOT EXISTS.

  30. 030Window functionshardCompany: employees and departments

    Numbering employees inside each department

    Number each employee inside their department by salary, highest first. Break ties by name A to Z. Show dept_id, name, salary and the number.

  31. 031Window functionshardCompany: employees and departments

    Company-wide rank with gaps

    Rank every employee by salary across the whole company, highest first. Equal salaries share a rank and the next rank is skipped. Show name, salary and rank.

  32. 032Window functionshardShop: customers, products and orders

    Running revenue by order

    For each order, show its id, its order date and the running total of revenue so far, where revenue is price times quantity. Count the orders in date order, then id order.

  33. 033Window functionshardShop: customers, products and orders

    Previous order date for each customer

    For each order, show the customer id, the order id, the order date and the date of that customer's previous order. The first order for a customer shows NULL. (Hint: try LAG.)

  34. 034Window functionshardShop: customers, products and orders

    Latest order for each customer

    Find the latest order of each customer. Show customer_id, order id and order_date. (Hint: use ROW_NUMBER to pick the full order row.)

  35. 035Window functionshardCompany: employees and departments

    Each salary beside its department average

    Show each employee's name, salary and the average salary of their department beside them. Do not round the average.

  36. 036Nth highest valuehardCompany: employees and departments

    Second highest distinct salary

    Find the second highest distinct salary. Show one value.

  37. 037Nth highest valuehardCompany: employees and departments

    Holders of the third highest distinct salary

    Find the name and salary of each employee who earns the third highest distinct salary. (Hint: try a window function.)

  38. 038CASEmediumCompany: employees and departments

    Pay bands with CASE

    Label each employee's pay band. Use High for a salary of 120000 or more, Mid for 80000 or more, and Entry for anything lower. Show name and band.

  39. 039DatesmediumShop: customers, products and orders

    Orders per month in 2024

    Count the orders placed in each month of 2024. Show the month as YYYY-MM and the count.

  40. 040DateshardShop: customers, products and orders

    Days from signup to first order

    For each customer with at least one order, show the name and the number of whole days from their signup date to their first order date. Count every order, whatever its status.

Practise one question

Solved 0 of 40

SELECT and WHERE | easy | Question 001

Departments and their cities

Show the name and city of every department. Every department should appear, including any with no employees.

Tables

  • departments (id, name, city)
  • employees (id, name, dept_id, manager_id, salary, hire_date)

Your query runs in this tab, on a fresh copy of the tables. The text you type is not sent anywhere.