Essential SQL Skills for Data Scientists in 2021
By: Leihua Ye, Ph.D. Researcher
Tags: Sql, Programming, Data Science, Interview, Database

WHERE

Friendly Movies Streamed Last Month, LeetCode

Table: TVProgram

+---------------+---------+

| Column Name | Type |

+---------------+---------+

| program_date | date |

| content_id | int |

| channel | varchar |

+---------------+---------+

(program_date, content_id) is the primary key for this table.

This table contains information of the programs on the TV.

content_id is the id of the program in some channel on the TV.

Table: Content

+------------------+---------+

| Column Name | Type |

+------------------+---------+

| content_id | varchar |

| title | varchar |

| Kids_content | enum |

| content_type | varchar |

+------------------+---------+

content_id is the primary key for this table.

Kids_content is an enum that takes one of the values ('Y', 'N') where:

'Y' means is content for kids otherwise 'N' is not content for kids.

content_type is the category of the content as movies, series, etc.

Write an SQL query to report the distinct titles of the kid-friendly movies streamed in June 2020.

Return the result table in any order.

Walking Through My Thinking

The question asks for a movie name that meets specific criteria. There are two tables with one table (Content) containing movie title and both tables containing other selection criteria. We should combine both tables and filter through the rows using the selection criteria.

We use a WHERE clause to list these three criteria out: content_type, Kids_content, and program_date.

For the last one, the LEFT(program_date,7) method allows us to obtain the first 7 values from the left and set it equal to ‘2020–06.’

Solution

# Write your MySQL query statement below

SELECT DISTINCT title

FROM Content

LEFT JOIN TVProgram

USING(content_id)

WHERE content_type = ‘Movies’

AND Kids_content = “Y”

AND LEFT(program_date,7) = ‘2020–06’

Photo by William Iven on Unsplash

#2 Self-Join

Question: Employees Earning More Than Their Managers (LeetCode)

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+----+-------+--------+-----------+

| Id | Name | Salary | ManagerId |

+----+-------+--------+-----------+

| 1 | Joe | 70000 | 3 |

| 2 | Henry | 80000 | 4 |

| 3 | Sam | 60000 | NULL |

| 4 | Max | 90000 | NULL |

+----+-------+--------+-----------+

Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

+----------+

| Employee |

+----------+

| Joe |

+----------+

Walking Through My Thinking

For this question, we are provided with one table, Employee , and we have to make use of it as much as possible. The question asks for the employee name(s) with specific attributes (higher salary than his manager).

The question contains only one table but asks for two salaries: employee’s and manager’s, which reminds us of using a ‘self-join’ to compare one column to another one in a single table. My user take of a ‘self-join’ is when there is limited information (e.g., only one table) and we are comparing one column to another.

To that end, a self-join would suffice. The rest of the code is self-evident: a LEFT JOIN and a WHERE clause filter through cases that meet the criteria.

Solution

# Write your MySQL query statement below

SELECT e1.name AS Employee

FROM Employee e1

LEFT JOIN Employee e2

ON e1.ManagerId = e2.Id

WHERE e1.Salary > e2.Salary

#3 subquery

Project Employees II, LeetCode

Table: Project

+-------------+---------+

| Column Name | Type |

+-------------+---------+

| project_id | int |

| employee_id | int |

+-------------+---------+

(project_id, employee_id) is the primary key of this table.

employee_id is a foreign key to Employee table.

Table: Employee

+------------------+---------+

| Column Name | Type |

+------------------+---------+

| employee_id | int |

| name | varchar |

| experience_years | int |

+------------------+---------+

employee_id is the primary key of this table.

Write an SQL query that reports all the projects that have the most employees.

Walking Through My Thinking

This question asks for the project_id that has the most employee number, but it does not specify whether there are only one or multiple projects.

This is the tricky part of this question!

Multiple projects may share the same number of maximum employee number, which makes the regular attempt, group by — order — limit 1, useless, shown below:

Incorrect Code:

SELECT project_id

FROM Project

GROUP BY project_id

ORDER BY COUNT(DISTINCT employee_id) DESC

LIMIT 1

BTW, the above code will output the correct result if you run the code but return an error message if you submit the code at LeetCode. The main reason is the test case in the submission contains multiple projects with the maximum number.

Here, the key is to use a subquery to find the maximum number of employees and then set the employee number equal to the max value in the outer query. By doing so, we can identify project_id(s) that fit the description.

Solution

SELECT DISTINCT project_id

FROM Project

GROUP BY project_id

HAVING COUNT(employee_id) = (

SELECT COUNT(employee_id) AS count_num

FROM Project

GROUP BY project_id

ORDER BY count_num DESC

LIMIT 1

)

Photo by Douglas Sanchez on Unsplash

#4 WITH()

We can also use the with clause to re-write the above subquery.

Walking Through My Thinking

At the top, we create a temporary table called Sub_query with two variables: project_id and COUNT(*). In the rest, we filter through the Sub_query for the maximum count.

The beauty with the WITH() clause is a cleaner query sequence: by creating a temporary table and then filtering through the table according to specific conditions. Quite neat!

Solution: