0% found this document useful (0 votes)
85 views

Sql-Joins: Inner, Outer, Full: Session 6 (Week 4)

The document discusses different types of SQL joins - inner, outer, full. It provides examples of using inner joins to select records that match between two tables based on common column values. It also discusses when joins are used such as to access or combine data from multiple tables based on common fields. Examples are given of left, right, and full outer joins and how they return matched and unmatched records.

Uploaded by

Subhadip Sinha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Sql-Joins: Inner, Outer, Full: Session 6 (Week 4)

The document discusses different types of SQL joins - inner, outer, full. It provides examples of using inner joins to select records that match between two tables based on common column values. It also discusses when joins are used such as to access or combine data from multiple tables based on common fields. Examples are given of left, right, and full outer joins and how they return matched and unmatched records.

Uploaded by

Subhadip Sinha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

SQL- JOINS : INNER, Session 6 (Week 4)

OUTER, FULL
OrderID CustomerID OrderDate
10308 2 1996-09-18

INNER JOIN 10309


10310
37
77
1996-09-19
1996-09-20
Then, we can create the following SQL
statement (that contains an INNER JOIN), that
selects records that have matching values in both CustomerI CustomerName ContactName City
D
tables:
1 Ashka Gulati Ashka Mumbai
2 Ashu Sharma Ashu Ajmer
3 Aslesha Choudhary Ashlesha Orissa

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate


FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

OrderID CustomerName OrderDate


2 Ashu Sharma 1996-09-18
WHEN DO WE USE JOINS ?
1. If you want to access more than one table through a select statement.
2. If you want to combine two or more table then SQL JOIN statement is used.
3. The joining of two or more tables is based on common field between them.
4. SQL INNER JOIN also known as simple join is the most common type of join.
Staff
ID Staff_NAME Staff_AGE STAFF_ADDRESS Monthley_Package

1 ARYAN 22 MUMBAI 18000


2 SUSHIL 32 DELHI 20000
3 MONTY 25 MOHALI 22000
4 AMIT 20 ALLAHABAD 12000

Perform a join to join these Payment


two tables based on the Payment_ID DATE Staff_ID AMOUNT
common values in them
101 30/12/2009 1 3000.00
102 22/02/2010 3 2500.00
103 23/02/2010 4 3500.00

SELECT Staff_ID, Staff_NAME, Staff_AGE, AMOUNT   
   FROM STAFF s, PAYMENT p  
   WHERE s.ID =p.STAFF_ID;  
REVISE : INNER JOIN
Syntax:
SELECT * FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;
• Inner join the cities table on the left to the countries table on the right, keeping all of the fields in both tables.
• You should match the tables on the country_code field in cities and the code field in countries.

SELECT * FROM cities INNER JOIN countries ON cities.country_code = countries.code;


Column names
Modify the SELECT statement to keep only the name of the city, the name of the can be aliased
country, and the name of the region the country resides in.

SELECT cities.name AS city, countries.name AS country, region FROM cities INNER JOIN countries

ON cities.country_code = countries.code;
EXAMPLE 1
Use table aliasing as a shortcut. For tables you also use AS to add the alias
immediately after the table name with a space. 

SELECT c1.name AS city, c2.name AS country


Table names
FROM cities AS c1 can be aliased
as well
INNER JOIN countries AS c2
ON c1.country_code = c2.code;

Notice that to select a field in your query that appears in multiple tables, you'll need to
identify which table/table alias you're referring to by using a . in your SELECT statement.
EXAMPLE 2
• Join the tables countries (left) and economies (right) aliasing countries AS
c and economies AS e.
• Specify the field to match the tables ON.
From this join, SELECT:
• c.code, aliased as country_code.
• name, year, and inflation_rate, not aliased.

SELECT c.code AS country_code, name, year, inflation_rate


FROM countries AS c
INNER JOIN economies AS e
ON c.code = e.code;
EXAMPLE 3
• Inner join countries (left) and populations (right) on
the code and country_code fields respectively
• Alias countries AS c and populations AS p.
• Select code, name, and region from countries and also
select year and fertility_rate from populations (5 fields in total).

select c.code, c.name, c.region, p.year, p.fertility_rate


from countries as c
INNER JOIN populations as p
ON c.code=p.country_code;
EXAMPLE 4
• Add an additional inner join with economies to your previous query by joining
on code.
• Include the unemployment_rate column that became available through joining
with economies.
• Note that year appears in both populations and economies, so you have to explicitly
use e.year instead of year as you did before.

SELECT c.code, name, region, e.year, fertility_rate, unemployment_rate


FROM countries AS c
INNER JOIN populations AS p
ON c.code = p.country_code
INNER JOIN economies as e
ON p.country_code=e.code;
LEFT JOIN OR LEFT OUTER
JOIN
The LEFT JOIN keyword returns all records from the left table (table1), and
the matched records from the right table (table2). The result is NULL from the
right side, if there is no match.

Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SELECT ID, NAME, AMOUNT, DATE FROM
CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Custo CustomerName ContactName Address City PostalCode Country
merID
1 Mary Po MP Amdheri Mumbai 400056 India
2 Pratiksha Rao Pratiksha Kukatpally Hyderabad 250215 India
3 Shristi Bansal Shristi Bansal Fateh Sagar Udaipur 350235 india

OrderID CustomerID EmployeeID OrderDate ShipperID


