Subqueries Inside Where and Select Clauses: Chester Ismay
Subqueries Inside Where and Select Clauses: Chester Ismay
Chester Ismay
Data Science Evangelist, DataRobot
Subquery inside WHERE clause set-up
+-----------+--------------+-------------+--------------------+
| name | indep_year | fert_rate | women_parli_perc |
|-----------+--------------+-------------+--------------------|
| Australia | 1901 | 1.88 | 32.74 |
| Brunei | 1984 | 1.96 | 6.06 |
| Chile | 1810 | 1.8 | 15.82 |
| Egypt | 1922 | 2.7 | 14.9 |
| Haiti | 1804 | 3.03 | 2.74 |
| India | 1947 | 2.43 | 11.58 |
| Liberia | 1847 | 4.64 | 11.65 |
| Norway | 1905 | 1.93 | 39.6 |
| Oman | 1951 | 2.75 | 8.82 |
| Portugal | 1143 | 1.31 | 34.8 |
| Spain | 1492 | 1.53 | 38.64 |
| Uruguay | 1828 | 2.03 | 22.31 |
| Vietnam | 1945 | 1.7 | 24 |
+-----------+--------------+-------------+--------------------+
+---------+
| avg |
|---------|
| 2.28385 |
+---------+
+---------+-------------+
| name | fert_rate |
|---------+-------------|
| Brunei | 1.96 |
| Vietnam | 1.7 |
+---------+-------------+
+---------------+
| continent |
|---------------|
| Africa |
| Asia |
| Europe |
| North America |
| Oceania |
+---------------+
+---------------+-----------------+
| continent | countries_num |
|---------------+-----------------|
| Africa | 2 |
| Asia | 4 |
| Europe | 3 |
| North America | 1 |
| Oceania | 1 |
+---------------+-----------------+
Let's practice!
J O I N I N G D ATA I N S Q L
Subquery inside the
FROM clause
J O I N I N G D ATA I N S Q L
Chester Ismay
Data Science Evangelist, DataRobot
Build-up
SELECT continent, MAX(women_parli_perc) AS max_perc
FROM states
GROUP BY continent
ORDER BY continent;
+---------------+------------+
| continent | max_perc |
|---------------+------------|
| Africa | 14.9 |
| Asia | 24 |
| Europe | 39.6 |
| North America | 2.74 |
| Oceania | 32.74 |
| South America | 22.31 |
+---------------+------------+
+-------------+
| continent |
|-------------|
| Asia |
| Asia |
| Asia |
| Asia |
| Asia |
| Asia |
| Asia |
| Asia |
| Europe |
| Europe |
| Europe |
| Europe |
| Europe |
| Europe |
+-------------+
FROM monarchs,
(SELECT continent, MAX(women_parli_perc) AS max_perc
-- Select fields
select local_name,subquery.lang_num
-- From countries
FROM states from countries,
-- Subquery (alias as subquery)
GROUP BY continent) AS subquery (SELECT code, COUNT(*) AS lang_num
FROM languages
GROUP BY code) AS subquery
WHERE monarchs.continent = subquery.continent -- Where codes match
WHERE countries.code=subquery.code
ORDER BY continent; -- Order by descending number of languages
ORDER BY lang_num ;
+-------------+------------+
| continent | max_perc |
|-------------+------------|
| Asia | 24 |
| Europe | 39.6 |
+-------------+------------+
Chester Ismay
Data Science Evangelist, DataRobot
Types of joins
INNER JOIN
Self-joins
OUTER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
CROSS JOIN
Semi-join / Anti-join
this!
rate for each region in 2015 FROM countries AS c LIMIT 10;
-- Join to right table
INNER JOIN populations AS p
-- Match on join condition
ON c.code = p.country_code
-- Where specific records matching some condition
Remember that you'll WHERE year = 2015
need to GROUP BY
all fields that
-- Group appropriately J O I N I N G D ATA I N S Q L
GROUP BY c.region, c.continent
aren't included in
-- Order appropriately
the aggregate
ORDER BY avg_fert_rate;
function of SELECT