SlideShare a Scribd company logo
1 of 24
JOINING
in SQL
Prepared
By
Mahir Mahtab Haque
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;
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;
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.
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;
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);
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;
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;
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;
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
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;
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;
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;
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’);
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.
UNION syntax
SELECT prime_minister AS leader, country
FROM prime_ministers
UNION
SELECT monarch, country
FROM monarchs
ORDER BY country;
UNION ALL syntax
SELECT prime_minister AS leader, country
FROM prime_ministers
UNION ALL
SELECT monarch, country
FROM monarchs
ORDER BY country
LIMIT 10;
INTERSECT syntax
SELECT country
FROM prime_ministers
INTERSECT
SELECT country
FROM presidents;
EXCEPT syntax
SELECT monarch, country
FROM monarchs
EXCEPT
SELECT prime_minister, country
FROM prime_ministers;
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);
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;
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);
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
);
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;

More Related Content

Recently uploaded

NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
Amil baba
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
pwgnohujw
 
edited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfedited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdf
great91
 
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
acoha1
 
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
mikehavy0
 
一比一原版西悉尼大学毕业证成绩单如何办理
一比一原版西悉尼大学毕业证成绩单如何办理一比一原版西悉尼大学毕业证成绩单如何办理
一比一原版西悉尼大学毕业证成绩单如何办理
pyhepag
 
一比一原版麦考瑞大学毕业证成绩单如何办理
一比一原版麦考瑞大学毕业证成绩单如何办理一比一原版麦考瑞大学毕业证成绩单如何办理
一比一原版麦考瑞大学毕业证成绩单如何办理
cyebo
 
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
fztigerwe
 
一比一原版阿德莱德大学毕业证成绩单如何办理
一比一原版阿德莱德大学毕业证成绩单如何办理一比一原版阿德莱德大学毕业证成绩单如何办理
一比一原版阿德莱德大学毕业证成绩单如何办理
pyhepag
 

Recently uploaded (20)

123.docx. .
123.docx.                                 .123.docx.                                 .
123.docx. .
 
Atlantic Grupa Case Study (Mintec Data AI)
Atlantic Grupa Case Study (Mintec Data AI)Atlantic Grupa Case Study (Mintec Data AI)
Atlantic Grupa Case Study (Mintec Data AI)
 
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
 
edited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfedited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdf
 
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
 
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
 
一比一原版西悉尼大学毕业证成绩单如何办理
一比一原版西悉尼大学毕业证成绩单如何办理一比一原版西悉尼大学毕业证成绩单如何办理
一比一原版西悉尼大学毕业证成绩单如何办理
 
社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction
 
NOAM AAUG Adobe Summit 2024: Summit Slam Dunks
NOAM AAUG Adobe Summit 2024: Summit Slam DunksNOAM AAUG Adobe Summit 2024: Summit Slam Dunks
NOAM AAUG Adobe Summit 2024: Summit Slam Dunks
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshare
 
一比一原版麦考瑞大学毕业证成绩单如何办理
一比一原版麦考瑞大学毕业证成绩单如何办理一比一原版麦考瑞大学毕业证成绩单如何办理
一比一原版麦考瑞大学毕业证成绩单如何办理
 
Formulas dax para power bI de microsoft.pdf
Formulas dax para power bI de microsoft.pdfFormulas dax para power bI de microsoft.pdf
Formulas dax para power bI de microsoft.pdf
 
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
 
2024 Q2 Orange County (CA) Tableau User Group Meeting
2024 Q2 Orange County (CA) Tableau User Group Meeting2024 Q2 Orange County (CA) Tableau User Group Meeting
2024 Q2 Orange County (CA) Tableau User Group Meeting
 
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...
 
Aggregations - The Elasticsearch "GROUP BY"
Aggregations - The Elasticsearch "GROUP BY"Aggregations - The Elasticsearch "GROUP BY"
Aggregations - The Elasticsearch "GROUP BY"
 
一比一原版阿德莱德大学毕业证成绩单如何办理
一比一原版阿德莱德大学毕业证成绩单如何办理一比一原版阿德莱德大学毕业证成绩单如何办理
一比一原版阿德莱德大学毕业证成绩单如何办理
 
MATERI MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI  MANAJEMEN OF PENYAKIT TETANUS.pptMATERI  MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI MANAJEMEN OF PENYAKIT TETANUS.ppt
 
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

JOINING with SQL

  • 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;
  • 18. INTERSECT syntax SELECT country FROM prime_ministers INTERSECT SELECT country FROM presidents;
  • 19. EXCEPT syntax SELECT monarch, country FROM monarchs EXCEPT SELECT prime_minister, country FROM prime_ministers;
  • 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;