Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views30 pages

Chapter 1

Uploaded by

Rahul Bhole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views30 pages

Chapter 1

Uploaded by

Rahul Bhole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 30

The ins and outs of

INNER JOIN
JOINING DATA IN SQL

Maham Faisal Khan


Senior Content Developer, DataCamp
The ins and outs of INNER JOINs

JOINING DATA IN SQL


The ins and outs of INNER JOINs
INNER JOIN looks for records in both tables which match on a given field

Diagram for an INNER JOIN on the id field

JOINING DATA IN SQL


The ins and outs of INNER JOINs
Diagram for an INNER JOIN ON the id field

JOINING DATA IN SQL


The ins and outs of INNER JOINs
Diagram for an INNER JOIN ON the id field

JOINING DATA IN SQL


The leadership database schema
World leaders up to 2024:

JOINING DATA IN SQL


At the presidents table
SELECT *
FROM presidents;

| country | continent | president |


|----------|--------------- |------------------------- |
| Egypt | Africa | Abdel Fattah el-Sisi |
| Portugal | Europe | Marcelo Rebelo de Sousa |
| USA | North America | Joe Biden |
| Uruguay | South America | Luis Lacalle Pou |
| Pakistan | Asia | Asif Ali Zardari |
| Chile | South America | Gabriel Boric |
| India | Asia | Droupadi Murmu |

JOINING DATA IN SQL


Meet the prime ministers
The prime_ministers table

JOINING DATA IN SQL


Prime ministers, meet the presidents
The presidents table The prime_ministers table

JOINING DATA IN SQL


Our first INNER JOIN
--Inner join of presidents and prime_ministers, joining on country
SELECT prime_ministers.country, prime_ministers.continent, prime_minister, president
FROM presidents
INNER JOIN prime_ministers
ON presidents.country = prime_ministers.country;

Note. The table.column_name format must be used when selecting columns that exist in both
tables to avoid a SQL error.

| country | continent | prime_minister | president |


|---------------- |----------- |------------------ |------------------------- |
| Egypt | Africa | Mostafa Madbouly | Abdel Fattah el-Sisi |
| Portugal | Europe | Luís Montenegro | Marcelo Rebelo de Sousa |
| Pakistan | Asia | Shehbaz Sharif | Asif Ali Zardari |
| India | Asia | Narendra Modi | Ram Nath Kovind |

JOINING DATA IN SQL


Aliasing tables
--Inner join of presidents and prime_ministers, joining on country
SELECT p2.country, p2.continent, prime_minister, president
FROM presidents AS p1
INNER JOIN prime_ministers AS p2
ON p1.country = p2.country;

| country | continent | prime_minister | president |


|---------------- |----------- |------------------ |------------------------- |
| Egypt | Africa | Mostafa Madbouly | Abdel Fattah el-Sisi |
| Portugal | Europe | Luís Montenegro | Marcelo Rebelo de Sousa |
| Pakistan | Asia | Shehbaz Sharif | Asif Ali Zardari |
| India | Asia | Narendra Modi | Ram Nath Kovind |

Aliases can be used in the table.column_name syntax in SELECT and ON clauses.

JOINING DATA IN SQL


Using USING
--Inner join of presidents and prime_ministers, joining on country
SELECT p2.country, p2.continent, prime_minister, president FROM
presidents AS p1
INNER JOIN prime_ministers AS p2
USING(country);

| country | continent | prime_minister | president |


|---------------- |-----------|------------------ |------------------------- |
| Egypt | Africa | Mostafa Madbouly | Abdel Fattah el-Sisi |
| Portugal | Europe | Luís Montenegro | Marcelo Rebelo de Sousa |
| Pakistan | Asia | Shehbaz Sharif | Asif Ali Zardari |
| India | Asia | Narendra Modi | Ram Nath Kovind |

JOINING DATA IN SQL


Let's practice!
JOINING DATA IN SQL
Defining
relationships
JOINING DATA IN SQL

Maham Faisal Khan


Senior Content Developer, DataCamp
One-to-many relationships

JOINING DATA IN SQL


One-to-many relationships

JOINING DATA IN SQL


One-to-one relationships

JOINING DATA IN SQL


One-to-one relationships

JOINING DATA IN SQL


Many-to-many relationships

JOINING DATA IN SQL


Let's practice!
JOINING DATA IN SQL
Multiple joins
JOINING DATA IN SQL

Maham Faisal Khan


Senior Content Developer, DataCamp
Joins on 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;

Note. Depending on the use case, left_table or right_table can be used in the ON
clause.

JOINING DATA IN SQL


Joins on joins
The prime_minister_terms table from our database of world leaders

JOINING DATA IN SQL


What to join first?
SELECT p1.country, p1.continent,
president, prime_minister
FROM prime_ministers as p1
INNER JOIN presidents as p2
USING(country);

| country | continent | president | prime_minister |


| -------- | ------------- | ----------------------- | ---------------- |
| Egypt | Africa | Abdel Fattah el-Sisi | Mostafa Madbouly |
| Portugal | Europe | Marcelo Rebelo de Sousa | Luís Montenegro |
| Pakistan | Asia | Asif Ali Zardari | Shehbaz Sharif |
| India | Asia | Droupadi Murmu | Narendra Modi |

JOINING DATA IN SQL


Chaining joins

JOINING DATA IN SQL


Chaining joins
- SQL query for chaining inner joins
SELECT
p1.country
,
p1.continent,
president,
prime_minister
, pm_start
FROM prime_ministers as p1
INNER JOIN presidents as
p2 USING(country)
INNER JOIN prime_minister_terms as

p3 USING(prime_minister);
JOINING DATA IN SQL
What are we joining ON?
SELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;

Diagram for an INNER JOIN ON the id field

JOINING DATA IN SQL


Joining on multiple keys
SELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id
AND left_table.date = right_table.date;

Diagram for an INNER JOIN ON the id AND date field

JOINING DATA IN SQL


Let's practice!
JOINING DATA IN SQL

You might also like