Professional Documents
Culture Documents
Follow 622K Followers · Editors' Picks Features Deep Dives Grow Contribute About
SQL interviews are one of the most determining parts of any analytics
interview, be it data or product, let alone business analytics. Major tech firms
such as Amazon, Uber, Facebook, just to name a few, rely heavily on this round.
If you are preparing for one, going through and revising all possible variations
of SQL questions might seem a bit daunting. In order to help you in the process,
here is a guide with some sample questions and queries that you must practice
before sitting for your next SQL interview. I have prepared this based on my
own experiences, while being on both sides of the table.
SQL problems can be divided into 4 levels. As a part of this guide, we will go
through each of these levels with some standard examples that you can
practice along (I would suggest without reading the solution).
Consider the following employees table. Each row of this table indicates the
employee details such as their department, salary, manager etc.
1 -- Table: employees
2 -- | dept_id | employee_id | amount | manager_id |
3 -- |---------|-------------|--------|------------|
4 -- | 1 | 1 | 8000 | 3 |
5 -- | 1 | 2 | 5000 | 3 |
6 -- | 1 | 3 | 10000 | null |
7 -- | 2 | 4 | 15000 | null |
8 -- | 2 | 5 | 16000 | 4 |
9 -- | 3 | 6 | 8000 | null |
table details.txt
hosted with ❤ by GitHub view raw
Based on this table, write a SQL query to find the id of employees who earn
the highest amount in each department.
Best way to solve any problem is to map the problem in a step by step logic.
For example, understanding what we are solving for, in this particular case,
highest amount for each department. Then, figuring out our output
format; here we just need employee_id.
solution1.sql
hosted with ❤ by GitHub view raw
1 -- part1 : Bring manager salary along side employee salary using self join
2
3 SELECT e1.employee_id
4 FROM employees as e1
5 LEFT JOIN employees as e2 ON e1.manager_id = e2.employee_id
6
7 -- part2 : Filter employees who earn more than manager salary
8
9 SELECT e1.employee_id
10 FROM employees as e1
11 LEFT JOIN employees as e2 ON e1.manager_id = e2.employee_id
12 AND e1.amount > e2.amount
solution2.sql
hosted with ❤ by GitHub view raw
Let’s go back to the first problem. We have used a subquery to find the
employee who earns the highest salary. We can do it easily using a windows
function as well. Try without looking at the solution.
1 -- Part 1: Rank the employee_ids by highest salary for each department using DENSE_RANK()
2
3 SELECT employee_id,
4 DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY amount desc) rnk
5 from employees
6
7 -- Part 2: Filter the rows where rnk = 1
8
9 SELECT employee_id
10 FROM
11 (SELECT employee_id,
12 DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY amount desc) rnk
13 from employees) a
14 WHERE rnk = 1
solution3.sql
hosted with ❤ by GitHub view raw
Consider the following attendance table. Each row in the table contains the
employee_id and the date on which they visited the office.
Write a SQL query to find the longest streak for each of the employees.
Output should contain the employee’s name and its longest streak.
1 -- Table: attendance
2 -- | employee_id | attend_dt |
3 -- |-------------|-------------|
4 -- | 1 | 2022-01-01 |
5 -- | 1 | 2022-01-02 |
6 -- | 1 | 2022-01-05 |
7 -- | 2 | 2022-01-01 |
8 -- | 2 | 2022-01-02 |
9 -- | 2 | 2022-01-04 |
10 --`| 2 | 2022-01-05 |
11 -- | 2 | 2022-01-06 |
12 -- | 3 | 2022-01-02 |
13 -- | 3 | 2022-01-04 |
14
15 -- Table: employees
16 -- | employee_id | name |
17 -- |-------------|-------------|
18 -- | 1 | samuel |
19 -- | 2 | karthik |
20 -- | 3 | casey |
island_gaps_table.txt
hosted with ❤ by GitHub view raw
island_gaps_solution.sql
hosted with ❤ by GitHub view raw
It’s a Wrap!
In this article, we have discussed some standard SQL problems that you
might come across in your next SQL tech interview. These problems are
based on my personal experiences. Write down more such problems that
you might have faced in the comments below. It will surely help the
community to ace it together.
Every Thursday, the Variable delivers the very best of Towards Data Science: from
hands-on tutorials and cutting-edge research to original features you don't want to
miss. Take a look.