Top 15 SQL LeetCode Questions for Data Engineering Interviews

Listen to this Post

Featured Image
LeetCode is a popular platform for practicing coding problems, especially for technical interviews. For Data Engineering roles, SQL is a critical skill, and mastering LeetCode’s SQL questions can significantly boost your interview performance. Below are 15 essential SQL questions to focus on, along with practical solutions and commands.

1. Big Countries

Problem: Find countries with an area of at least 3 million or a population of at least 25 million.

Solution:

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

2. Employee Bonus

Problem: Select employees with a bonus less than 1000.

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; 

3. Delete Duplicate Emails

Problem: Delete duplicate emails, keeping only the one with the smallest ID.

Solution:

DELETE p1 FROM Person p1, Person p2 
WHERE p1.email = p2.email AND p1.id > p2.id; 
  1. List the Products Ordered in a Period
    Problem: Find products ordered in February 2020 with at least 100 units.

Solution:

SELECT p.product_name, SUM(o.unit) AS unit 
FROM Products p 
JOIN Orders o ON p.product_id = o.product_id 
WHERE o.order_date BETWEEN '2020-02-01' AND '2020-02-29' 
GROUP BY p.product_name 
HAVING SUM(o.unit) >= 100; 

5. Find Followers Count

Problem: Count followers for each user.

Solution:

SELECT user_id, COUNT(follower_id) AS followers_count 
FROM Followers 
GROUP BY user_id 
ORDER BY user_id; 

6. Product Sales Analysis III

Problem: Find the first year of sales for each product.

Solution:

SELECT product_id, year AS first_year, quantity, price 
FROM Sales 
WHERE (product_id, year) IN ( 
SELECT product_id, MIN(year) 
FROM Sales 
GROUP BY product_id 
); 

7. Recyclable and Low Fat Products

Problem: Find products that are both low fat and recyclable.

Solution:

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

8. Consecutive Numbers

Problem: Find numbers that appear at least three times consecutively.

Solution:

SELECT DISTINCT l1.num AS ConsecutiveNums 
FROM Logs l1, Logs l2, Logs l3 
WHERE l1.id = l2.id - 1 AND l2.id = l3.id - 1 
AND l1.num = l2.num AND l2.num = l3.num; 

9. Classes More Than 5 Students

Problem: Find classes with at least five students.

Solution:

SELECT class 
FROM Courses 
GROUP BY class 
HAVING COUNT(student) >= 5; 

10. Count Salary Categories

Problem: Categorize salaries into Low, Average, and High.

Solution:

SELECT 
'Low' AS category, 
COUNT() AS accounts_count 
FROM Accounts 
WHERE income < 20000 
UNION 
SELECT 
'Average' AS category, 
COUNT() AS accounts_count 
FROM Accounts 
WHERE income BETWEEN 20000 AND 50000 
UNION 
SELECT 
'High' AS category, 
COUNT() AS accounts_count 
FROM Accounts 
WHERE income > 50000; 

11. Average Selling Price

Problem: Calculate the average selling price per product.

Solution:

SELECT p.product_id, ROUND(SUM(p.price  u.units) / SUM(u.units), 2) AS average_price 
FROM Prices p 
JOIN UnitsSold u ON p.product_id = u.product_id 
WHERE u.purchase_date BETWEEN p.start_date AND p.end_date 
GROUP BY p.product_id; 

12. Monthly Transactions I

Problem: Summarize transactions by month and country.

Solution:

SELECT 
DATE_FORMAT(trans_date, '%Y-%m') AS month, 
country, 
COUNT() AS trans_count, 
SUM(IF(state = 'approved', 1, 0)) AS approved_count, 
SUM(amount) AS trans_total_amount, 
SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount 
FROM Transactions 
GROUP BY month, country; 

13. Group Sold Products By The Date

Problem: List products sold for each date.

Solution:

SELECT 
sell_date, 
COUNT(DISTINCT product) AS num_sold, 
GROUP_CONCAT(DISTINCT product ORDER BY product SEPARATOR ',') AS products 
FROM Activities 
GROUP BY sell_date 
ORDER BY sell_date; 

14. Second Highest Salary

Problem: Find the second-highest salary.

Solution:

SELECT MAX(salary) AS SecondHighestSalary 
FROM Employee 
WHERE salary < (SELECT MAX(salary) FROM Employee); 

15. Movie Rating

Problem: Find the user with the most movie ratings in February 2020.

Solution:

(SELECT u.name AS results 
FROM Users u 
JOIN MovieRating mr ON u.user_id = mr.user_id 
GROUP BY u.user_id 
ORDER BY COUNT() DESC, u.name ASC 
LIMIT 1) 
UNION 
(SELECT m.title AS results 
FROM Movies m 
JOIN MovieRating mr ON m.movie_id = mr.movie_id 
WHERE mr.created_at BETWEEN '2020-02-01' AND '2020-02-29' 
GROUP BY m.movie_id 
ORDER BY AVG(mr.rating) DESC, m.title ASC 
LIMIT 1); 

You Should Know:

  • SQL Optimization: Use `EXPLAIN` to analyze query performance.
  • Indexing: Improve query speed with CREATE INDEX idx_name ON table(column);.
  • CTEs (Common Table Expressions): Use `WITH` for complex queries.
  • Window Functions: Leverage RANK(), DENSE_RANK(), and ROW_NUMBER().
  • Data Cleaning: Use TRIM(), COALESCE(), and `REGEXP_REPLACE()` for preprocessing.

What Undercode Say:

Mastering SQL is essential for Data Engineering roles. These LeetCode questions cover key concepts like joins, aggregations, and subqueries. Practice them regularly and optimize your queries for better performance.

Expected Output:

  • Top SQL LeetCode Questions
  • Practical Solutions & Commands
  • Performance Optimization Tips

Prediction:

SQL will remain a fundamental skill for Data Engineers, with increasing emphasis on optimization and real-time querying in cloud environments like AWS Redshift and Google BigQuery.

Relevant URLs:

  1. Big Countries
  2. Employee Bonus
  3. Delete Duplicate Emails
  4. List the Products Ordered in a Period
  5. Find Followers Count

References:

Reported By: Sachincw Leetcode – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram