DevLearningTools

LEARN · SQL · PRACTICE

50 SQL Practice Questions

Five levels, basic SELECT statements to joins, subqueries, and table constraints. Every question has a real solution, an explanation, and its expected output, all against one fixed sample dataset below.

The Sample Dataset

Beacon Robotics' schema, the same one used throughout this course, with a small, fixed set of rows so every question's expected output below is exact and checkable.

departments
idname
1Engineering
2Sales
3HR
4Finance
5Legal
employees
idnamedept_idsalary
1Grace199000
2Noah182000
3Priya176000
4David268000
5Fatima271000
6Carlos359000
7Aisha361000
8Tom476000
9Sofia470000
10Meera185000
Your Progress

0 / 50 marked solved

Self-reported, not auto-graded, there's no live SQL engine behind this page. Check your own query against the shown solution and expected output, then mark it yourself. Saved only in this browser.

Level 1 · SQL Basics

Q1–15 · Beginner

SELECT, WHERE, ORDER BY, DISTINCT, LIKE, BETWEEN, IN.

01

Every Employee

Display every column for every row in the employees table.

02

Just Name and Salary

Display only each employee's first name, last name, and salary.

03

Salary Above 70000

Find every employee earning more than 70000.

04

Engineering Only

Find every employee in department_id 1 (Engineering).

05

Lowest Salary First

Display every employee ordered by salary, lowest first.

06

Highest Salary First

Display every employee ordered by salary, highest first.

07

Distinct Departments

Display every distinct department_id that actually has an employee in it.

08

A Salary Range

Find every employee whose salary is between 70000 and 80000, inclusive.

09

Two Departments at Once

Find every employee in department_id 1 or department_id 2.

10

Names Starting With A

Find every employee whose first name starts with the letter A.

11

Names Ending in 'a'

Find every employee whose first name ends with the letter a.

12

Names Containing 'a'

Find every employee whose first name contains the letter a anywhere.

13

The First Three Employees

Display the first three employees, ordered by id.

14

Combining Two Conditions

Find every employee earning more than 70000 who's also in Engineering (department_id 1).

15

Renaming a Column With an Alias

Display every employee's salary under the column name annual_salary instead of salary.

Level 2 · Operators and Data Manipulation

Q16–25 · Beginner

OR, NOT, INSERT, UPDATE, DELETE, and NULL.

16

Sales or Finance

Find every employee in Sales (department_id 2) or Finance (department_id 4).

17

Everyone Except Engineering

Find every employee who is not in Engineering (department_id 1).

18

Outside a Salary Range

Find every employee whose salary is NOT between 70000 and 80000.

19

A Friendlier Column Name

Display every first name under the alias employee_name.

20

Counting Everyone

Count the total number of employees.

21

Hiring a New Employee

Insert a new employee: Liam O'Brien, department_id 2 (Sales), salary 65000, hired 2026-01-15, reporting to employee id 4.

22

Giving Grace a Raise

Update employee id 1 (Grace Hopper)'s salary to 105000.

23

Transferring an Employee

Move employee id 3 (Priya Sharma) to department_id 2 (Sales).

24

An Employee Leaves

Delete employee id 7 (Aisha Bello) from the employees table.

25

Employees With No Manager

Find every employee whose manager_id is NULL (the department heads).

Level 3 · Aggregate Functions and Grouping

Q26–35 · Intermediate

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

26

The Highest Salary

Find the single highest salary in the company.

27

The Lowest Salary

Find the single lowest salary in the company.

28

Total Payroll

Calculate the total of every employee's salary.

29

Average Salary

Calculate the average salary across the whole company.

30

Headcount Per Department

Count how many employees are in each department.

31

Average Salary Per Department

Find the average salary within each department.

32

Departments With More Than One Employee

Find every department that has more than one employee.

33

Departments Earning Above 70000 on Average

Find every department whose average salary is greater than 70000.

34

The Second-Highest Salary

Find the second-highest salary in the company, without a window function.

35

Departments Ranked by Headcount

List every department's headcount, largest department first.

Level 4 · Joins and Subqueries

Q36–45 · Intermediate

INNER/LEFT JOIN, EXISTS, ANY, ALL, and correlated comparisons.

36

Employees With Department Names

Display every employee's name alongside their department's actual name, not just its id.

37

Every Department, Even Empty Ones

Display every department alongside each of its employees, including a department with no employees at all.

38

Departments With Zero Employees

Using the same LEFT JOIN idea, find which departments have no employees at all.

39

Engineering, via a Join

Find every employee in the Engineering department, by joining on the department's name instead of filtering by department_id.

40

Above-Average Earners

Find every employee earning more than the company-wide average salary.

41

Same Department as Grace

Find every employee in the same department as Grace Hopper, excluding Grace herself.

42

The Top Earner

Find the employee (or employees) earning the single highest salary.

43

Beating at Least One Sales Salary

Find every employee earning more than at least one employee in Sales (department_id 2).

44

Beating Every Sales Salary

Find every employee earning more than every employee in Sales (department_id 2).

45

Departments That Actually Have Staff

Find every department that has at least one employee, using EXISTS instead of a JOIN.

Level 5 · Table Creation and Constraints

Q46–50 · Intermediate

CREATE TABLE, ALTER TABLE, DROP TABLE, and foreign keys.

46

A New Table for Job Candidates

Create a candidates table with an id, full name, the role applied for, and the department it's for.

47

A Table With NOT NULL and UNIQUE

Create an employee_accounts table where username is required and must be unique, and email is required.

48

Adding a New Column

Add a phone column to the existing employees table.

49

Removing a Table

Delete the candidates table created in question 46.

50

A Table Linked by Foreign Key

Create an expense_claims table where every claim references a real employee by id.

HOW TO PRACTICE
  • Write your own query first, against the sample dataset shown above, before revealing the solution.
  • Run it for real in a free online playground (linked below) to actually confirm your output.
  • If your query differs from the shown solution but gets the same result, that's fine, more than one correct query usually exists.
  • When a question trips you up, use its lesson link to review that exact topic before retrying.
</>

Run These for Real

Create the sample tables above in a free SQL playground, then try each question yourself before checking the solution. Pick one based on which database you want to practice on.

SQLite / PostgreSQL

Fast, no server, no ads.

Open DB Fiddle ↗

MySQL / SQL Server

Switch databases with one dropdown.

Open SQL Fiddle ↗

Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.

Other Practice Levels