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

Orderid Customerid Employeeid Orderdate: Joins

The document discusses different types of SQL joins: - INNER JOIN returns rows when there is a match between both tables. - LEFT JOIN returns all rows from the left table even if no matches in right table. - RIGHT JOIN returns all rows from the right table even if no matches in left table. - FULL OUTER JOIN returns rows when there is a match in either left or right tables. - SELF JOIN allows a table to be joined with itself. The business problem section describes using multiple INNER JOINs to find customers interested in a New Year campaign by matching rows from onlinecustomers, orders and sales tables.

Uploaded by

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

Orderid Customerid Employeeid Orderdate: Joins

The document discusses different types of SQL joins: - INNER JOIN returns rows when there is a match between both tables. - LEFT JOIN returns all rows from the left table even if no matches in right table. - RIGHT JOIN returns all rows from the right table even if no matches in left table. - FULL OUTER JOIN returns rows when there is a match in either left or right tables. - SELF JOIN allows a table to be joined with itself. The business problem section describes using multiple INNER JOINs to find customers interested in a New Year campaign by matching rows from onlinecustomers, orders and sales tables.

Uploaded by

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

JOINS

ORDERS TABLE

OrderID CustomerID EmployeeID OrderDate

O111 3 12 2021-02-18

O222 2 13 2021-02-19

O333 70 11 2021-02-20

CUSTOMERS TABLE

CustomerID CustomerName City

1 Anthony CBE

2 Balan CHENNAI

3 Ravi CHENNAI
JOIN

OLD VERSION
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders , Customers
WHERE  Orders.CustomerID = Customers.CustomerID;

OrderID CustomerName

O111 Ravi

O222 Balan

SQL JOIN – ANSI VERSION

SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Note: The INNER JOIN keyword selects all rows from both tables as long
as there is a match between the columns. If there are records in the
"Orders" table that do not have matches in "Customers", these orders will
not be shown!

JOIN THREE TABLES

SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName


FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
SQL LEFT JOIN Keyword
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.

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

CustomerName OrderID

Anthony Null

Balan O222

Ravi O333
The LEFT JOIN keyword returns all records from the left table
(Customers), even if there are no matches in the right table (Orders)

SQL RIGHT JOIN Keyword


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.

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

Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

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


FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
EmployeeID LastName FirstName

11 Sharma Nina

12 Rajesh Sathish

13 Mehta Sanket

14 Khan Zara

The output will be

OrderID LastName FirstName

O333 Sharma Nina

O111 Rajesh Sathish

0222 Mehta Sanket

null Khan Zara


SQL FULL OUTER JOIN Keyword
The FULL OUTER JOIN keyword returns all records when there is a match
in left (table1) or right (table2) table records.

Note: FULL OUTER JOIN can potentially return very large result-sets!

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;

CustomerName OrderID

Anthony Null

Balan O222

Ravi 0111

Null 0333
SQL Self JOIN
A self JOIN is a regular join, but the table is joined
with itself.

Self JOIN Syntax


SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

The following SQL statement matches customers


that are from the same city:

Example

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;
Business problem

Example scenario
ABC Ltd. company launched a new campaign for the New Year and made different offers to
its online customers. As a result of their campaign, they succeeded in converting some offers
to sales. In the following examples, we will uncover the new year campaign data details of
the ABC company.

The company stores these campaign data details in the following tables.

CREATE TABLE onlinecustomers


(customerid INT PRIMARY KEY ,
CustomerName VARCHAR(100) ,
CustomerCity VARCHAR(100) ,
Customermail VARCHAR(100))

CREATE TABLE orders (orderId INT PRIMARY KEY ,


customerid INT  ,
ordertotal float ,
discountrate float ,
orderdate DATE
);

CREATE TABLE sales (salesId INT PRIMARY KEY ,


orderId INT  ,
salestotal FLOAT
);

Business problem: Which customers were interested in this New Year campaign?
In order to answer this question, we need to find out the matched rows for all the tables
because some customers did not receive an email offer, and some offers could not be
converted into a sale. The following Venn diagram will help us to figure out the matched
rows which we need. In short, the result of this query should be the intersecting rows of all
tables in the query. The grey-colored area specifies these rows in the Venn diagram:

The SQL multiple joins approach will help us to


join onlinecustomers, orders, and sales tables. As shown in the Venn diagram,
we need to matched rows of all tables. For this reason, we will combine all tables
with an inner join clause. The following query will return a result set that is
desired from us and will answer the question:

SELECT customerName, customercity, customermail, salestotal


FROM onlinecustomers AS oc
   INNER JOIN
   orders AS o
   ON oc.customerid = o.customerid
   INNER JOIN
   sales AS s
   ON o.orderId = s.orderId
The result

You might also like