10308 2 7 1996-09-18 3
10309 3 3 1996-09-19 1
10310 1 8 1996-09-20 2

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT OUTER JOIN Orders 
ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
The LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches in the right table
(Orders).
Customer Table Order Table
ID NAME AGE SALARY O_ID DATE CUSTOMER_ID AMOUNT
1 ARYAN 51 56000 1 20-01-2012 2 3000
2 AROHI 21 25000 2 12/2/2012 2 2000
3 VINEET 24 31000
3 22-03-2012 3 4000
4 AJEET 23 32000
4 11/4/2012 4 5000
5 RAVI 23 42000

SELECT ID, NAME, AMOUNT,DATE   ID NAME AMOUNT DATE


FROM CUSTOMER  
1 ARYAN NULL NULL
LEFT JOIN ORDER  
ON CUSTOMER.ID = ORDER.CUSTOME 2 AROHI 3000 20-01-2012
R_ID;   2 AROHI 2000 12/2/2012
3 VINEET 4000 22-03-2012
4 AJEET 5000 11/4/2012
5 RAVI NULL NULL
RIGHT JOIN OR RIGHT OUTER
JOIN
The RIGHT JOIN keyword returns all records from the right table (table2),
and the matched records from the left table (table1). The result is NULL from
the left side, when there is no match.

Example : RIGHT JOIN or RIGHT OUTER JOIN

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Customers
Orders

SELECT ID, NAME, AMOUNT, DATE


FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID =
ORDERS.CUSTOMER_ID;
CUSTOMER TABLE ORDER TABLE
ID NAME AGE SALARY DATE O_ID CUSTOMER_ID AMOUNT
1 ARYAN 51 56000
20/01/2012 1 2 3000
2 AROHI 21 25000
12/2/2012 2 2 2000
3 VINEET 24 31000
22/03/2012 3 3 4000
4 AJEET 23 32000
11/4/2012 4 4 5000
5 RAVI 23 42000

Result :
ID NAME AMOUNT DATE

SELECT ID,NAME,AMOUNT,DATE   2 AROHI 3000 20/01/2012


FROM CUSTOMER   2 AROHI 2000 12/2/2012
RIGHT JOIN ORDER  
ON CUSTOMER.ID = ORDER.CUSTOME 3 VINEET 4000 22/03/2012
R_ID;   4 AJEET 5000 11/4/2012
OrderID CustomerID EmployeeID OrderDate ShipperID
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2

EmployeeID LastName FirstName BirthDate Photo


1 Sharma Hiren 12/8/1968 EmpID1.pic
2 Jani Mihir 2/19/1952 EmpID2.pic
3 Baxi Aditya 8/30/1963 EmpID3.pic

SELECT Orders.OrderID, Employees.LastName, OrderID LastName FirstName


Employees.FirstName
FROM Orders
RIGHT JOIN Employees 
ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
FULL OUTER JOIN
The FULL OUTER JOIN keyword return all records when there is a match in left (table1) or
right (table2) table records. Includes tables on both sides of the join clause. Combines the results
of both left and right outer joins.

Returns all matched or unmatched rows. FULL OUTER JOIN can potentially return very large
result-sets! FULL OUTER JOIN and FULL JOIN are the same.

Example: FULL OUTER JOIN

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
FULL JOIN ORDERS
ON CUSTOMERS.ID =
ORDERS.CUSTOMER_ID;
B1
A1
A N
TRY YOURSELF1 ! A M
m 2 p

2 n 3 q

4 o 5 r

How will you derive this output ?

A M A N
SELECT *  
FROM  A1   2 n 2 p
FULL OUTER JOIN B1 1 m NULL NULL
ON A1.A = B1.A;  
4 o NULL NULL
NULL NULL 3 q
NULL NULL 5 r
MatchScore
Player Department_id Goals
Franklin 1 2

EXAMPLE : Alan
Priyanka
Rajesh
1
2
3
3
2
5
SELECT * FROM [TABLE1] CROSS JOIN [TABLE2];  

OR   Departments
Department_id Department_name
SELECT * FROM [ TABLE1] , [TABLE2] ; 1 IT
Player Department_id Goals Depatment_id Department_name
Franklin 1 2 1 IT
2 HR
Alan 1 3 1 IT 3 Marketing
Priyanka 2 2 1 IT
Rajesh 3 5 1 IT
Franklin 1 2 2 HR
Alan 1 3 2 HR
Priyanka 2 2 2 HR
Rajesh 3 5 2 HR
Franklin 1 2 3 Marketing
Alan 1 3 3 Marketing
Priyanka 2 2 3 Marketing
Rajesh 3 5 3 Marketing
SELF JOIN
The SQL SELF JOIN is used to join a table to itself as if the table were two
tables; temporarily renaming at least one table in the SQL statement.

A self join is a join in which a table is joined with itself (Unary relationships

To join a table itself means that each row of the table is combined with itself
and with every other row of the table.

SELECT a.column_name, b.column_name


FROM table1 as a, table1 as b
WHERE a.common_field = b.common_field;
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Ana Trujillo Avda. de la México 5021 Mexico
Emparedados y Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Moreno Mataderos México 5023 Mexico
Taquería 2312 D.F.

Match customers that are from the same city and order them
alphabetically by city

SELECT A.CustomerName AS CustomerName1,
B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
SQL UNION Session 7

You might also like