Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL JOINS. WHY USE A JOIN? The SQL JOIN clause is used whenever we have to select data from 2 or more tables.

Similar presentations


Presentation on theme: "SQL JOINS. WHY USE A JOIN? The SQL JOIN clause is used whenever we have to select data from 2 or more tables."— Presentation transcript:

1 SQL JOINS

2 WHY USE A JOIN? The SQL JOIN clause is used whenever we have to select data from 2 or more tables

3 HOW TO USE A JOIN To be able to use the SQL JOIN clause to extract data from 2 (or more) tables, we need a relationship between certain columns in these tables.

4 TABLES USED FOR EXAMPLES CustomerIDFirstNameLastName DOBPhone CustomerIDDateSaleAmount 25/6/2004$ /7/2004$ /7/2004$ /13/2004$ /22/2004$ CUSTOMER TABLE: SALES TABLE: THE COMMON FIELD IS CustomerID

5 FIRST JOIN Select all distinct customers (their first and last names) and the total respective amount of dollars they have spent. SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer FROM Customers, Sales WHERE Customers.CustomerID = Sales.CustomerID GROUP BY Customers.FirstName, Customers.LastName The SQL JOIN condition has been specified after the SQL WHERE clause and says that the 2 tables have to be matched by their respective CustomerID columns.

6 RESULT? FirstNameLastNameSalesPerCustomers JohnSmith$99.95 StevenGoldfish$ PaulaBrown$ JamesSmith$555.55

7 USE JOIN INSTEAD OF WHERE SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer FROM Customers JOIN Sales ON Customers.CustomerID = Sales.CustomerID GROUP BY Customers.FirstName, Customers.LastName NOTICE THE USE OF ON WITH THE JOIN COMMAND

8 TWO (2) TYPES OF JOINS Inner Join – When you use the JOIN command, inner join is the default – Will select all rows from both tables as long as there is a match between the columns being matched

9 CustomerIDDateSaleAmount 25/6/2004$ /6/2004$99.95 INNER JOIN EXAMPLE What if the Sales table only had the following: And we use the same JOIN statement as before SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer FROM Customers JOIN Sales ON Customers.CustomerID = Sales.CustomerID GROUP BY Customers.FirstName, Customers.LastName

10 RESULT? FirstNameLastNameSalesPerCustomers JohnSmith$99.95 StevenGoldfish$ Even though Paul and James are listed in the Customers table, they wont be displayed because they havent ordered anything yet.

11 OUTER JOIN 2 SUB-TYPES Left Outer Join – Simply used as left join (outer is not required) – selects all the rows from the first table listed after the FROM clause, no matter if they have matches in the second table.

12 LEFT JOIN EXAMPLE What if we wanted to list all the customers and their sales even if they havent placed an order? The sales table still has the following: FirstNameLastNameSalesPerCustomers JohnSmith$99.95 StevenGoldfish$100.22

13 LEFT JOIN EXAMPLE contd SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer FROM Customers LEFT JOIN Sales ON Customers.CustomerID = Sales.CustomerID GROUP BY Customers.FirstName, Customers.LastName Replaced JOIN with LEFT JOIN

14 RESULT? FirstNameLastNameSalesPerCustomers JohnSmith$99.95 StevenGoldfish$ PaulaBrownNULL JamesSmithNULL Everything from the Customers (first table) has been selected. For all rows from Customers, which dont have a match in the Sales (second table), the SalesPerCustomer column has amount NULL (NULL means a column contains nothing).

15 RIGHT OUTER JOIN Simply used as left join (outer is not required) Behaves exactly as SQL LEFT JOIN, except that it returns all rows from the second table (the right table in our SQL JOIN statement).

16 CARTESIAN PRODUCT If the left table has 10 rows and the right table has 18 rows then SQL Join (if not written correctly) will return 180 rows combining each record of left table with all records of right side table.


Download ppt "SQL JOINS. WHY USE A JOIN? The SQL JOIN clause is used whenever we have to select data from 2 or more tables."

Similar presentations


Ads by Google