Skip to content

Commit 6e993f3

Browse files
committed
SQL Exercises
1 parent 72a9e64 commit 6e993f3

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

select_from_nobel.sql

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
-- 1. nobel Nobel Laureates
2+
-- We continue practicing simple SQL queries on a single table. This tutorial is concerned with a table of Nobel prize winners:
3+
4+
SELECT yr, subject, winner
5+
FROM nobel
6+
WHERE yr = 1950
7+
8+
9+
10+
-- 2. 1962 Literature
11+
-- Show who won the 1962 prize for literature.
12+
13+
SELECT winner
14+
FROM nobel
15+
WHERE yr = 1962
16+
AND subject = 'literature'
17+
18+
19+
20+
-- 3. Albert Einstein
21+
22+
select yr, subject
23+
from nobel
24+
where winner = 'Albert Einstein'
25+
26+
27+
28+
-- 4. Recent Peace Prizes
29+
-- Give the name of the 'peace' winners since the year 2000, including 2000.
30+
31+
select winner
32+
from nobel
33+
where yr >= 2000 and subject = 'peace'
34+
35+
36+
37+
-- 5. Literature in the 1980's
38+
-- Show all details (yr, subject, winner) of the literature prize winners for 1980 to 1989 inclusive.
39+
40+
select yr, subject, winner
41+
from nobel
42+
where (yr between 1980 and 1989) and (subject = 'literature')
43+
44+
45+
46+
47+
-- 6. Only Presidents
48+
-- Show all details of the presidential winners:
49+
-- Theodore Roosevelt
50+
-- Thomas Woodrow Wilson
51+
-- Jimmy Carter
52+
-- Barack Obama
53+
54+
SELECT *
55+
FROM nobel
56+
WHERE winner IN (
57+
'Theodore Roosevelt',
58+
'Woodrow Wilson',
59+
'Jimmy Carter',
60+
'Barack Obama'
61+
)
62+
63+
64+
65+
-- 7. John
66+
-- Show the winners with first name John
67+
68+
select winner
69+
from nobel
70+
where winner like 'John%'
71+
72+
73+
74+
75+
-- 8. Chemistry and Physics from different years
76+
-- Show the year, subject, and name of physics winners for 1980 together with the chemistry winners for 1984.
77+
78+
79+
select *
80+
from nobel
81+
where (yr = 1980 and subject = 'physics') or (yr = 1984 and subject = 'chemistry')
82+
83+
84+
85+
-- 9. Exclude Chemists and Medics
86+
-- Show the year, subject, and name of winners for 1980 excluding chemistry and medicine
87+
88+
select *
89+
from nobel
90+
where yr = 1980 and not subject in ('chemistry', 'medicine')
91+
92+
93+
94+
-- 10. Early Medicine, Late Literature
95+
-- 10. Show year, subject, and name of people who won a 'Medicine' prize in an early year (before 1910, not including 1910) together with winners of a 'Literature' prize in a later year (after 2004, including 2004)
96+
97+
98+
select yr, subject, winner
99+
from nobel
100+
where (yr < 1910 and subject = 'Medicine') or (yr >= 2004 and subject = 'Literature')
101+
102+
103+
104+
105+
-- 11. Umlaut
106+
-- Find all details of the prize won by PETER GRÜNBERG
107+
108+
SELECT *
109+
FROM nobel
110+
WHERE winner LIKE 'peter gr%nberg'
111+
112+
113+
114+
115+
-- 12. Apostrophe
116+
-- Find all details of the prize won by EUGENE O'NEILL
117+
118+
select *
119+
from nobel
120+
where winner = 'Eugene O''neill'
121+
122+
123+
124+
-- 13. Knights of the realm
125+
-- List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
126+
127+
128+
select winner, yr, subject
129+
from nobel
130+
where winner like 'Sir%'
131+
order by yr DESC, winner
132+
133+
134+
135+
-- 14. Chemistry and Physics last
136+
-- The expression subject IN ('chemistry','physics') can be used as a value - it will be 0 or 1. Show the 1984 winners and subject ordered by subject and winner name; but list chemistry and physics last.
137+
138+
SELECT winner, subject
139+
FROM nobel
140+
WHERE yr=1984
141+
order by
142+
case when subject IN ('physics', 'chemistry') then 1 else 0 END,
143+
subject, winner

select_from_world.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- 1. Introduction
1+
- 1. Introduction
22
-- Read the notes about this table. Observe the result of running this SQL command to show the name, continent and population of all countries.
33

44
SELECT name, continent, population

0 commit comments

Comments
 (0)