SQL Interview Questions — Real Questions to Prep for Your Job Interview
If you're looking for a job in data, chances are you're going to have to answer some SQL interview questions, or complete some kind of SQL test.
That's because SQL skills are required for most data jobs. We dug into the data in depth in this post about why you should learn SQL, but the short version is this: more than half of all data analyst, data scientist, and data engineer jobs in 2021 list SQL as a requirement.
The importance of SQL is especially stark for data analyst roles:
SQL is far and away the most in-demand skill for Data Analyst roles. Data: Indeed.com, 1/29/2021.
Preparing for SQL questions in a job interview
We've written an extensive guide on job interviews in data science. You should be aware that SQL will almost certainly play a role in your interview process, especially if you're looking at Data Analyst roles.
Every company does things differently, but here are a few of the more common ways companies test SQL skills:
- In-person (or video) interview where you're asked SQL questions or given SQL problems to solve.
- Take-home SQL task or tasks.
- In-person (or video) live coding session where you're asked to use SQL skills to answer questions in real time.
- Whiteboard coding session where you're asked to demonstrate your SQL skills by sketching out queries on a whiteboard.
If you're not comfortable writing SQL queries already, there's no time like the present to sign up for a free account and dive into our interactive SQL courses. But let's say you're already a SQL master. You're still going to want some practice!
And that's where you may encounter a problem.
Online practice question lists are (mostly) terrible
If you Google "SQL Interview questions," you're going to find a bunch of articles that list questions like these (these are all real questions pulled from top-ranking articles)
- What is SQL?
- What is a database?
- What are tables?
- What is a join?
You get the idea. And we suppose it's possible that you'll be asked "what is SQL" in a job interview. But it's definitely not likely.
Much more likely: the SQL interview questions you'll face will be asking you to solve real problems with SQL, or asking you to answer trickier questions that test your working knowledge.
We've compiled some of these questions below, and provided expandable answers so that you can test yourself, and then check to make sure you're right.
Test yourself with real SQL interview questions:
Question 1
Given the table below, write a SQL query that retrieves the personal data about alumni who scored above 16 on their calculus exam.
alumni
student_id | name | surname | birth_date | faculty |
---|---|---|---|---|
347 | Daniela | Lopes | 1991-04-26 | Medical School |
348 | Robert | Fischer | 1991-03-09 | Mathematics |
evaluation
student_id | class_id | exam_date | grade |
---|---|---|---|
347 | 74 | 2015-06-19 | 16 |
347 | 87 | 2015-06-06 | 20 |
348 | 74 | 2015-06-19 | 13 |
curricula
class_id | class_name | professor_id | semester |
---|---|---|---|
74 | algebra | 435 | 2015_summer |
87 | calculus | 532 | 2015_summer |
46 | statistics | 625 | 2015_winter |
Click to reveal answer
There are several possible answers. Here’s one:
SELECT a.name, a.surname, a.birth_date, a.faculty
FROM alumni AS a
INNER JOIN evaluation AS e
ON a.student_id=e.student_id
INNER JOIN curricula AS c
ON e.class_id = c.class_id
WHERE c.class_name = 'calculus' AND e.grade>16;
Question 2
We’ll work with the beverages
table. Its first rows are given below.
id | name | launch_year | fruit_pct | contributed_by |
---|---|---|---|---|
1 | Bruzz | 2007 | 45 | Sam Malone |
2 | Delightful | 2008 | 41 | Sam Malone |
3 | Nice | 2015 | 42 | Sam Malone |
Write a query to extract only beverages where fruit_pct
is between 35
and 40
(including both ends).
Click to reveal answer
There are several possible answers. Here’s one:
SELECT *
FROM beverages
WHERE fruit_pct BETWEEN 35 AND 40;
Question 3
We’ll work with the beverages table again. Its first rows are given below.
id | name | launch_year | fruit_pct | contributed_by |
---|---|---|---|---|
1 | Bruzz | 2007 | 45 | Sam Malone |
2 | Delightful | 2008 | 41 | Sam Malone |
3 | Nice | 2015 | 42 | Sam Malone |
Write a query to extract only beverages whose contributor only has one name
Click to reveal answer
There are several possible answers. Here’s one:
SELECT *
FROM beverages
WHERE contributed_by NOT LIKE '% %';
Question 4
We’ll work with the beverages
table again. Its first rows are given below.
id | name | launch_year | fruit_pct | contributed_by |
---|---|---|---|---|
1 | Bruzz | 2007 | 45 | Sam Malone |
2 | Delightful | 2008 | 41 | Sam Malone |
3 | Nice | 2015 | 42 | Sam Malone |
Write a query that finds the average fruit_pct
by contributor and displays it ascending order.
Click to reveal answer
There are several possible answers. Here’s one:
SELECT contributed_by, AVG(fruit_pct) AS mean_fruit
FROM beverages
GROUP BY contributed_by
ORDER BY mean_fruit;
Question 5
Take a look at the query given below:
SELECT column, AGG_FUNC(column_or_expression), …
FROM a_table
INNER JOIN some_table
ON a_table.column = some_table.column
WHERE a_condition
GROUP BY column
HAVING some_condition
ORDER BY column
LIMIT 5;
In what order does SQL run the clauses? Select the correct option from the list of choices below:
SELECT
,FROM
,WHERE
,GROUP BY
FROM
,WHERE
,HAVING
,SELECT
,LIMIT
SELECT
,FROM
,INNER JOIN
, GROUP BYFROM
,SELECT
,LIMIT
,WHERE
Click to reveal answer
The correct option is 2
. It goes like this:
- The SQL engine fetches the data from the tables (
FROM
andINNER JOIN
) - Filters it (
WHERE
) - Aggregates the data (
GROUP BY
) - Filters the aggregated data (
HAVING
) - Selects the columns and expressions to display (
SELECT
) - Orders the remaining data (
ORDER BY
) - Limits the results (
LIMIT
)
Question 6
What is the purpose of an index in a database table?
Click to reveal answer
The purpose of an index in a database table is to improve the speed of looking through that table's data. The standard analogy is that it's (usually) much faster to look up something in a book by looking at its index than by flipping every page until we find what we want.
Question 7
What rows of my_table
does the following query yield? Give a descriptive answer.
SELECT *
FROM my_table
WHERE 1 = 1.0;
Click to reveal answer
It returns the whole table because 1=1.0
always evaluates to true.
Question 8
What rows of my_table
does the following query yield? Give a descriptive answer.
SELECT *
FROM my_table
WHERE NULL = NULL;
Click to reveal answer
It returns no rows because, by definition, NULL
does not equal itself.
More resources for SQL interview prep
We'll be adding new questions to that list over time, but in the interim, here are some more helpful resources for review during your SQL interview question prep:
- Our downloadable SQL Cheat Sheet
- Our SQL courses. We also have interactive SQL practice problems in our app to give you even more SQL practice.
- Cracking the SQL Interview — A good collection of SQL interview questions and topics on Github that's much more useful than most of the listicles out there.
- Another Github repo of interview questions that are ostensibly pulled from real SQL job interviews.
Of course, don't forget to bookmark this post, because we'll be adding more SQL interview questions for you to quiz yourself with over time!
SQL questions written by Bruno Cunha.