JOBSEEKER?

SQL Interview Questions

Practice for SQL interviews by solving TestDome questions. Our interview questions are used by more than 7,000 companies and 450,000 individual test takers.

Jobseekers: Certify Your Knowledge

Take a Certification Test

Companies: Use Our Tests for Screening

Buy a Pack Of Candidates

Need to practice your SQL skills for an upcoming job interview? Try solving these SQL interview questions that test knowledge of relational database concepts such as joins, conditions, indexes, constraints, and other skills. We’ll provide feedback on your answers, and you can use a hint if you get stuck.

These SQL interview questions are examples of real tasks used by employers to screen job candidates such as database administrators, back-end developers, data analysts, and others that require knowledge of SQL in order to interface with and access an SQL database efficiently.

1. Students

Aggregation Select
   
Easy  

Given the following data definition, write a query that returns the number of students whose first name is John. String comparisons should be case sensitive.

TABLE students
   id INTEGER PRIMARY KEY,
   firstName VARCHAR(30) NOT NULL,
   lastName VARCHAR(30) NOT NULL
SQLite 3.28  
 


  •   No students named John: Wrong answer
  •   Several students named John: Wrong answer
  •   Every student is named John: Wrong answer


2. Enrollment

Conditions Update
   
Easy  

A table containing the students enrolled in a yearly course has incorrect data in records with ids between 20 and 100 (inclusive).

TABLE enrollments
  id INTEGER NOT NULL PRIMARY KEY
  year INTEGER NOT NULL
  studentId INTEGER NOT NULL

Write a query that updates the field 'year' of every faulty record to 2015.

SQLite 3.28  
 


  •   Ids equal to 20 and 100: Wrong answer
  •   Ids between 20 and 100: Wrong answer
  •   All ids: Wrong answer


3. Pets

Select Union
   
Easy  

Information about pets is kept in two separate tables:

TABLE dogs
  id INTEGER NOT NULL PRIMARY KEY,
  name VARCHAR(50) NOT NULL

TABLE cats
  id INTEGER NOT NULL PRIMARY KEY,
  name VARCHAR(50) NOT NULL

Write a query that select all distinct pet names.

See the example case for more details.

SQLite 3.28  
 


  •   Example case: Wrong answer
  •   Unique names: Wrong answer
  •   Cats have the same names as dogs: Wrong answer
  •   Various duplicate names: Wrong answer


4. Social Network

Conditions Left join Select
   
Easy 

A new social network site has the following data tables:

users
id name sex
1 Ann null
2 Steve m
3 Mary f
4 Brenda f
friends
user1 user2
1 2
1 3
2 3

Select data that will be returned by the following SQL query:

SELECT users.name, COUNT(*) as count FROM users
LEFT JOIN friends
ON users.id = friends.user1 OR users.id = friends.user2
WHERE users.sex = 'f'
GROUP BY users.id, users.name;

(Select all acceptable answers.)

Ann, 1
Ann, 2
Steve, 1
Steve, 2
Mary, 1
Mary, 2
Brenda, 0
Brenda, 1
   


5. Sessions

Aggregation Group by Select
   
Easy  

App usage data are kept in the following table:

TABLE sessions
  id INTEGER PRIMARY KEY,
  userId INTEGER NOT NULL,
  duration DECIMAL NOT NULL

Write a query that selects userId and average session duration for each user who has more than one session.

See the example case for more details.

SQLite 3.28  
 


  •   Example case: Wrong answer
  •   Users with several sessions: Wrong answer
  •   Various users: Wrong answer


6. Web Shop

Joins Select
   
Easy  

Each item in a web shop belongs to a seller. To ensure service quality, each seller has a rating.

The data are kept in the following two tables:

TABLE sellers
  id INTEGER PRIMARY KEY,
  name VARCHAR(30) NOT NULL,
  rating INTEGER NOT NULL

TABLE items
  id INTEGER PRIMARY KEY,
  name VARCHAR(30) NOT NULL,
  sellerId INTEGER REFERENCES sellers(id)

Write a query that selects the item name and the name of its seller for each item that belongs to a seller with a rating greater than 4. The query should return the name of the item as the first column and name of the seller as the second column.

See the example case for more details.

SQLite 3.28  
 


  •   Example case: Wrong answer
  •   Single seller with single item: Wrong answer
  •   Top rated sellers: Wrong answer
  •   Low rated sellers: Wrong answer


7. Workers

Conditions Select Subqueries
   
Hard  

The following data definition defines an organization's employee hierarchy.

An employee is a manager if any other employee has their managerId set to this employee's id. That means John is a manager if at least one other employee has their managerId set to John's id.

TABLE employees
  id INTEGER NOT NULL PRIMARY KEY
  managerId INTEGER REFERENCES employees(id)
  name VARCHAR(30) NOT NULL

Write a query that selects the names of employees who are not managers.

