0% found this document useful (0 votes)
6 views15 pages

LeetCode SQL50 Complete Revision

Uploaded by

placehetvideo1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

LeetCode SQL50 Complete Revision

Uploaded by

placehetvideo1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

LeetCode SQL50 — Complete Revision Handbook

Last-Minute SQL Guide with Problems, Inputs, Outputs, Solutions & Explanations

Prepared for: Quick Revision • Format: 1 Problem/Page • Dialect: MySQL


Table of Contents
01. Recyclable and Low Fat Products ........ Page 3
02. Find Customer Referee ........ Page 4
03. Big Countries ........ Page 5
04. Article Views I ........ Page 6
05. Invalid Tweets ........ Page 7
06. Replace Employee ID With The Unique Identifier ........ Page 8
07. Product Sales Analysis I ........ Page 9
08. Customer Who Visited but Did Not Make Any Transactions ........ Page 10
09. Rising Temperature ........ Page 11
10. Average Time of Process per Machine ........ Page 12
11. Employee Bonus ........ Page 13
12. Students and Examinations ........ Page 14
13. Managers with at Least 5 Direct Reports ........ Page 15
Problem 1. Recyclable and Low Fat Products
Return product_id for products that are both low fat ('Y') and recyclable ('Y').

Input Example(s):
• Products

product_id low_fats recyclable


1 Y N
2 Y Y
3 N Y

Expected Output:
product_id
2

MySQL Solution:
SELECT product_id
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';

Explanation:
Filter rows using WHERE with AND to keep records satisfying both conditions.
Keywords: WHERE, AND, filtering
Problem 2. Find Customer Referee
Return names of customers whose referee_id is not 2 (or is NULL).

Input Example(s):
• Customer

id name referee_id
1 Will
2 Jane
3 Alex 2
4 Bill
5 Zack 1
6 Mark 2

Expected Output:
name
Will
Jane
Bill
Zack

MySQL Solution:
SELECT name
FROM Customer
WHERE referee_id IS NULL OR referee_id <> 2;

Explanation:
Use IS NULL to detect missing values; combine with <> 2 to exclude referrals by 2.
Keywords: IS NULL, <>, OR, WHERE
Problem 3. Big Countries
Return name, population, area for countries with area >= 3,000,000 or population >= 25,000,000.

Input Example(s):
• World

name continent area population gdp


Afghanistan Asia 652230 25500100 20343000000
Algeria Africa 2381741 37100000 188681000000

Expected Output:
name population area

MySQL Solution:
SELECT name, population, area
FROM World
WHERE area >= 3000000 OR population >= 25000000;

Explanation:
Basic filtering with numeric comparisons and OR.
Keywords: WHERE, OR, comparisons
Problem 4. Article Views I
Find authors (author_id as id) who viewed their own articles (author_id = viewer_id).

Input Example(s):
• Views

article_id author_id viewer_id view_date


1 3 5 2019-08-01
1 3 3 2019-08-02
2 7 7 2019-08-01

Expected Output:
id
3
7

MySQL Solution:
SELECT DISTINCT author_id AS id
FROM Views
WHERE author_id = viewer_id
ORDER BY id;

Explanation:
Self-views are rows where author_id equals viewer_id. DISTINCT removes duplicates.
Keywords: DISTINCT, WHERE, ORDER BY
Problem 5. Invalid Tweets
Return tweet_id where the tweet's content length is strictly greater than 15 characters.

Input Example(s):
• Tweets

tweet_id content
1 Vote for Biden
2 Let us make SQL fun again!
3 SQL is cool

Expected Output:
tweet_id
2

MySQL Solution:
SELECT tweet_id
FROM Tweets
WHERE CHAR_LENGTH(content) > 15;

Explanation:
Use CHAR_LENGTH to count characters (multi-byte safe).
Keywords: CHAR_LENGTH, WHERE, >
Problem 6. Replace Employee ID With The Unique Identifier
Return unique_id with employee name by joining mapping table on id.

Input Example(s):
• Employees

id name
1 Alice
7 Bob
11 Meir
90 Winston

• EmployeeUNI

id unique_id
1 101
7 102
11 103
90 104

Expected Output:
unique_id name
101 Alice
102 Bob
103 Meir
104 Winston

MySQL Solution:
SELECT u.unique_id, e.name
FROM Employees e
LEFT JOIN EmployeeUNI u
ON e.id = u.id;

Explanation:
LEFT JOIN preserves all employees; join picks the matching unique_id.
Keywords: LEFT JOIN, ON
Problem 7. Product Sales Analysis I
Return product_name, year, price by joining Sales with Product on product_id.

Input Example(s):
• Sales

seller_id product_id year price


1 100 2008 10
1 100 2009 12
2 200 2011 15

• Product

product_id product_name
100 Nokia
200 iPhone

Expected Output:
product_name year price
Nokia 2008 10
Nokia 2009 12
iPhone 2011 15

