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 Prepared By : - Chintan Shah & Pankti Dharwa 2
SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables. Prepared By : - Chintan Shah & Pankti Dharwa 3
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. Prepared By : - Chintan Shah & Pankti Dharwa 4
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.column_na me Prepared By : - Chintan Shah & Pankti Dharwa 5
Prepared By : - Chintan Shah & Pankti Dharwa 6
SELECT tablename.column_name1,tablename.column_name2 FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_na me Prepared By : - Chintan Shah & Pankti Dharwa 7
Now we want to list all the persons with any orders. Prepared By : - Chintan Shah & Pankti Dharwa 8
The INNER JOIN keyword return rows when there is at least one match in both tables. If there are rows in j_person" that do not have matches in j_order", those rows will NOT be listed. Prepared By : - Chintan Shah & Pankti Dharwa 9
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 Prepared By : - Chintan Shah & Pankti Dharwa 10
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 tablename.column_name1,tablename.column_name2 FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name In some databases LEFT JOIN is called LEFT OUTER JOIN. Prepared By : - Chintan Shah & Pankti Dharwa 11
Prepared By : - Chintan Shah & Pankti Dharwa 12
Now we want to list all the persons and their orders - if any, from the tables above. Prepared By : - Chintan Shah & Pankti Dharwa 13
Prepared By : - Chintan Shah & Pankti Dharwa 14
The LEFT JOIN keyword returns all the rows from the left table (j_persons), even if there are no matches in the right table (j_orders). Prepared By : - Chintan Shah & Pankti Dharwa 15
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 tablename.column_name1,tablename.column_name2 FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_na me In some databases RIGHT JOIN is called RIGHT OUTER JOIN. Prepared By : - Chintan Shah & Pankti Dharwa 16
Now we want to list all the ORDERS with containing PERSON - if any, from the tables above. Prepared By : - Chintan Shah & Pankti Dharwa 17
The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table (Persons). Prepared By : - Chintan Shah & Pankti Dharwa 18
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. Prepared By : - Chintan Shah & Pankti Dharwa 19
Use cross join in person and order_details table. SELECT * FROM PERSON CROSS JOIN ORDER_DETAILS; OR SELECT * FROM PERSON,ORDER_DETAILS; Prepared By : - Chintan Shah & Pankti Dharwa 20
Prepared By : - Chintan Shah & Pankti Dharwa 21
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. Prepared By : - Chintan Shah & Pankti Dharwa 22
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 Prepared By : - Chintan Shah Output & Pankti Dharwa of the Union Clause 23
Table Name : - Emp_Ahmedabad E_ID FirstName LastName 1 Chintan Shah 2 Pankti Dharwa 3 Tarun Sharma Table Name : - Emp_Mumbai E_ID FirstName LastName 1 Bhavik Shah 2 Bhavesh Sharma Prepared By : - Chintan Shah & Pankti Dharwa 24
SELECT COLUMN_NAME(S) FROM TABLE_NAME1 UNION ALL SELECT COLUMN_NAME(S) FROM TABLE_NAME2 Prepared By : - Chintan Shah & Pankti Dharwa 25
SELECT FIRSTNAME,LASTNAME FROM EMP_AHMEDABAD UNION ALL SELECT FIRSTNAME,LASTNAME FROM EMP_MUMBAI FirstName Chintan Pankti Tarun Bhavik Bhavesh LastName Shah Dharwa Sharma Shah Sharma Prepared By : - Chintan Shah & Pankti Dharwa 26
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 Prepared By : - Chintan Shah & Pankti Dharwa 27
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 Prepared By : - Chintan Shah & Pankti Dharwa 28
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. Prepared By : - Chintan Shah & Pankti Dharwa 29
Table name : - Store_Sales Store_Name Sales Date_S Ahmedabad $1500 23-JUN-09 Mumbai $250 24-JUN-09 Ahmedabad $300 24-JUN-09 Chennai $700 25-JUN-09 Table Name : - Internet_Sales Store_Name Date_S Sales Ahmedabad 23-JUN-09 $1500 Mumbai 24-JUN-09 $250 Ahmedabad 24-JUN-09 $300 Chennai 25-JUN-09 $700 Prepared By : - Chintan Shah & Pankti Dharwa 30
SELECT STORE_NAME,DATE_S FROM STORE_SALES INTERSECT SELECT STORE_NAME,DATE_S FROM INTERNET_SALES OUTPUT STORE_NAME AHMEDABAD AHMEDABAD DATE_S 23-JUN-09 24-JUN-09 Prepared By : - Chintan Shah & Pankti Dharwa 31
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 Prepared By : - Chintan Shah & Pankti Dharwa 32
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. Prepared By : - Chintan Shah & Pankti Dharwa 33
Table name : - Store_Sales Store_Name Sales Date_S Ahmedabad $1500 23-JUN-09 Mumbai $250 24-JUN-09 Baroda $300 27-JUN-09 Rajkot $700 26-JUN-09 Table Name : - Internet_Sales Store_Name Date_S Sales Ahmedabad 23-JUN-09 $1500 Mumbai 24-JUN-09 $250 Ahmedabad 24-JUN-09 $300 Chennai 25-JUN-09 $700 Prepared By : - Chintan Shah & Pankti Dharwa 34
SELECT STORE_NAME,DATE_S FROM STORE_SALES MINUS SELECT STORE_NAME,DATE_S FROM INTERNET_SALES OUTPUT STORE_NAME Baroda Rajkot DATE_S 27-JUN-09 26-JUN-09 Prepared By : - Chintan Shah & Pankti Dharwa 35