1907. Count Salary Categories | Using Union Operator |
SheCodes SheCodes
409 subscribers
52 views
11

 Published On Oct 3, 2024

💼 In this video, we’ll solve the LeetCode SQL problem 'Count Salary Categories'. We will classify bank accounts into three salary categories—Low, Average, and High Salary—based on income, and count how many accounts fall into each category. This problem demonstrates the use of conditional aggregation in SQL, where you can apply IF() statements inside aggregate functions like COUNT() to group and classify data.

📚 What you'll learn in this video:

How to use conditional logic inside COUNT() with IF() in SQL.
Using UNION ALL to combine multiple result sets.
Categorizing data based on ranges and counting the number of entries per category.
💡 Problem Overview: Given a table of account incomes, our goal is to classify each account into one of three categories:

Low Salary: Incomes less than $20,000.
Average Salary: Incomes between $20,000 and $50,000 (inclusive).
High Salary: Incomes greater than $50,000.
The task is to return a result with the number of accounts in each salary category.


💻We’ll walk through how this query works:
The IF() statement is used to classify each row based on the income column.
COUNT() is used to count the number of rows in each salary range.
UNION ALL combines the three categories into a single result set, ensuring all salary categories (Low, Average, High) are included, even if no accounts fall into a particular range.

🔗 Related SQL Concepts:

Conditional Aggregation with IF() and COUNT()
Using UNION ALL to combine query results
Grouping and Categorizing Data in SQL

👉 Watch the full video to see how to structure the query, run it on sample data, and get the desired results!

🔔 Don’t forget to like, subscribe, and hit the bell icon for more SQL tutorials and interview preparation tips!

#SQL #LeetCode #SalaryCategories #SQLQueries #ConditionalAggregation #Database #Programming #TechTutorials #InterviewPreparation #SQLProblems

show more

Share/Embed