0% found this document useful (0 votes)
14 views

50 SQL To Python Series Problems

Uploaded by

olegruchinsky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

50 SQL To Python Series Problems

Uploaded by

olegruchinsky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 165

Complete

SQL to Python Series


50 Problems
50 Most Common Easy to Hard level
Interview Problems Solved Using
SQL and Python(Pandas)

Deepanshu
SQL to Python Series
Problem 1/50

Write a solution to find the ids of products that are


both low fat and recyclable.
Click here to try
SQL to Python Series
Problem 1/50

SQL Solution
SQL to Python Series
Problem 1/50
Key Points:
1. Used WHERE clause with AND to filter
data.
2. SQL use of 𝘁𝗵𝗿𝗲𝗲-𝘃𝗮𝗹𝘂𝗲𝗱 𝗹𝗼𝗴𝗶𝗰 where
expressions can evaluate to TRUE,
FALSE, or UNKNOWN.
3. 𝗪𝗛𝗘𝗥𝗘 𝗰𝗹𝗮𝘂𝘀𝗲, only rows where the
expression is TRUE are returned.
4. Order of predicates evaluation is not
guaranteed. For detail explanation click
here.
SQL Solution
SQL to Python Series
Problem 1/50

Python Solution
SQL to Python Series
Problem 1/50
Key Points:
1. Used Boolean Indexing clause with
Boolean operator & to filter data.
2. Pandas does not use of 𝘁𝗵𝗿𝗲𝗲-𝘃𝗮𝗹𝘂𝗲𝗱
𝗹𝗼𝗴𝗶𝗰.
3. For fetching column used fancy indexing
df[[`col1`]] to return DataFrame object.
Simply using df[`col1`] will return series
object.

Python Solution
SQL to Python Series
Problem 2/50

Find the names of the customer that are not referred


by the customer with id = 2.
Click here to try
SQL to Python Series
Problem 2/50

SQL Solution
SQL to Python Series
Problem 2/50
Key Points:
1. Used WHERE clause with OR to filter data.
2. SQL use of 𝘁𝗵𝗿𝗲𝗲-𝘃𝗮𝗹𝘂𝗲𝗱 𝗹𝗼𝗴𝗶𝗰 where
expressions can evaluate to TRUE, FALSE,
or UNKNOWN.
3. Rows having referee_id null will return
unknown by predicate referee_id<>2 and
will not be shown in output if we do not
include OR referee_id is null.
4. 𝗪𝗛𝗘𝗥𝗘 𝗰𝗹𝗮𝘂𝘀𝗲, only rows where the
expression is TRUE are returned.
SQL Solution
SQL to Python Series
Problem 2/50

Python Solution
SQL to Python Series
Problem 2/50
Key Points:
1. Used Boolean Indexing clause with
Boolean operator & to filter data.
2. Used Fancy indexing to fetch DataFrame
with requested column.

Python Solution
SQL to Python Series
Problem 3/50

Write a solution to find the name, population, and area of


the big countries.

A country is big if:


it has an area of at least three million (i.e., 3000000 km2), or
it has a population of at least twenty-five million (i.e.,
25000000).
Click here to try
SQL to Python Series
Problem 3/50

SQL Solution
SQL to Python Series
Problem 3/50

Python Solution
SQL to Python Series
Problem 4/50

Write a solution to find all the authors that viewed at least


one of their own articles.
Return the result table sorted by id in ascending order.
Click here to try
SQL to Python Series
Problem 4/50

SQL Solution
SQL to Python Series
Problem 4/50

Python Solution
SQL to Python Series
Problem 5/50

Write a solution to find the IDs of the invalid tweets. The


tweet is invalid if the number of characters used in the
content of the tweet is strictly greater than 15.

Click here to try


SQL to Python Series
Problem 5/50

SQL Solution
SQL to Python Series
Problem 5/50

Python Solution
SQL to Python Series
Problem 6/50

Write a solution to show the unique ID of each user, If a


user does not have a unique ID replace just show null.
Click here to try
SQL to Python Series
Problem 6/50

SQL Solution
SQL to Python Series
Problem 6/50

Python Solution
SQL to Python Series
Problem 7/50

Write a solution to report the product_name, year, and


price for each sale_id in the Sales table.
Click here to try
SQL to Python Series
Problem 7/50

SQL Solution
SQL to Python Series
Problem 7/50

Python Solution
SQL to Python Series
Problem 8/50

Write a solution to find the IDs of the users who visited


without making any transactions and the number of times
they made these types of visits.
Click here to try
SQL to Python Series
Problem 8/50

SQL Solution
SQL to Python Series
Problem 8/50

Python Solution
SQL to Python Series
Problem 9/50

Write a solution to find all dates' id with higher


