Solutions for SQL 50 Study Plan on LeetCode
1757 - Recyclable and Low Fat Products
SELECT product_id
FROM Products
WHERE low_fats = 'Y'
AND recyclable = 'Y'
584 - Find Customer Referee
SELECT name
FROM Customer
WHERE referee_id != 2 OR referee_id IS null
595 - Big Countries
SELECT name, population, area
FROM WORLD
WHERE area >= 3000000
OR population >= 25000000
1148 - Article Views I
SELECT DISTINCT author_id as id
FROM Views
WHERE viewer_id >= 1
AND author_id = viewer_id
ORDER BY author_id
1683 - Invalid Tweets
SELECT tweet_id
FROM Tweets
WHERE length(content) > 15
1378 - Replace Employee ID With The Unique Identifier
SELECT unique_id, name
FROM Employees e
LEFT JOIN EmployeeUNI eu
ON e.id = eu.id
1068 - Product Sales Analysis I
SELECT product_name, year, price
FROM Sales s
LEFT JOIN Product p
ON s.product_id = p.product_id
1581 - Customer Who Visited but Did Not Make Any Transactions
SELECT customer_id, COUNT(*) as count_no_trans
FROM Visits
WHERE visit_id NOT IN (SELECT DISTINCT visit_id FROM Transactions)
GROUP BY customer_id
- Rising Temperature
SELECT w1.id
FROM Weather w1, Weather w2
WHERE DATEDIFF(w1.recordDate, w2.recordDate) = 1
AND w1.temperature > w2.temperature
-- OR
SELECT w1.id
FROM Weather w1, Weather w2
WHERE w1.temperature > w2.temperature
AND SUBDATE(w1.recordDate, 1) = w2.recordDate
- Average Time of Process per Machine
SELECT machine_id, ROUND(AVG(end - start), 3) AS processing_time
FROM
(SELECT machine_id, process_id,
MAX(CASE WHEN activity_type = 'start' THEN timestamp END) AS start,
MAX(CASE WHEN activity_type = 'end' THEN timestamp END) AS end
FROM Activity
GROUP BY machine_id, process_id) AS subq
GROUP BY machine_id
- Employee Bonus
SELECT name, bonus
FROM Employee e
LEFT JOIN Bonus b
ON e.empId = b.empId
WHERE bonus < 1000
OR bonus IS NULL
- Students and Examinations
SELECT a.student_id, a.student_name, b.subject_name, COUNT(c.subject_name) AS attended_exams
FROM Students a
JOIN Subjects b
LEFT JOIN Examinations c
ON a.student_id = c.student_id
AND b.subject_name = c.subject_name
GROUP BY 1, 3
ORDER BY 1, 3