I faced a problem in the interview (it is over now).
There is table salary
like this:
ID SALARY
-----------
1 20000
2 30000
3 90000
...
I need to construct table which will classify salaries into groups and count number in each one of them. The classification is
if salary < 50000, then `low_salary`
if 50000 <= salary < 80000, then `medium_salary`
if salary >= 80000 then `high_salary`
The structure would be something like
Group_name cnt
-------------------
low_salary 20
medium_salary 40
high_salary 30
My solution was
WITH tmp AS
(
SELECT
id,
CASE
WHEN salary < 50000 THEN 'low_salary'
WHEN salary >= 50000 AND salary < 80000 THEN 'medium_salary'
ELSE 'high_salary'
END AS group_name
FROM
salary
)
SELECT group_name, COUNT(id)
FROM tmp
GROUP BY group_name
I used a CTE because I was not sure that I would be able to perform GROUP BY
by the aliased CASE
column, defined in select (please specify if that is true).
The interviewers said that it would be correct, however it is too complex, and there are two “conceptually simpler” solutions. They didn’t disclose it. So how could I do it “conceptually” simpler? I suppose it means not using GROUP BY
and CASE
since they were surprised to see it.
So I wonder if there really is a much simpler solution….
6
Solution without CASE or GROUP. Works for SQL-Server, MySQL, MariaDB, and Postgress.
SELECT 'low_salary' as group_name, COUNT(*) as GroupCount FROM salary WHERE salary < 50000
UNION ALL
SELECT 'Medium_salary' as group_name, COUNT(*) as GroupCount FROM salary WHERE salary between 50000 and 80000
UNION ALL
SELECT 'high_salary' as group_name, COUNT(*) as GroupCount FROM salary WHERE salary > 80000
fiddle
group_name | GroupCount |
---|---|
low_salary | 2 |
Medium_salary | 0 |
high_salary | 1 |
4
The best solution put this is one row instead of three and uses conditional aggregation:
SELECT
sum(case when salary < 50000 then 1 end) low_salary,
sum(case when salary >= 50000 and salaray <80000 then 1 end) medium_salary,
sum(case when salary >= 80000 then 1 end) high_salaray
FROM salary
This will tend to perform better and clearly less code.
1
select
case
when salary < 50000 then ‘low_salary’
when salary >=50000 AND salary < 80000 then ‘medium_salary’
else ‘high_salary’
end as salary_type
count(*) as count
from salary
group by salary_type;
Gunjan Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1