temperatures compared to its previous dates (yesterday).
Click here to try
SQL to Python Series
Problem 9/50

SQL Solution
SQL to Python Series
Problem 9/50

Python Solution
SQL to Python Series
Problem 10/50

Write a solution to find the average time each machine


takes to complete a process.

Click here to try


SQL to Python Series
Problem 10/50

SQL Solution Using Lag


SQL to Python Series
Problem 10/50

SQL Solution Using Join


SQL to Python Series
Problem 10/50

Pandas Solution Using .shift()


SQL to Python Series
Problem 10/50

Pandas Solution Using .diff()


SQL to Python Series
Problem 11/50

Write a solution to report the name and bonus


amount of each employee with a bonus less than
1000.
Return the result table in any order.
Click here to try
SQL to Python Series
Problem 11/50

SQL Solution
SQL to Python Series
Problem 11/50

Pandas Solution Using .merge()


SQL to Python Series
Problem 12/50

Write a solution to
find the number of
times each
student attended
each exam.

Return the result


table ordered by
student_id and
subject_name.
Click here to try
SQL to Python Series
Problem 12/50

SQL Solution
SQL to Python Series
Problem 12/50

Pandas Solution Using .merge()


SQL to Python Series
Problem 13/50

Write a solution to find manager name with at


least five direct reports.

Click here to try


SQL to Python Series
Problem 13/50

SQL Solution using Subquery


SQL to Python Series
Problem 13/50

SQL Solution using Self Join


SQL to Python Series
Problem 13/50

Python Solution using .isin()


SQL to Python Series
Problem 13/50

Python Solution using .merge()


SQL to Python Series
Problem 14/50
Click here to try

Write a solution to find the


confirmation rate of each
user.
Return the result table in
any order.
The confirmation rate of a user is the number of 'confirmed' messages divided by the total numbe
of requested confirmation messages. The confirmation rate of a user that did not request any
confirmation messages is 0. Round the confirmation rate to two decimal places.
SQL to Python Series
Problem 14/50

SQL Solution
SQL to Python Series
Problem 14/50

Python Solution
SQL to Python Series
Problem 15/50

Write a solution to report the movies with an odd-numbered ID


and a description that is not "boring".

Return the result table ordered by rating in descending order.


Click here to try
SQL to Python Series
Problem 15/50

SQL Solution
SQL to Python Series
Problem 15/50

Python Solution
SQL to Python Series
Problem 16/50

Write a solution to find the average selling price for each


product. average_price should be rounded to 2 decimal places.

Return the result table in any order.


Click here to try
SQL to Python Series
Problem 16/50

SQL Solution
SQL to Python Series
Problem 16/50

Python Solution
SQL to Python Series
Problem 17/50

Write an SQL query that reports the average experience years


of all the employees for each project, rounded to 2 digits.

Return the result table in any order.


Click here to try
SQL to Python Series
Problem 17/50

SQL Solution
SQL to Python Series
Problem 17/50

Python Solution
SQL to Python Series
Problem 18/50

Calculate the percentage of users registered in each contest,


rounded to two decimal places.
Order the result by percentage in descending order. In case of
a tie, order by contest_id in ascending order.
Click here to try
SQL to Python Series
Problem 18/50

Using subquery in select is not considered as a best


practice so for alternate solution check next page.

SQL Solution using Subquery


SQL to Python Series
Problem 18/50

SQL Solution using subquery and


Join
SQL to Python Series
Problem 18/50

SQL Solution using CTE and Join


SQL to Python Series
Problem 18/50

Python Solution
SQL to Python Series
Problem 19/50

Write a solution to find each query_name, the quality and


poor_query_percentage.

Both quality and poor_query_percentage should be rounded to


2 decimal places.

Click here to try


SQL to Python Series
Problem 19/50

SQL Solution
SQL to Python Series
Problem 19/50

Python Solution
SQL to Python Series
Problem 20/50

Write an SQL query to find for each month and country, the
number of transactions and their total amount, the number of
approved transactions and their total amount.

Click here to try


SQL to Python Series
Problem 20/50

SQL Solution
SQL to Python Series
Problem 20/50

Python Solution using .assign() in


a single chained expression
SQL to Python Series
Problem 21/50

If the customer's preferred delivery date is the same as the order date,
then the order is called immediate; otherwise, it is called scheduled.

The first order of a customer is the order with the earliest order date that
the customer made. It is guaranteed that a customer has precisely one
first order.

Write a solution to find the percentage of immediate orders in the first


orders of all customers, rounded to 2 decimal places.
Click here to try
SQL to Python Series
Problem 21/50

SQL Solution
SQL to Python Series
Problem 21/50

Python Solution
SQL to Python Series
Problem 22/50

Write a solution to report the fraction of players that logged in again on


the day after the day they first logged in, rounded to 2 decimal places.

