Introduction
Hi coders,
In this blog, we will tackle SQL questions from LeetCode. If you're a coder, a computer science student, or simply interested in coding, you may be familiar with LeetCode. It's a platform where coding enthusiasts practice problems to enhance their critical thinking and problem-solving skills. Most of us practice programming questions on leetcode so here we practice SQL questions which is taken from leetcode.
We'll start with basic SQL questions focused on SELECT
,
joins, and aggregate functions. Once we've covered these foundational topics,
we'll transition to more advanced concepts. Before diving into the questions,
ensure you have a basic understanding of SQL syntax and statements, including SELECT
,
various joins (self join, left join, right join, outer join), and basic
aggregate functions.
Here are some SQL interview questions with solution:
Q1 1757. Recyclable and Low Fat Products
Explanation: This query retrieves the product_id
of all products that are both low in fat (low_fats
= 'Y'
) and recyclable (recyclable
= 'Y'
).
Q2 584. Find Customer Referee
Explanation:
Method 1: The COALESCE
function
replaces NULL
values in
the referee_id
column
with 0
and ensures that
any referee with referee_id
not
equal to 2
is selected.
Method 2: This method checks for customers whose referee_id
is
not 2
or is NULL
.
Method 3: This method is similar to the first one but
uses IFNULL
to
replace NULL
values
with 0
.
Method 4: This method finds all customer names whose id
is
not in the list of customers having referee_id
= 2
.
Q3 595. Big Countries
Explanation: This query selects the names of countries with
an area greater than or equal to 3 million (3000000
)
square kilometers or a population greater than or equal to 25 million (25000000
).
Q4 1148. Article Views I
Explanation: This query retrieves unique author_id
values where the author is also the viewer of their article, ordered by id
in ascending order.
Q5 1683. Invalid Tweets
Explanation: This query retrieves the tweet_id
of tweets whose content length exceeds 15 characters.
Q6 1378. Replace Employee ID With The Unique Identifier
Explanation:
Method 1: This query joins the Employees
table with the EmployeeUNI
table
on the id
column, returning the
employee's name
along with
their unique_id
.
Method 2: This is an alternative to the previous query,
using the USING(id)
syntax to
simplify the join condition.
Q7 1068. Product Sales Analysis I
Explanation: This query joins the Sales
and Product
tables on product_id
to retrieve the product_name
, year
,
and price
for each sale.
Q8 1581. Customer Who Visited but Did Not Make Any Transactions
Explanation: This query counts how many times each customer
visited without making any transactions by using a LEFT
JOIN
between the Visits
and Transactions
tables.
Explanation: This method checks for visit_id
values that are not present in the Transactions
table and counts the number of visits for each customer.
Explanation: This method uses the NOT EXISTS
clause to count visits that have no corresponding transaction.
Q9 197. Rising Temperature
Explanation: This query compares temperatures recorded on consecutive days and selects the day where the temperature was higher than the previous day.
Q10 1661. Average Time of Process Per Machine
Explanation: This method uses a self-join to calculate the
average processing time by subtracting the start
timestamp from the end
timestamp.
Explanation: This method uses subqueries to calculate the
average timestamps for start
and end
activities and then subtracts them to get the processing time.
Q11 577. Employee Bonus
Explanation: This query retrieves the names of employees
who either have no bonus (NULL
)
or a bonus less than 1000.
Q12 1280. Students and Examinations
Explanation: This query retrieves each student's student_id
,
student_name
, the subject_name
,
and the count of exams they have attended for each subject.
Q13 570. Managers with at Least 5 Direct Reports
Explanation:
Method 1: This method counts the number of direct reports each manager has, selecting managers with at least 5 direct reports.
Method 2: This method uses a subquery to find the managerId
values that have 5 or more direct reports and retrieves the corresponding
manager names.
Q14 1934. Confirmation Rate
Explanation: This query calculates the confirmation rate
for each user by averaging the action
values in the confirmations
table. If the action
is
'confirmed', the value is 1
,
otherwise 0
.
Conclusion
In this blog, we've walked through a variety of SQL questions from LeetCode,
covering both basic and intermediate topics. By practicing queries that involve
SELECT
statements, joins,
and aggregate functions, you're building a solid foundation in SQL. As you
advance through more complex scenarios, your problem-solving skills and
understanding of database operations will deepen.
These SQL questions are a great way to sharpen your skills, especially if you're preparing for coding interviews or want to improve your ability to handle real-world data challenges. Continue practicing, explore more advanced topics like subqueries and window functions, and you'll find yourself becoming more confident in writing efficient and optimized SQL queries.
Happy coding!
Comments
Post a Comment