0% found this document useful (0 votes)
2 views10 pages

07 Join Queries

Uploaded by

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

07 Join Queries

Uploaded by

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

Join Queries

Week 8

Muhammad Qasim
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
 (INNER) JOIN: Returns records that have matching values in both
tables
 LEFT (OUTER) JOIN: Returns all records from the left table, and
the matched records from the right table
 RIGHT (OUTER) JOIN: Returns all records from the right table,
and the matched records from the left table
 FULL (OUTER) JOIN: Returns all records when there is a match in
either left or right table
Different Types of SQL JOINs
Inner Join
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

SELECT *
FROM Customer inner join Employee
ON Customer.CustomerID=Employee.EmployeeID;
Left Join
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

SELECT *

FROM Customer

LEFT JOIN Employee

ON Customer.CustomerID=Employee.EmployeeID;
Right Join
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

SELECT *

FROM Customer

RIGHT JOIN Employee

ON Customer.CustomerID=Employee.EmployeeID;
Full Join or Full outer Join
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Self Join
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

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;
Sample tables

 Question: Write query to pull the record of person living in Charlottesville?


 Question: Write query to pull the all order placed in 3rd qtr of year?
 Question: Write query to pull the record of lowest amount order?
 Question: Write query to pull the customer with most orders?
Example Tables

Question: Write a query to pull a list with salesman name, customer name and their
cities for the salesmen and customer who belongs to the same city.

You might also like