Skip to content

Commit 3bbb115

Browse files
committed
Create query.sql
1 parent 62db15a commit 3bbb115

File tree

44 files changed

+531
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+531
-59
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- 1. Select name fields (with alias) and region
2+
SELECT cities.name as city, countries.name as country, countries.region
3+
FROM cities
4+
INNER JOIN countries
5+
ONe cities.country_code = countries.code;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- 3. Select fields with aliases
2+
SELECT c.code AS country_code, c.name, e.year, e.inflation_rate
3+
FROM countries AS c
4+
-- 1. Join to economies (alias e)
5+
INNER JOIN economies AS e
6+
-- 2. Match on code
7+
ON c.code = e.code;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- 6. Select fields
2+
SELECT c.code, name, region, e.year, fertility_rate, unemployment_rate
3+
-- 1. From countries (alias as c)
4+
FROM countries AS c
5+
-- 2. Join to populations (as p)
6+
INNER JOIN populations AS p
7+
-- 3. Match on country code
8+
ON c.code = p.country_code
9+
-- 4. Join to economies (as e)
10+
INNER JOIN economies AS e
11+
-- 5. Match on country code and year
12+
ON c.code = e.code AND e.year = p.year;

Joining Data in SQL/01 Introduction to joins/01 Inner join/script.py renamed to Joining Data in SQL/01 Introduction to joins/04 Review inner join using on/readme.md

File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- 4. Select fields
2+
SELECT c.name AS country, c.continent, l.name AS language, l.official
3+
-- 1. From countries (alias as c)
4+
FROM countries AS c
5+
-- 2. Join to languages (as l)
6+
JOIN languages as l
7+
-- 3. Match using code
8+
USING(code)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
SELECT p1.country_code,
2+
p1.size AS size2010,
3+
p2.size AS size2015,
4+
-- 1. calculate growth_perc
5+
((p2.size - p1.size)/p1.size * 100.0) AS growth_perc
6+
-- 2. From populations (alias as p1)
7+
FROM populations AS p1
8+
-- 3. Join to itself (alias as p2)
9+
INNER JOIN populations AS p2
10+
-- 4. Match on country code
11+
ON p1.country_code = p2.country_code
12+
-- 5. and year (with calculation)
13+
AND p1.year = p2.year - 5;

Joining Data in SQL/01 Introduction to joins/06 Self-join/script.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SELECT name, continent, code, surface_area,
2+
-- 1. First case
3+
CASE WHEN surface_area > 2000000 THEN 'large'
4+
-- 2. Second case
5+
WHEN surface_area > 350000 THEN 'medium'
6+
-- 3. Else clause + end
7+
ELSE 'small' END
8+
-- 4. Alias name
9+
AS geosize_group
10+
-- 5. From table
11+
FROM countries;

Joining Data in SQL/01 Introduction to joins/07 Case when and then/script.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
SELECT country_code, size,
2+
CASE WHEN size > 50000000
3+
THEN 'large'
4+
WHEN size > 1000000
5+
THEN 'medium'
6+
ELSE 'small' END
7+
AS popsize_group
8+
INTO pop_plus
9+
FROM populations
10+
WHERE year = 2015;
11+
12+
-- 5. Select fields
13+
SELECT *
14+
-- 1. From countries_plus (alias as c)
15+
FROM countries_plus AS c
16+
-- 2. Join to pop_plus (alias as p)
17+
JOIN pop_plus AS p
18+
-- 3. Match on country code
19+
ON c.code = p.country_code
20+
-- 4. Order the table
21+
ORDER BY geosize_group ASC;

0 commit comments

Comments
 (0)