80 likes | 180 Views
Good Afternoon. Joining. By using join, we can retrieve data from two or more tables based on logical relationships between the tables. A join condition defines the way two tables are related in a query by:
E N D
Joining • By using join, we can retrieve data from two or more tables based on logical relationships between the tables. • A join condition defines the way two tables are related in a query by: • Specifying the column from each table to be used for the join. A typical join condition specifies a foreign key from one table and its associated key in the other table. • Specifying a logical operator (=,<>,and so on) to be used in comparing values from the columns.
Types of Join • Inner join • Outer join • Left outer join • Right outer join • Cross join
Inner Join Inner join extracts only matching records from tables matched on a single column. Syntax: Select <col name,…..> from <table1 name> JOIN <table 2 name> ON <Primary key = foreign key> [where <condition>] Ex: select productid, suppliers.suppliersid, companyname from suppliers join products on (suppliers.supplierid=products.supplierid)
Left Outer Join Left outer join extracts all rows from the first-named table. Unmatched rows in the right table do not appear. Syntax: Select <col name,…..> from <table1 name> LEFT OUTER JOIN <table 2 name> ON <Primary key = foreign key> [where <condition>] Ex: select productid, suppliers.suppliersid, companyname from suppliers left outer join products on (suppliers.supplierid=products.supplierid)
Right Outer Join Right outer join extracts all rows from the second-named table. Unmatched rows in the left table do not appear. Syntax: Select <col name,…..> from <table1 name> RIGHT OUTER JOIN <table 2 name> ON <Primary key = foreign key> [where <condition>] Ex: select productid, suppliers.suppliersid, companyname from suppliers right outer join products on (suppliers.supplierid=products.supplierid)
Cross Join It is a join whose result set includes one row for each possible pairing of rows from the two tables. Syntax: Select <col name,…..> from <table1 name> CROSS JOIN <table 2 name> ON <Primary key = foreign key> [where <condition>] Ex: select productid, suppliers.suppliersid, companyname from suppliers cross join products on (suppliers.supplierid=products.supplierid)