In other words, you need to count the number of players that logged in
for at least two consecutive days starting from their first login date,
then divide that number by the total number of players.
Click here to try
SQL to Python Series
Problem 22/50

SQL Solution
SQL to Python Series
Problem 22/50

Python Solution using


.transform()
SQL to Python Series
Problem 23/50

Write a solution to calculate the number of unique


subjects each teacher teaches in the university.

Click here to try


SQL to Python Series
Problem 23/50

SQL Solution
SQL to Python Series
Problem 23/50

Python Solution
SQL to Python Series
Problem 24/50

Write a solution to find the daily active user count


for a period of 30 days ending 2019-07-27
inclusively. A user was active on someday if they
made at least one activity on that day.
Click here to try
SQL to Python Series
Problem 24/50

SQL Solution
SQL to Python Series
Problem 24/50

Python Solution using


pd.Timedelta()
SQL to Python Series
Problem 25/50

Write a solution to select the product id, year,


quantity, and price for the first year of every
product sold.

Click here to try


SQL to Python Series
Problem 25/50

SQL Solution
SQL to Python Series
Problem 25/50

Python Solution using


.rank()
SQL to Python Series
Problem 26/50

Write a solution to find all the classes that


have at least five students.
Click here to try
SQL to Python Series
Problem 26/50

SQL Solution
SQL to Python Series
Problem 26/50

Python Solution using


.query()
SQL to Python Series
Problem 27/50

Write a solution that will, for each user,


return the number of followers.
Return the result table ordered by user_id
in ascending order.
Click here to try
SQL to Python Series
Problem 27/50

SQL Solution
SQL to Python Series
Problem 27/50

Python Solution
SQL to Python Series
Problem 28/50

Write a solution
to find the
largest number
that appears
only once in the
table.

If there is no
single number,
return null.

Click here to try


SQL to Python Series
Problem 28/50

SQL Solution
SQL to Python Series
Problem 28/50

Python Solution
SQL to Python Series
Problem 29/50

Write a solution to
report the customer ids
from the Customer table
that bought all the
products in the Product
table.
Click here to try
SQL to Python Series
Problem 29/50

SQL Solution
SQL to Python Series
Problem 29/50

Python Solution
SQL to Python Series
Problem 30/50

Write a solution to report the ids and the names of all


managers, the number of employees who report directly to
them, and the average age of the reports rounded to the
nearest integer. Return the result table ordered by
employee_id.
Click here to try
SQL to Python Series
Problem 30/50

SQL Solution
SQL to Python Series
Problem 30/50

Python Solution
SQL to Python Series
Problem 31/50

Write a solution to report all the employees with their


primary department. For employees who belong to one
department, report their only department.
Click here to try
SQL to Python Series
Problem 31/50

SQL Solution
SQL to Python Series
Problem 31/50

Python Solution
SQL to Python Series
Problem 31/50

Python Solution
SQL to Python Series
Problem 32/50

Write a solution to determine whether three line segments


can form a triangle.
Click here to try
SQL to Python Series
Problem 32/50

SQL Solution
SQL to Python Series
Problem 32/50

Python Solution
SQL to Python Series
Problem 33/50

Find all numbers that appear at least


three times consecutively.
Click here to try
SQL to Python Series
Problem 33/50

SQL Solution
SQL to Python Series
Problem 33/50

Python Solution
SQL to Python Series
Problem 34/50

Write a solution to find the prices of all products on


2019-08-16. Assume the price of all products before
any change is 10.
Click here to try
SQL to Python Series
Problem 34/50

SQL Solution
SQL to Python Series
Problem 34/50

Python Solution
SQL to Python Series
Problem 35/50

Write a solution to find the person_name of the last


person that can fit on the bus without exceeding the
weight limit(1000). The test cases are generated such
that the first person does not exceed the weight limit.
Click here to try
SQL to Python Series
Problem 35/50

SQL Solution
SQL to Python Series
Problem 35/50

Python Solution
SQL to Python Series
Problem 36/50
The salary categories are:

"Low Salary": All the salaries


strictly less than $20000.

"Average Salary": All the


salaries in the inclusive
range [$20000, $50000].

"High Salary": All the salaries


strictly greater than $50000.

Write a solution to calculate the number of bank accounts


for each salary category.
The result table must contain all three categories. If there
are no accounts in a category, return 0.
Click here to try
SQL to Python Series
Problem 36/50

SQL Solution
SQL to Python Series
Problem 36/50

Python Solution
SQL to Python Series
Problem 37/50

Find the IDs of the employees whose salary is strictly less


than $30000 and whose manager left the company. When a
manager leaves the company, their information is deleted
from the Employees table, but the reports still have their
manager_id set to the manager that left.
Click here to try
SQL to Python Series
Problem 37/50