MySQL Solution:
SELECT p.product_name, s.year, s.price
FROM Sales s
JOIN Product p
ON s.product_id = p.product_id;

Explanation:
Inner join to enrich sales with product names.
Keywords: JOIN, ON, SELECT projection
Problem 8. Customer Who Visited but Did Not Make Any Transactions
For each customer, count visits that had no matching transaction.

Input Example(s):
• Visits

visit_id customer_id
1 23
2 9
4 30
5 54

• Transactions

transaction_id visit_id amount


2 5 310
3 5 300
9 4 200

Expected Output:
customer_id count_no_trans
23 1
9 1
30 0
54 0

MySQL Solution:
SELECT v.customer_id, COUNT(v.visit_id) AS count_no_trans
FROM Visits v
LEFT JOIN Transactions t
ON v.visit_id = t.visit_id
WHERE t.transaction_id IS NULL
GROUP BY v.customer_id;

Explanation:
LEFT JOIN + IS NULL to count visits that didn't join to a transaction.
Keywords: LEFT JOIN, IS NULL, GROUP BY, COUNT
Problem 9. Rising Temperature
Find IDs of days where temperature is higher than the previous day.

Input Example(s):
• Weather

id recordDate temperature
1 2015-01-01 10
2 2015-01-02 25
3 2015-01-03 20
4 2015-01-04 30

Expected Output:
id
2
4

MySQL Solution:
SELECT w1.id
FROM Weather w1
JOIN Weather w2
ON DATEDIFF(w1.recordDate, w2.recordDate) = 1
WHERE w1.temperature > w2.temperature;

Explanation:
Self-join adjacent days using DATEDIFF=1; compare temperatures.
Keywords: DATEDIFF, self JOIN, WHERE
Problem 10. Average Time of Process per Machine
Compute average (end - start) time per machine and round to 3 decimals.

Input Example(s):
• Activity

machine_id process_id activity_type timestamp


0 0 start 0.712
0 0 end 1.52
0 1 start 3.14
0 1 end 4.12

Expected Output:
machine_id processing_time
0 0.894

MySQL Solution:
SELECT machine_id, ROUND(AVG(end_time - start_time), 3) AS processing_time
FROM (
SELECT machine_id, process_id,
MAX(CASE WHEN activity_type='end' THEN timestamp END) AS end_time,
MAX(CASE WHEN activity_type='start' THEN timestamp END) AS start_time
FROM Activity
GROUP BY machine_id, process_id
) x
GROUP BY machine_id;

Explanation:
Pivot start/end per process via conditional aggregation, then average durations.
Keywords: CASE, MAX, AVG, ROUND, GROUP BY, subquery
Problem 11. Employee Bonus
List employee name and bonus for employees with bonus < 1000 or no bonus.

Input Example(s):
• Employee

empId name supervisor salary


3 Brad 4000
1 John 3 1000
2 Dan 3 2000

• Bonus

empId bonus
2 500

Expected Output:
name bonus
John
Brad
Dan 500

MySQL Solution:
SELECT e.name, b.bonus
FROM Employee e
LEFT JOIN Bonus b
ON e.empId = b.empId
WHERE b.bonus < 1000 OR b.bonus IS NULL;

Explanation:
LEFT JOIN to include all employees; filter small or missing bonuses.
Keywords: LEFT JOIN, IS NULL, OR, WHERE
Problem 12. Students and Examinations
For each student and subject combination, count attended exams.

Input Example(s):
• Students

student_id student_name
1 Alice
2 Bob

• Subjects

subject_name
Math
Physics

• Examinations

student_id subject_name
1 Math
1 Math
2 Physics

Expected Output:
student_id student_name subject_name attended_exams
1 Alice Math 2
1 Alice Physics 0
2 Bob Math 0
2 Bob Physics 1

MySQL Solution:
SELECT s.student_id, s.student_name, sub.subject_name,
COUNT(e.subject_name) AS attended_exams
FROM Students s
CROSS JOIN Subjects sub
LEFT JOIN Examinations e
ON s.student_id = e.student_id AND sub.subject_name = e.subject_name
GROUP BY s.student_id, s.student_name, sub.subject_name
ORDER BY s.student_id, sub.subject_name;

Explanation:
Form full grid via CROSS JOIN; LEFT JOIN counts matching exams per cell.
Keywords: CROSS JOIN, LEFT JOIN, COUNT, GROUP BY, ORDER BY
Problem 13. Managers with at Least 5 Direct Reports
Return managerId where at least 5 employees have that managerId.

Input Example(s):
• Employee

id name department managerId


101 John A
102 Dan A 101
103 James A 101
104 Amy A 101
105 Anne A 101
106 Ron A 101

Expected Output:
managerId
101

MySQL Solution:
SELECT managerId
FROM Employee
WHERE managerId IS NOT NULL
GROUP BY managerId
HAVING COUNT(*) >= 5;

Explanation:
Aggregate by managerId and keep groups with size >= 5.
Keywords: GROUP BY, HAVING, IS NOT NULL

You might also like