Browse Source

done for today I think

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

+ 71
- 7
11-5-18.sql View File

@@ -1,27 +1,31 @@
1 1
 -- in class examples
2 2
 
3
+-- dataset is employee(name)
4
+
3 5
 -- first, always see the data you are working with
4 6
 SELECT * FROM employee;
5 7
 
6 8
 -- then, see the size of the data
7 9
 SELECT COUNT(*) FROM employee;
8 10
 
11
+-- dataset is nobel(yr, subject, winner) (where winner is name)
12
+
13
+-- q1 show winners of the nobel prize in 1950
9 14
 SELECT yr, subject, winner FROM nobel
10 15
 WHERE yr = 1950;
11 16
 
17
+-- q2 show 1962 Literature prize winners
12 18
 SELECT winner
13 19
 FROM nobel
14
-WHERE yr = 1950
20
+WHERE yr = 1950winner, subject, year
15 21
 AND subject = 'Literature';
16 22
 
23
+-- q3 find nobel prizes that Albert Einstein won
17 24
 SELECT yr, subject
18 25
 FROM nobel
19 26
 WHERE winner = 'Albert Einstein';
20 27
 
28
+-- q4 Peace winners since 2000 inclusively
21 29
 SELECT winner
22 30
 FROM nobel
23 31
 WHERE yr >= 2000
@@ -92,33 +96,89 @@ SELECT yr, subject, winner
92 96
 FROM nobel
93 97
 WHERE subject <> 'Chemistry' -- not equal is <>
94 98
 AND subject <> 'Medicine'
95
-AND yr = 1980
99
+AND yr = 1980;
96 100
 
97 101
 -- another one I came up with
98 102
 SELECT *
99 103
 FROM nobel
100
-WHERE yr = 1980 AND subject NOT IN ('Chemistry', 'Medicine')
104
+WHERE yr = 1980 AND subject NOT IN ('Chemistry', 'Medicine');
101 105
 
102 106
 -- another one he did
103 107
 SELECT *
104 108
 FROM nobel
105 109
 WHERE subject NOT IN ('Chemistry', 'Medicine')
106
-AND yr = 1980
110
+AND yr = 1980;
107 111
 
108 112
 -- q10 Show the info for the 'Medicine' prize before 1910, and Literature after 2004 inclusively
109 113
 SELECT *
110 114
 FROM nobel
111 115
 WHERE (subject = 'Literature' AND yr >= 2004)
112
-OR (subject = 'Medicine' AND yr < 1910)
116
+OR (subject = 'Medicine' AND yr < 1910);
113 117
 
114 118
 -- q11 Find all of the prizes by Peter Grünberg
115 119
 SELECT *
116 120
 FROM nobel
117
-WHERE name = 'Peter Grünberg'
121
+WHERE name = 'Peter Grünberg';
118 122
 
119 123
 -- he didn't expect this to work, lol. I think this newer database engine uses UTF-8 encoding for the data, and thus there is no problem there
120 124
 
121 125
 -- sad excuse for LIKE demonstrated here. He does mention it's bad to do this
122 126
 SELECT *
123 127
 FROM nobel
124
-WHERE name LIKE 'Peter Gr%nberg'
128
+WHERE name LIKE 'Peter Gr%nberg';
129
+
130
+-- can also use CHAR(252) to show ü. Not sure why you would need to do this since you can type this particular character, but I guess if your name has newlines in it that this would matter.
131
+SELECT *
132
+FROM nobel
133
+WHERE winner = 'Peter Gr' || CHAR(252) || 'nberg';
134
+
135
+-- q12 find this other guy
136
+SELECT *
137
+FROM nobel
138
+WHERE winner = 'Eugene O''Niell';
139
+
140
+-- q13 find winners that have names that start with 'Sir', and in the order name, year, subject
141
+SELECT winner, year, subject
142
+FROM nobel
143
+WHERE winner LIKE 'Sir %'
144
+ORDER BY yr DESC, winner;
145
+
146
+
147
+
148
+-- dataset is now world(name, continent, area, population, gdp)
149
+
150
+-- q1 list each country name where the population is larger than that of Russia
151
+SELECT name
152
+FROM world
153
+WHERE population > (
154
+  SELECT population
155
+  FROM world
156
+  WHERE name = 'Russia'
157
+);
158
+-- note - you can only expect one result to be returned from the nested query, otherwise it will fail. For example, returning all countries numbers that have an R to start the name of the country will fail.
159
+
160
+-- could have also done the population straight.
161
+SELECT name
162
+FROM world
163
+WHERE population > 1460000000;
164
+-- "cheating"
165
+
166
+-- q2 show the countries with a per capita GDP greater than the UK
167
+SELECT name
168
+FROM world
169
+WHERE continent = 'Europe'
170
+AND gdp/population > (
171
+  SELECT gdp/population AS percapitagdp
172
+  FROM world
173
+  WHERE name = 'United Kingdom'
174
+);
175
+
176
+-- q3 List the names and continent of countries in the continents containng either Argentena or Australia, and order by country
177
+SELECT name, continent
178
+FROM world
179
+WHERE continent IN (
180
+  SELECT continent
181
+  FROM world
182
+  WHERE name IN ('Argentena', 'Australia')
183
+)
184
+ORDER BY name;

Loading…
Cancel
Save