SQL Solution
SQL to Python Series
Problem 37/50

Python Solution
SQL to Python Series
Problem 38/50

Seat Table: Contains the ID and name of each student. The IDs
are continuous increments.
Swap the seat IDs for every pair of consecutive students. If
there’s an odd number of students, the last student’s ID
remains unchanged.
Return the result table ordered by ID in ascending order.
Click here to try
SQL to Python Series
Problem 38/50

SQL Solution
SQL to Python Series
Problem 38/50

SQL Solution
SQL to Python Series
Problem 38/50

Python Solution
SQL to Python Series
Problem 39/50

Write a solution to:


Find the name of the user who has rated the greatest number
of movies. In case of a tie, return the lexicographically smaller
user name.
Find the movie name with the highest average rating in
February 2020. In case of a tie, return the lexicographically
smaller movie name. Click here to try
SQL to Python Series
Problem 39/50

SQL Solution
SQL to Python Series
Problem 39/50

Python Solution
Alternate using pd.concat()
SQL to Python Series
Problem 40/50

Click here to try

Calculate the 7-day moving sum and the moving average of the
amount. The moving average should be rounded to two decimal
places.
Return the result table ordered by visited_on in ascending order.
SQL to Python Series
Problem 40/50

SQL Solution
SQL to Python Series
Problem 40/50

Python Solution
SQL to Python Series
Problem 41/50

Write a solution to find the people who have the


most friends and the most friends number.
The test cases are generated so that only one
person has the most friends. Click here to try
SQL to Python Series
Problem 41/50

SQL Solution
SQL to Python Series
Problem 41/50

Python Solution
SQL to Python Series
Problem 42/50

Write a solution to report the sum of all total investment


values in 2016 tiv_2016, for all policyholders who:
have the same tiv_2015 value as one or more other
policyholders, and
are not located in the same city as any other policyholder
(i.e., the (lat, lon) attribute pairs must be unique).
Round tiv_2016 to two decimal places. Click here to try
SQL to Python Series
Problem 42/50

SQL Solution
SQL to Python Series
Problem 42/50

Python Solution
SQL to Python Series
Problem 43/50

Find the employees with the top three unique


salaries in each department.
Click here to try
SQL to Python Series
Problem 43/50

SQL Solution
SQL to Python Series
Problem 43/50

Python Solution
SQL to Python Series
Problem 44/50

Write a solution to fix the names so that only the first


character is uppercase and the rest are lowercase.
Return the result table ordered by user_id.
Click here to try
SQL to Python Series
Problem 44/50

SQL Solution
SQL to Python Series
Problem 44/50

SQL Solution
SQL to Python Series
Problem 44/50

Python Solution
SQL to Python Series
Problem 45/50

Retrieve the patient ID, name, and conditions


for patients diagnosed with Type I Diabetes,
identified by the 'DIAB1' prefix. Click here to try
SQL to Python Series
Problem 45/50

SQL Solution
SQL to Python Series
Problem 45/50

SQL Solution
SQL to Python Series
Problem 45/50

Python Solution
SQL to Python Series
Problem 46/50

Write a solution to delete all duplicate emails,


keeping only one unique email with the smallest id.
For SQL users, please note that you are supposed to write a DELETE statement
and not a SELECT one.
For Pandas users, please note that you are supposed to modify Person in place.
Click here to try
SQL to Python Series
Problem 46/50

SQL Solution
SQL to Python Series
Problem 46/50

Python Solution
SQL to Python Series
Problem 47/50

Write a solution to find the second highest distinct


salary from the Employee table. If there is no second
highest salary, return null (return None in Pandas).
Click here to try
SQL to Python Series
Problem 47/50

SQL Solution
SQL to Python Series
Problem 47/50

Python Solution
SQL to Python Series
Problem 48/50

Write a solution to find for each date the number of


different products sold and their names.
The sold products names for each date should be sorted
lexicographically. Click here to try
SQL to Python Series
Problem 48/50

SQL Solution
SQL to Python Series
Problem 48/50

Python Solution
SQL to Python Series
Problem 49/50

Write a solution to get the names of products that have


at least 100 units ordered in February 2020 and their
amount. Click here to try
SQL to Python Series
Problem 49/50

SQL Solution
SQL to Python Series
Problem 49/50

Python Solution
SQL to Python Series
Problem 50/50

Write a solution to find the users who have valid emails.


A valid e-mail has a prefix name and a domain where:
The prefix name is a string that may contain letters (upper or
lower case), digits, underscore '_', period '.', and/or dash '-'. The
prefix name must start with a letter.
The domain is '@leetcode.com'.
Click here to try
SQL to Python Series
Problem 50/50

SQL Solution
SQL to Python Series
Problem 50/50

Python Solution

You might also like