2. INNER JOIN
• An INNER JOIN only includes records in which the key is in both tables. With INNER JOINS, we
look for matches in the right table corresponding to all entries in the key field in the left table.
• For example: The syntax for completing an INNER JOIN from the prime_ministers table to the
presidents table based on a key field of country is as follows:
• Aliasing helps to simplify your code especially with longer table names like prime_ministers and
presidents.
• Also, since ‘country’ is the key field and it exits in both tables, we write p1 and the ‘period’ to
avoid a SQL error.
SELECT p1.country, p1.continent,
prime_minister, president
FROM prime_ministers AS p1
INNER JOIN presidents AS p2
ON p1.country = p2.country;
3. Basic syntax for an INNER JOIN including all
columns in both tables
SELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;
4. Aliasing table names
SELECT c1.name AS city, c2.name AS country
FROM cities AS c1
INNER JOIN countries AS c2
ON c1.country_code = c2.code;
To select a field in your query that appears in multiple tables, you will need to identify which table/table
alias you are referring to by using a ‘period’ in your select statement.
5. Multiple JOINs
SELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id
INNER JOIN another_table
ON left_table.id = another_table.id;
6. INNER JOIN via USING
SELECT left_table.id AS L_id,
left_table.val AS L_val,
right_table.val AS R_val
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;
SELECT left_table.id AS L_id,
left_table.val AS L_val,
right_table.val AS R_val
FROM left_table
INNER JOIN right_table
USING (id);
Since ‘id’ is the same name in both tables, we can specify ‘USING’ instead of ‘ON’ here. Example: When
joining tables with a common field name:
SELECT *
FROM countries
INNER JOIN economies
ON countries.code = economies.code;
SELECT *
FROM countries
INNER JOIN economies
USING (code);
7. Self-JOINs
• Self-JOINS are used to compare values in a field to other values of the
same field from within the same table. For example: Joining
prime_ministers to itself
• This will pair each country with every other country in its same
continent. But as we don’t want to list the country with itself after all,
our query would then be:
SELECT p1.country AS country1, p2.country AS country2, p1.continent
FROM prime_ministers AS p1
INNER JOIN prime_ministers AS p2
ON p1.continent = p2.continent
LIMIT 14;
SELECT p1.country AS country1, p2.country AS country2, p1.continent
FROM prime_ministers AS p1
INNER JOIN prime_ministers AS p2
ON p1.continent = p2.continent AND p1.country <> p2.country
LIMIT 13;
8. CASE
• CASE is a way to do multiple if-then-else statements in a simplified
way in SQL. For example: Preparing indep_year_group in states
SELECT name, continent, indep_year
CASE WHEN indep_year < 1900 THEN ‘before 1900’
WHEN indep_year < = 1930 THEN ‘between 1900 and 1930’
ELSE ‘after 1930’ END
AS indep_year_group
FROM states
ORDER BY indep_year_group;
9. INTO
SELECT name, continent, code, surface_area
CASE WHEN surface_area > 2000000 THEN ‘large’
WHEN surface_area > 350000 THEN ‘medium’
ELSE ‘small’ END
AS geosize_group
INTO countries_plus
FROM countries;
10. OUTER JOINs
• There are 3 types of OUTER JOINs:
- LEFT JOINs: A LEFT JOIN keeps all of the original records in the left
table but then marks the values as missing in the right table for those
that don’t have a match. However, there are exceptions as in the case
for multiple LEFT JOINs.
- RIGHT JOINs: A reverse of the LEFT JOIN.
- FULL JOINs
11. Syntax of a LEFT JOIN
SELECT p1.country, prime_minister, president
FROM prime_ministers AS p1
LEFT JOIN presidents AS p2
ON p1.country = p2.country;
12. Syntax of a RIGHT JOIN
SELECT right_table.id AS R_id,
left_table.val AS L_val,
right_table.val AS R_val
FROM left_table
RIGHT JOIN right_table
ON left_table.id = right_table.id;
13. FULL JOINs
• A FULL JOIN combines both LEFT and RIGHT JOINs, i.e., it will bring in
all records from both the left and right tables and keep track of the
missing values accordingly.
Syntax for FULL JOIN:
SELECT left_table.id AS L_id,
right_table.id AD R_id,
left_table.val AS L_val,
right_table.val AS R_val
FROM left_table
FULL JOIN right_table
USING (id);
Example:
SELECT p1.country AS pm_co, p2.country AS pres_co,
prime_minister, president
FROM prime_ministers AS p1
FULL JOIN presidents AS p2
ON p1.country = p2.country;
14. CROSS JOINs
• CROSS JOINs create all possible combinations of two tables.
Example:
SELECT prime_minister, president
FROM prime_ministers AS p1
CROSS JOIN presidents AS p2
WHERE p1.continent IN (‘North America’, ‘Oceania’);
15. Set Theory
• UNION: This includes every record in both tables but does not double
count those that are in both tables.
• UNION ALL: This includes every record in both tables and does
replicate those that are in both tables.
• INTERSECT: This results in only those records found in both of the two
tables.
• EXCEPT: This results in only those records in one table but not the
other.
16. UNION syntax
SELECT prime_minister AS leader, country
FROM prime_ministers
UNION
SELECT monarch, country
FROM monarchs
ORDER BY country;
17. UNION ALL syntax
SELECT prime_minister AS leader, country
FROM prime_ministers
UNION ALL
SELECT monarch, country
FROM monarchs
ORDER BY country
LIMIT 10;
20. SEMI-JOINs
• SEMI-JOINs choose records in the first table where a condition is met
in a second table.
SELECT president, country, continent
FROM presidents
WHERE country IN
(SELECT name
FROM states
WHERE indep_year < 1800);
21. Semi-JOINs (cont.)
• Sometimes problems that can be solved with SEMI-JOINs can also be
solved by INNER JOINs. For example: Retrieving languages spoken in
the Middle East
SELECT DISTINCT name
FROM languages
WHERE code IN
(SELECT code
FROM countries
WHERE region = ‘Middle East’)
ORDER BY name;
SELECT DISTINCT languages.name AS language
FROM languages
INNER JOIN countries
ON languages.code = countries.code
WHERE region = ‘Middle East’
ORDER BY language;
22. Anti-JOINs
• ANTI-JOINs chooses records in the first table where a condition is not
met in the second table.
SELECT president, country, continent
FROM presidents
WHERE country LIKE ‘%America’
AND country NOT IN
(SELECT name
FROM states
WHERE indep_year < 1800);
23. SET THEORY Challenge
• Identify country codes that are included in either economies or
currencies but not in populations and use that result to determine
the names of cities in the countries that match the specification.
SELECT name
FROM cities AS c1
WHERE country_code IN
(
SELECT e.code
FROM economies AS e
UNION
SELECT c2.code
FROM currencies AS c2
EXCEPT
SELECT p.country_code
FROM populations AS p
);
24. Sub-queries
Subquery inside WHERE
clause set-up:
Subquery inside SELECT clause set-up: Subquery inside FROM clause set-up
SELECT name, fert_rate
FROM states
WHERE continent = ‘Asia’
AND fert_rate <
(SELECT AVG (fert_rate)
FROM states);
SELECT DISTINCT continent,
(SELECT COUNT(*)
FROM states
WHERE prime_ministers.continent =
states.continent) AS countries_num
FROM prime_ministers;
SELECT DISTINCT monarchs.continent,
subquery.max_perc
FROM monarchs,
(SELECT continent, MAX(women_parli_perc)
AS max_perc
FROM states
GROUP BY continent) AS subquery
WHERE monarchs.continent =
subquery.continent
ORDER BY continent;