Browse Source

that's a wrap

Jared Dunbar 2 years ago
parent
commit
5a4c1419b4
Signed by: Jared Dunbar <jrddunbr@gmail.com> GPG Key ID: CF202CC859BAC692
1 changed files with 220 additions and 0 deletions
  1. 220
    0
      11-7-18.sql

+ 220
- 0
11-7-18.sql View File

@@ -0,0 +1,220 @@
1
+-- aggregate functions
2
+
3
+-- COUNT()
4
+
5
+-- we're using the ZAGI database (and associated example commands)
6
+
7
+-- number of rows (* can also be replaced with a primary key?)
8
+COUNT(*)
9
+
10
+-- number of entries
11
+COUNT(field)
12
+
13
+-- ZAGI 13
14
+COUNT (*)
15
+FROM product;
16
+
17
+-- returns number or rows
18
+
19
+-- 15
20
+
21
+-- returns count, avg, min, max
22
+
23
+-- 16
24
+
25
+-- for each vendor id, get total number of product, and the average product price, sorted by vendor id.
26
+
27
+-- returns vid, count, avg
28
+
29
+
30
+-- GROUP BY xxxxx
31
+-- must have xxxx in the select statement, otherwise we can get errors
32
+
33
+-- 17 is highly un-recommended. it doesn't indicate what the data is sorted by (context)
34
+
35
+-- 18
36
+
37
+-- returns vid and count of products over 100$
38
+
39
+-- 19
40
+
41
+-- average product prices by category
42
+
43
+-- 20
44
+
45
+-- retunrs productid and the number of items sold
46
+
47
+-- SUM()
48
+
49
+-- shows a sum of the items
50
+
51
+-- 21
52
+
53
+-- shows number of transactions for each one, grouped by product
54
+
55
+-- HAVING
56
+-- similar to WHERE, used for a grouping feature, generally found after a GROUP BY statement
57
+
58
+-- enhanced query based on 28
59
+
60
+SELECT productid, productname, productprice, (
61
+  SELECT AVG(productprice) FROM product
62
+) AS avg_price
63
+FROM product
64
+WHERE productprice < (
65
+  SELECT AVG (productprice)
66
+  FROM product
67
+);
68
+
69
+-- notice that we can't just do WHERE productprice < avg_price. That's a bit derp. Some SQL db's can handle that, but ours (mySQL?) appears that it cannot
70
+-- switching to SQL zoo after q30
71
+
72
+
73
+
74
+
75
+
76
+-- dataset is now world(name, continent, area, population, gdp)
77
+
78
+-- q2 show the countries with a per capita GDP greater than the UK
79
+SELECT name
80
+FROM world
81
+WHERE continent = 'Europe'
82
+AND gdp/population > (
83
+  SELECT gdp/population AS percapitagdp
84
+  FROM world
85
+  WHERE name = 'United Kingdom'
86
+);
87
+
88
+-- he did this first:
89
+SELECT gdp/population AS percapitagdp
90
+FROM world
91
+WHERE name = 'United Kingdom';
92
+
93
+-- q3 List the names and continent of countries in the continents containng either Argentena or Australia, and order by country
94
+SELECT name, continent
95
+FROM world
96
+WHERE continent IN (
97
+  SELECT continent
98
+  FROM world
99
+  WHERE name IN ('Argentena', 'Australia')
100
+)
101
+ORDER BY name;
102
+
103
+-- he did this first
104
+SELECT continent
105
+FROM world
106
+WHERE name IN ('Argentena', 'Australia');
107
+
108
+-- q4 Which country has a population that is more than Canada and less than poland? Show the name and the population.
109
+SELECT name, country
110
+FROM world
111
+WHERE population > (
112
+  SELECT population
113
+  FROM world
114
+  WHERE name = 'Canada'
115
+)
116
+AND population < (
117
+  SELECT population
118
+  FROM world
119
+  WHERE name = 'Poland'
120
+);
121
+
122
+-- I came up with these first. He started with the external query first
123
+SELECT population
124
+FROM world
125
+WHERE name = 'Canada';
126
+
127
+SELECT population
128
+FROM world
129
+WHERE name = 'Poland';
130
+
131
+-- show the inner query results for more context as to how the numbers compares
132
+SELECT name, country, (
133
+  SELECT population
134
+  FROM world
135
+  WHERE name = 'Canada'
136
+) as popCanada, (
137
+  SELECT population
138
+  FROM world
139
+  WHERE name = 'Poland'
140
+) as popPoland
141
+FROM world
142
+WHERE population > (
143
+  SELECT population
144
+  FROM world
145
+  WHERE name = 'Canada'
146
+)
147
+AND population < (
148
+  SELECT population
149
+  FROM world
150
+  WHERE name = 'Poland'
151
+);
152
+
153
+-- q5 Germany, population 80 million has the largest population of the countries in Europe.
154
+-- Show the name and population of each country in Europe
155
+-- Show the population as a percentage of the population of Germany
156
+
157
+-- I made this first
158
+SELECT population
159
+FROM world
160
+WHERE name = 'Germany';
161
+
162
+-- first round
163
+SELECT name, population/(
164
+  SELECT population
165
+  FROM world
166
+  WHERE name = 'Germany'
167
+) AS popPctgOfGermany
168
+FROM world
169
+WHERE continent = 'Europe';
170
+
171
+-- second round - add rounding statement and multiply by 100 to get percentage
172
+SELECT name, ROUND (
173
+  100 * population/(
174
+    SELECT population
175
+    FROM world
176
+    WHERE name = 'Germany'
177
+  ), 0 -- number of decimal places
178
+) AS popPctgOfGermany
179
+FROM world
180
+WHERE continent = 'Europe';
181
+
182
+-- third round - add percent symbol with || '%'
183
+SELECT name, ROUND (
184
+  100 * population/(
185
+    SELECT population
186
+    FROM world
187
+    WHERE name = 'Germany'
188
+  ), 0 -- number of decimal places
189
+) || '%' AS popPctgOfGermany
190
+FROM world
191
+WHERE continent = 'Europe';
192
+
193
+-- q6 Which countries have a GDP greater than every contry in Europe (name only, some countries may have NULL GDP values)
194
+-- we need to find the greatest GDP in Europe as well
195
+SELECT name
196
+FROM world
197
+WHERE gdp > (
198
+  SELECT MAX(gdp)
199
+  FROM world
200
+  WHERE continent = 'Europe'
201
+)
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+--

Loading…
Cancel
Save