-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice on case.sql
More file actions
61 lines (50 loc) · 1.23 KB
/
practice on case.sql
File metadata and controls
61 lines (50 loc) · 1.23 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
-- 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 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
group by continent;