SQL Interview Questions

Want to become an expert in cracking SQL interview questions/Database interview questions?

Start with practicing the questions below. Whether a question involves multiple choice or live coding, we will give you hints as you go and tell you if your answers are correct or incorrect.

After that, take our timed public SQL Interview Questions Test.

To use our service for testing candidates, buy a pack of candidates.


1. Students
SQL Aggregation Public

Given the following data definition, write a query that returns the number of students whose first name is John.

TABLE students
   id INTEGER PRIMARY KEY,
   firstName VARCHAR(30) NOT NULL,
   lastName VARCHAR(30) NOT NULL
Easy  
3min
SQLite 3.19.3
 


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

2. Enrollment
SQL Conditions Update Public

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.

Easy  
5min
SQLite 3.19.3
 


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

3. Pets
SQL Joins Public

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.

Easy  
5min
SQLite 3.19.3
 


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

4. Sessions
SQL Aggregation Public

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.

Easy  
7min
SQLite 3.19.3
 


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

5. Web Shop
SQL Joins Public

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.

See the example case for more details.

Easy  
7min
SQLite 3.19.3
 


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

6. Users And Roles
SQL Constraints Public New

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.

Improve the provided 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.

Hard  
7min
SQLite 3.19.3
 


  •   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 exist within a role once: Wrong answer

7. Workers
SQL Subqueries Public

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

An employee is a manager if any other employee has their managerId set to the first employees id. An employee who is a manager may or may not also have a manager.

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.

Hard  
7min
SQLite 3.19.3
 


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

If you feel ready, take one of our timed public SQL Interview Questions tests:
  • ASP.NET (Core) MVC, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)
  • ASP.NET Web Forms, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)
  • C# Algorithms and SQL Online Test (Easy / Hard)
  • C# and SQL Online Test (Easy / Hard)
  • C++ Algorithms and SQL Online Test (Easy / Hard)
  • C++ 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, 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)
  • Java Algorithms and SQL Online Test (Easy / Hard)
  • Java and SQL Online Test (Easy / Hard)
  • JavaScript and SQL Online Test (Easy / Hard)
  • Oracle PL/SQL and SQL Online Test (Easy)
  • PHP and SQL Online Test (Easy / Hard)
  • Python Algorithms and SQL Online Test (Easy)
  • Python and SQL Online Test (Easy / Hard)
  • Ruby and SQL Online Test (Easy / Hard)
  • SQL Online Test (Easy / Hard)
Not exactly what you are looking for? Go to our For Jobseekers section.