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 developers, purchase one of the paid plans for companies.


1. 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  
3min
Solve question

2. 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
Solve question

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
Solve question

4. Web Shop
SQL Joins Public

Each item in a web shop belongs to a different 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 of more than 4.

See the example case for more details.

Easy  
7min
Solve question

5. 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  
5min
Solve question

6. 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.

Hard  
7min
Solve question

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 it's 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
Solve question

If you feel ready, take one of our timed public SQL interview questions tests:
  • ASP.NET 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, 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 (quick test) ( Easy )
  • PHP and SQL online test ( Easy  /  Hard )
  • Python and SQL online test ( Easy  /  Hard )
  • Ruby and SQL online test ( Easy  /  Hard )
  • SQL (quick test) ( Easy  /  Hard )
Not exactly what you are looking for? Go to our For developers section.