-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsum_and_count.sql
155 lines (58 loc) · 2.28 KB
/
sum_and_count.sql
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
SUM and COUNT
Language: English • 中文
Contents [hide]
1 World Country Profile: Aggregate functions
2 You might want to look at these examples first
3 Total world population
4 List of continents
5 GDP of Africa
6 Count the big countries
7 Baltic states population
8 Using GROUP BY and HAVING
9 Counting the countries of each continent
10 Counting big countries in each continent
11 Counting big continents
World Country Profile: Aggregate functions
This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5 to deliver the single value 11.
name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000
...
You might want to look at these examples first
Using SUM, Count, MAX, DISTINCT and ORDER BY.
Total world population
1.
Show the total population of the world.
world(name, continent, area, population, gdp)
select SUM(population)
from world
List of continents
2.
List all the continents - just once each.
select distinct continent from world
3.
Give the total GDP of Africa
select sum(gdp) from world where continent='Africa'
4.
How many countries have an area of at least 1000000
select count(name) from world where area >= 1000000
Baltic states population
5.
What is the total population of ('Estonia', 'Latvia', 'Lithuania')
select sum(population) from world where name in ('Estonia', 'Latvia', 'Lithuania')
Counting the countries of each continent
6.
For each continent show the continent and number of countries.
select continent,count(name) from world group by continent
Counting big countries in each continent
7.
For each continent show the continent and number of countries with populations of at least 10 million.
select continent,count(name) from world where population >=10000000 group by continent
Counting big continents
8.
List the continents that have a total population of at least 100 million.
select continent from world group by continent having
sum(population) >=100000000