Top Banner
JOINS IN DATABASE
12
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 1. JOINS IN DATABASE

2. Definition Of JOINS :- An SQL join clause combines records from two or more tables in a database. It creates a set that can be saved as a table or used as is. A JOIN is a means for combining fields from two tables by using values common to each. 3. NaturalJoinSelf JOINEquiJoin JoinS Outer Join 4. Natural Join - The NATURAL JOIN is a type of equi-join and is structured in such a way that, columns with same name of associate tables will appear once only. Eg - Select * FROM table1 NATURAL JOIN table2; EquiJoin - SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the associated tables. An equal sign (=) is used as comparison operator in the where clause to refer equality.Eg - Select column_list FROM table1, table2 WHERE table1.column_name =table2.column_name 5. Self Join - SELF JOIN is used to join a table to itself, as if the table were two tables, temporarily renaming at least one table in the SQL statement.Eg - SELECT a.column_name, b.column_name FROMtable1 a, table1 b WHERE a.common_filed =b.common_field; Outer Join Right Outer Join - Left Outer Join 6. Right Outer Join - The LEFT JOIN keyword returnsall rows from the left table (table_name1), even if thereare no matches in the right table (table_name2).Eg - SELECT column_name(s) FROM table_name1 LEFT JOINtable_name2 ON table_name1.column_name=table_name2.column_name Left Outer Join - The RIGHT JOIN keywordreturns all the rows from the right table(table_name2), even if there are no matches in theleft table (table_name1). Eg -SELECT column_name(s) FROM table_name1 RIGHT JOINtable_name2 ON table_name1.column_name=table_name2.column_name 7. A SQL nested query is a SELECT query that isnested inside a SELECT, UPDATE, INSERT, orDELETE SQL query. Here is a simple exampleof SQL nested query:Eg - SELECT Model FROM Product WHERE ManufacturerID IN (SELECT ManufacturerID FROM Manufacturer WHERE Manufacturer = Dell) 8. UNIONALL - Combines the results of two SELECT statements into one result set. SELECTCUST_NBR, NAME FROM CUSTOMER WHERE REGION_ID = 5 UNION ALL SELECT C.CUST_NBR, C.NAME FROM CUSTOMER C WHERE C.CUST_NBR IN (SELECT O.CUST_NBR FROM CUST_ORDER O, EMPLOYEE E WHERE O.SALES_EMP_ID = E.EMP_ID AND E.LNAME = MARTIN); 9. UNION - Combines the results of two SELECTstatements into one result set, and theneliminates any duplicate rows from that resultset.Eg - SELECT CUST_NBR, NAME FROM CUSTOMER WHERE REGION_ID = 5 UNION SELECT C.CUST_NBR, C.NAME FROM CUSTOMER C WHERE C.CUST_NBR IN (SELECT O.CUST_NBR FROM CUST_ORDER O, EMPLOYEE E WHERE O.SALES_EMP_ID = E.EMP_ID AND E.LNAME = MARTIN); 10. MINUS - Takes the result set of one SELECTstatement, and removes those rows that are alsoreturned by a second SELECT statement. Eg - SELECT CUST_NBR, NAME FROM CUSTOMERWHERE REGION_ID = 5 MINUS SELECTC.CUST_NBR, C.NAME FROM CUSTOMER C WHEREC.CUST_NBR IN (SELECT O.CUST_NBR FROMCUST_ORDER O, EMPLOYEE E WHEREO.SALES_EMP_ID = E.EMP_ID AND E.LNAME =MARTIN); 11. INTERSECT - Returns only those rows that are returnedby each of two SELECT statements. Eg - SELECT CUST_NBR, NAME FROM CUSTOMERWHERE REGION_ID = 5 INTERSECT SELECTC.CUST_NBR, C.NAME FROM CUSTOMER C WHEREC.CUST_NBR IN (SELECT O.CUST_NBR FROMCUST_ORDER O, EMPLOYEE E WHEREO.SALES_EMP_ID = E.EMP_ID AND E.LNAME =MARTIN);