JOINS IN SQL
By Rohit Dhanwani
JOINS IN SQL By Rohit Dhanwani OBJECTIVES Define and use different - - PowerPoint PPT Presentation
JOINS IN SQL By Rohit Dhanwani OBJECTIVES Define and use different types of joins INNER JOIN EQUI JOIN NATURAL JOIN OUTER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN 2 PROCESSING MULTIPLE TABLES - JOIN
By Rohit Dhanwani
2
combined into a single table or view
condition
key of another table in a 1:M relationship (i.e., joining columns = common columns)
the primary-foreign key relationship (i.e., joining columns ≠ common columns)
(one join condition for each pair of tables joined)
3
4
CustomerID CustomerName CustomerAddress CustomerCity CustomerPostalCode CustomerState OrderID OrderDate CustomerID OrderID ProductID OrderedQuantity ProductID ProductDescription ProductFinish ProductStandardPrice ProductLineID
CUSTOMER ORDER ORDERLINE PRODUCT
for the values of the joining columns. Returns matching rows (i.e. rows meeting the join condition) [the most popular join]
joining columns. Joining columns may appear redundantly in the result table
identical names. Plus, the result table contains only one column for each pair of joining columns
5
6
Creates a Cartesian product of rows in involved tables Finds the matching rows meeting the join condition(s) Identifies columns to be presented in the result table STEP 1 STEP 2 FROM WHERE SELECT STEP 3
SupplierID SupplierName
10 Walmart 20 Target 30 Best Buy
SELECT SupplierName, ShipmentDate FROM Supplier_T, Shipment_T WHERE Supplier_T.SupplierID=Shipment_T.SupplierID; SUPPLIER EQUI JOIN SQL STATEMENT
ShipmentID ShipmentDate SupplierID
1 10-10-2016 20 2 10-12-2016 30 3 11-01-2016
SHIPMENT
7
Step 1: FROM Supplier_T, Shipment_T Step 2: WHERE Supplier_T.SupplierID=Shipment_T.SupplierID Step 3: SELECT
SupplierID SupplierName ShipmentID ShipmentDate SupplierID
10 Walmart 1 10-10-2016 20 10 Walmart 2 10-12-2016 30 10 Walmart 3 11-01-2016 20 Target 1 10-10-2016 20 20 Target 2 10-12-2016 30 20 Target 3 11-01-2016 30 Best Buy 1 10-10-2016 20 30 Best Buy 2 10-12-2016 30 30 Best Buy 3 11-01-2016
SupplierID SupplierName ShipmentID ShipmentDate SupplierID
20 Target 1 10-10-2016 20 30 Best Buy 2 10-12-2016 30
SupplierName ShipmentDate
Target 10-10-2016 Best Buy 10-12-2016
8
This query produces the same results as the previous equi-join example MS SQL and ORACLE support the JOIN …ON syntax without the keyword INNER
ON clause performs the equality check for joining columns of the two tables Join (explicit) involves multiple tables in the FROM clause
SEL ELECT CT Cus ustom
erID, , Cust ustome
rName, , OrderID derID FROM OM Cus ustom
er_T INN NNER R JOIN Order_T er_T USIN ING (Cus ustom
erID) ORDER ER BY OrderID derID;
9
This query produces the same results as the previous equi-join example, except CustomerID column is not repeated INNER JOIN …USING syntax may not be supported by all RDBMS
USING (column name) clause performs the equality check for joining columns of the two tables with identical names Join (explicit) involves multiple tables in FROM clause You cannot use a qualifier in the SELECT clause for the joining column when INNER JOIN…USING syntax is used
10
NATURAL JOIN performs the join using CustomerID column. CustomerID is the attribute that appears in both tables This query produces a result table in which CustomerID column is not repeated NATURAL JOIN is not supported by all RDBMS (Oracle and MySQL support natural join) You cannot use a qualifier in the SELECT clause for the joining column when NATURAL JOIN syntax is used NATURAL JOIN is very similar to INNER JOIN…USING, except the column name is not explicitly mentioned
11
SELECT * FROM Item_T NATURAL JOIN Company_T;
No explicit mention of joining columns Company_ID that exists in both tables is used to join the tables Company_ID column is not repeated in the result table
12
13
Unlike INNER join, this will include customer rows with no matching order rows
14
14
15
Due to the referential integrity constraint, every order will have a valid customer ID. Hence, this query produces the same result table as the previous equi-join example
16
Hoffer, Jeffrey A., V. Ramesh, and Heikki Topi "Modern database management" 10th edition