See the example case for more details.

SQLite 3.28  
 


  •   Example case: Wrong answer
  •   No managers: Wrong answer
  •   Workers have managers: Wrong answer
  •   Managers have managers: Wrong answer


8. Users And Roles

Constraints Create table Database schema
   
Hard  

The following two tables are used to define users and their respective roles:

TABLE users
  id INTEGER NOT NULL PRIMARY KEY,
  userName VARCHAR(50) NOT NULL

TABLE roles
  id INTEGER NOT NULL PRIMARY KEY,
  role VARCHAR(20) NOT NULL

The users_roles table should contain the mapping between each user and their roles. Each user can have many roles, and each role can have many users.

Modify the provided SQLite create table statement so that:

  • Only users from the users table can exist within users_roles.
  • Only roles from the roles table can exist within users_roles.
  • A user can only have a specific role once.

See the example case for more details.

SQLite 3.28  
 


  •   Example case: Wrong answer
  •   Only users from users table can exist: Wrong answer
  •   Only roles from the roles table can exist: Wrong answer
  •   A user can only have a specific role once: Wrong answer


9. Regional Sales Comparison

Aggregation Left join Select
   
Hard  

An insurance company maintains records of sales made by its employees. Each employee is assigned to a state. States are grouped under regions. The following tables contain the data:

TABLE regions
  id INTEGER PRIMARY KEY
  name VARCHAR(50) NOT NULL

TABLE states
  id INTEGER PRIMARY KEY
  name VARCHAR(50) NOT NULL
  regionId INTEGER NOT NULL REFERENCES regions(id)

TABLE employees
  id INTEGER PRIMARY KEY
  name VARCHAR(50) NOT NULL
  stateId INTEGER NOT NULL REFERENCES states(id)

TABLE sales
  id INTEGER PRIMARY KEY
  amount INTEGER NOT NULL
  employeeId INTEGER NOT NULL REFERENCES employees(id)  

Management requires a comparative region sales analysis report.

Write a query that returns:

  • The region name.
  • Average sales per employee for the region (Average sales = Total sales made for the region / Number of employees in the region).
  • The difference between the average sales of the region with the highest average sales, and the average sales per employee for the region (average sales to be calculated as explained above).

Employees can have multiple sales. A region with no sales should be also returned. Use 0 for average sales per employee for such a region when calculating the 2nd and the 3rd column.

See the example case for more details.

SQLite 3.28  
 


  •   Example case: Wrong answer
  •   Region-wise sales correctly calculated: Wrong answer
  •   Some regions have no employees, or all employees have no sales: Wrong answer
  •   All regions have sales, employees have zero to many sales: Wrong answer


If you feel ready, take one of our timed public SQL Interview Questions tests:
HTML/CSS

HTML/CSS, Angular, TypeScript, Node.js, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, C# Algorithms, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Java Algorithms, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Java, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Node.js, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, PHP, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Python Algorithms, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Python, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Ruby, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Scala, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, VB.NET, and SQL Online Test (Easy / Hard)

HTML/CSS, React, Node.js, and SQL Online Test (Easy / Hard)

HTML/CSS, Vue.js, Node.js, and SQL Online Test (Easy / Hard)

Data Science

General and Python Data Science, and SQL Online Test (Easy / Hard)

General and Python Data Science, Python, and SQL Online Test (Easy / Hard)

General Data Science and SQL Online Test (Easy / Hard)

C#

C# Algorithms and SQL Online Test (Easy / Hard)

C# and SQL Online Test (Easy / Hard)

C++

C++ Algorithms and SQL Online Test (Easy / Hard)

C++ and SQL Online Test (Easy / Hard)

Java

Java Algorithms and SQL Online Test (Easy / Hard)

Java and SQL Online Test (Easy / Hard)

JavaScript

JavaScript and SQL Online Test (Easy / Hard)

JavaScript, Node.js, and SQL Online Test (Easy / Hard)

PHP

PHP and SQL Online Test (Easy / Hard)

PHP, MySQL, and SQL Online Test (Easy / Hard)

Python

Python Algorithms and SQL Online Test (Easy / Hard)

Python and SQL Online Test (Easy / Hard)

ASP.NET (Core) MVC

ASP.NET (Core) MVC, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

ASP.NET Web Forms

ASP.NET Web Forms, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

MySQL

MySQL and SQL Online Test (Easy / Hard)

Oracle PL/SQL

Oracle PL/SQL and SQL Online Test (Easy / Hard)

Ruby

Ruby and SQL Online Test (Easy / Hard)

Scala

Scala and SQL Online Test (Easy / Hard)

SQL

SQL Online Test (Easy / Hard)

VB.NET

VB.NET and SQL Online Test (Easy / Hard)

Not exactly what you are looking for? Go to our For Jobseekers section.
Dashboard Start Trial Sign In Home Tour Tests Questions Pricing For Jobseekers