-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice on case.sql
More file actions
129 lines (109 loc) · 3.19 KB
/
practice on case.sql
File metadata and controls
129 lines (109 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-- case:
/*
if (condition , true, false)
if (conditon, true , if (condition, true, if)
case:
when condition | expression then output
when condition, then output
end
True --> 1
False --> 0
*/
use sakila;
select * from employees;
select first_name , department, salary, if (department = 'IT' , True, False) from employees;
select first_name, department, salary, if(department= 'IT', salary*1.5, salary*1.05) from employees;
select first_name, department, salary,
if(department = 'IT', salary*1.1, if(department= 'Hr', salary*0.5, salary)) from employees;
-- case statement
/*
select col1, col2
case
when condition then statement
end
from table_name
*/
select first_name, department, salary,
case
when department = 'IT' then True
else False
end
from employees;
select first_name, department, salary,
case
when department = 'IT' or department = 'HR' then 'A category'
else 'B category'
end
from employees;
select first_name, department, salary,
case
when department = 'IT' then salary*1.1
when department = 'IT' then salary*1.05
else salary
end as 'New salary'
from employees;
select * from employees;
-- if the hiring date of an employee is before 2020 year print job_title with senior if 2021 print associate
select first_name, job_title, year(hire_date),
case
when year(hire_date) <2020 then CONCAT('senior ', job_title)
when year(hire_date) = 2021 then CONCAT('associate ', job_title)
else job_title
end as title
from employees;
-- if salary >70000, create the category name as avg salary if the salary is > 85000 high salary
-- if salary >100000 then extreme high salary othewise print the category name as low salary
SELECT first_name, salary,
CASE
WHEN salary > 100000 THEN 'extreme high salary'
WHEN salary > 85000 THEN 'high salary'
WHEN salary > 70000 THEN 'avg salary'
ELSE 'low salary'
END AS update_slry
FROM employees;
-- select the job_title and print the column value based on the following cond
SELECT job_title,
CASE
WHEN COUNT(*) = 1 THEN CONCAT(job_title, ' has 1 person')
ELSE CONCAT(job_title, ' has ', COUNT(*), ' persons')
END AS job_info
FROM employees
GROUP BY job_title;
use world;
select * from country;
select name, population,
case
when population = 0 then 'no population'
when population between 8000 and 70000 then 'med population'
else 'condition is false'
end from world .country;
select name, population,
case
when population = 0 then 'no population'
when population between 8000 and 70000 then 'med population'
else 'condition is false'
end as 'status' from world.country;
-- case + group by
select count(*),
case
when population = 0 then 'no population'
when population between 8000 and 70000 then 'med population'
else 'condition is false'
end as 'status' from world.country
group by status;
-- how many countries which have population 8000 and 70000
-- by using count
select continent ,
count(case
when population between 8000 and 70000 then 1
else 0
end )
from world.country
group by continent;
-- by using sum
select continent ,
sum(case
when population between 8000 and 70000 then 1
else 0
end )
from world.country