Presentation is loading. Please wait.

Presentation is loading. Please wait.

JOINS (Joinining multiple tables)

Similar presentations


Presentation on theme: "JOINS (Joinining multiple tables)"— Presentation transcript:

1

2 JOINS (Joinining multiple tables)
Sometimes it necessary to work with multiple tables as through they were a single entity. Then single SQL sentence can manipulate data from all the tables. Join are used to achive this. Tables are joined on columns that have the same data type and data width in the tables Types Inner join ( Equi Joins ) Outer(left, right, full) Cross

3 joins SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.

4 INNER JOIN Inner join are also known as equi join.There are the most common joins used in SQL. They are known as equi joins because the where statement generally compares two columns from two tables with the equivalence operator =. The INNER JOIN returns all rows from the both tables where there is match.

5 eXAMPLE The INNER JOIN keyword return rows when there is at least one match in both tables. Syntax : SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.col umn_name

6 Example The "Persons" table: The “Orders" table: P_ID LASTNAME
FIRSTNAME ADDRESS CITY 1 Patel sanjay Gandhinagar 2 Pandya Nitin 3 Shah Chintan Ahemedabad The “Orders" table: O_Id OrderNo P_ID 1 77895 3 2 44678 22456 4 24562 5 34764 15

7 Example Now we want to list all the persons with any orders.
We use the following SELECT statement: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName

8 example The result-set will look like this:
LASTNAME FIRSTNAME OrderNO Patel Sanjay 22456 sanjay 24562 Shah Chintan 77895 44678 The INNER JOIN keyword return rows when there is at least one match in both tables. If there are rows in "Persons" that do not have matches in "Orders", those rows will NOT be listed.

9 OUTER JOIN Outer joins are similar to inner joins, but a bit more flexibility when selecting data from a related tables. This type of join can be used in situations where it desired, to select all rows from the table on the left(or right, or both) regardless of whether the other table has values in common and enter NULL where data is missing. LEFT JOIN RIGHT JOIN

10 LEFT JOIN The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2). Syntax : SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.colum n_name In some databases LEFT JOIN is called LEFT OUTER JOIN.

11 Example The "Persons" table: The “Orders" table: P_ID LASTNAME
FIRSTNAME ADDRESS CITY 1 Patel sanjay Gandhinagar 2 Pandya Nitin 3 Shah Chintan Ahemedabad The “Orders" table: O_Id OrderNo P_ID 1 77895 3 2 44678 22456 4 24562 5 34764 15

12 example Now we want to list all the persons and their orders - if any, from the tables above. We use the following SELECT statement: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName

13 example The result-set will look like this: LASTNAME FIRSTNAME OrderNO Patel Sanjay 22456 sanjay 24562 Shah Chintan 77895 44678 Pandya Nitin The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in the right table (Orders).

14 Right join The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1). Syntax : SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.colum n_name In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

15 Example The "Persons" table: The “Orders" table: P_ID LASTNAME
FIRSTNAME ADDRESS CITY 1 Patel sanjay Gandhinagar 2 Pandya Nitin 3 Shah Chintan Ahemedabad The “Orders" table: O_Id OrderNo P_ID 1 77895 3 2 44678 22456 4 24562 5 34764 15

16 example Now we want to list all the orders with containing persons - if any, from the tables above. We use the following SELECT statement: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName

17 example The result-set will look like this: LASTNAME FIRSTNAME OrderNO Patel Sanjay 22456 sanjay 24562 Shah Chintan 77895 44678 34764 The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table (Persons).

18 CROSS JOIN A cross join (or Cartesian Product join) will return a result table where each row from the first table is combined with each row from the second table. The number of rows in the result table is the product of the number of rows in each table. A cross join can be specified in two ways: using the JOIN syntax or by listing the tables in the FROM clause separated by commas without using a WHERE clause to supply join criteria.

19 Example Use cross join in person and order_details table.
SELECT * FROM PERSON CROSS JOIN ORDER_DETAILS; OR SELECT * FROM PERSON,ORDER_DETAILS;

20 oUTPUT

21 Self join In some situations , it is necessary to join a table to itself, as though joining a two separate tables. This is referred to as a self-join. In a self-join two rows from the same table combine to form a result row. To join a table to itself, two copies of the very same table have to opened in memory. Hence in the FORM clause , the table needs to be mentioned twice.

22 Union Clause Multiple queries can be put together and their output can be combined using the union clause. The union clause merges the output of two or more queries into a single set of rows and columns. Records only in Query one Common Records From both queries Records only in Query one Output of the Union Clause

23 example

24 Restrictions Number of columns in all the queries should be the same
The data type of the columns in each query must be same Unions can not be use in sub queries Aggregate functions cannot be used with union clause

25 Intersect clause Multiple queries can be put together and their output combined using the intersect clause. The Intersect clause outputs only rows produced by both the queries intersect i.e. the output in an intersect clause will include only those rows that are retrieved common to both the queries. Common Records From both queries Output of the Intersect Clause

26 Intersect clause INTERSECT does not ignore NULL values.
The number of columns and the data types of the columns being selected by the SELECT statement in the queries must be identical in all the SELECT statements used in the query.

27 example

28 Minus clause Multiple queries can be put together and their output combined using the minus clause. The Minus clause outputs the rows produced by the first query , after filtering the rows retrieved by the second query. Records only in Query one Output of the Minus Clause

29 Minus clause The number of columns and the data types of the columns being selected by the SELECT statement in the queries must be identical in all the SELECT statements used in the query. All on the columns in the WHERE clause must be in the SELECT clause for the minus operator to work.

30 example


Download ppt "JOINS (Joinining multiple tables)"

Similar presentations


Ads by Google