Q - WHAT IS A JOIN IN THE DATABASE ?
ans-A join is an SQL operation performed to establish a connection between two or more database tables based on matching columns, thereby creating a relationship between the tables. Most complex queries in an SQL database management system involve join commands.
There are different types of joins. The type of join a programmer uses determines which records the query selects. Three algorithms work behind join operations: hash join, sort-merge join and nested loop join.
Q-DIFFERENCE BETWEEN NATURAL JOIN AND OTHER INNER JOINS
ANS -
ans-A join is an SQL operation performed to establish a connection between two or more database tables based on matching columns, thereby creating a relationship between the tables. Most complex queries in an SQL database management system involve join commands.
There are different types of joins. The type of join a programmer uses determines which records the query selects. Three algorithms work behind join operations: hash join, sort-merge join and nested loop join.
Q- what are the different types of join in the database ?
ans -
tAn SQL
JOIN clause is used to combine rows from two or more tables, based on a common field between them.
There are many types of
join.- Inner Join
- Equi-join
- Natural Join
- thita join
- Outer Join
- Left outer Join
- Right outer join
- Full outer join
- Cross Join
- Self Join
INNER JOIN
We understand the benefits of taking a Cartesian product of two relations, which gives us all the possible tuples that are paired together. But it might not be feasible for us in certain cases to take a Cartesian product where we encounter huge relations with thousands of tuples having a considerable large number of attributes.
Join is a combination of a Cartesian product followed by a selection process. A Join operation pairs two tuples from different relations, if and only if a given join condition is satisfied.
We will briefly describe various join types in the following sections.
Theta (θ) Join
Theta join combines tuples from different relations provided they satisfy the theta condition. The join condition is denoted by the symbol θ.
Notation
R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that the attributes don’t have anything in common, that is R1 ∩ R2 = Φ.
Theta join can use all kinds of comparison operators.
| Student | ||
|---|---|---|
| SID | Name | Std |
| 101 | Alex | 10 |
| 102 | Maria | 11 |
| Subjects | |
|---|---|
| Class | Subject |
| 10 | Math |
| 10 | English |
| 11 | Music |
| 11 | Sports |
Student_Detail −
STUDENT ⋈Student.Std = Subject.Class SUBJECT
| Student_detail | ||||
|---|---|---|---|---|
| SID | Name | Std | Class | Subject |
| 101 | Alex | 10 | 10 | Math |
| 101 | Alex | 10 | 10 | English |
| 102 | Maria | 11 | 11 | Music |
| 102 | Maria | 11 | 11 | Sports |
Equijoin
When Theta join uses only equality comparison operator, it is said to be equijoin. The above example corresponds to equijoin.
Natural Join (⋈)
Natural join does not use any comparison operator. It does not concatenate the way a Cartesian product does. We can perform a Natural Join only if there is at least one common attribute that exists between two relations. In addition, the attributes must have the same name and domain.
Natural join acts on those matching attributes where the values of attributes in both the relations are same.
| Courses | ||
|---|---|---|
| CID | Course | Dept |
| CS01 | Database | CS |
| ME01 | Mechanics | ME |
| EE01 | Electronics | EE |
| HoD | |
|---|---|
| Dept | Head |
| CS | Alex |
| ME | Maya |
| EE | Mira |
| Courses ⋈ HoD | |||
|---|---|---|---|
| Dept | CID | Course | Head |
| CS | CS01 | Database | Alex |
| ME | ME01 | Mechanics | Maya |
| EE | EE01 | Electronics | Mira |
Q-DIFFERENCE BETWEEN NATURAL JOIN AND OTHER INNER JOINS
ANS -
- Inner join can have equality (=) and other operators (like <,>,<>) in the join condition.
- Equi join only have equality (=) operator in the join condition.
- Equi join can be an Inner join, Left Outer join, Right Outer join
- The USING clause is not supported by SQL Server and Sybase. This clause is supported by Oracle and MySQL.
- IN CASE OF NATURAL JOIN NEW TABLE CONTAINS REMOVE REDUNDENT COLUMNS
Outer JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
- Left Outer Join
- Right Outer Join
- Full Outer Join
Left Outer Join
The left outer join returns a result table with the matched data of two tables then remaining rows of the left table and null for the right table's column.
Left Outer Join syntax is,
SELECT column-name-list from table-name1 LEFT OUTER JOIN table-name2 on table-name1.column-name = table-name2.column-name;
Left outer Join Syntax for Oracle is,
select column-name-list from table-name1, table-name2 on table-name1.column-name = table-name2.column-name(+);
Example of Left Outer Join
The class table,
| ID | NAME |
|---|---|
| 1 | abhi |
| 2 | adam |
| 3 | alex |
| 4 | anu |
| 5 | ashish |
The class_info table,
| ID | Address |
|---|---|
| 1 | DELHI |
| 2 | MUMBAI |
| 3 | CHENNAI |
| 7 | NOIDA |
| 8 | PANIPAT |
Left Outer Join query will be,
SELECT * FROM class LEFT OUTER JOIN class_info ON (class.id=class_info.id);
The result table will look like,
| ID | NAME | ID | Address |
|---|---|---|---|
| 1 | abhi | 1 | DELHI |
| 2 | adam | 2 | MUMBAI |
| 3 | alex | 3 | CHENNAI |
| 4 | anu | null | null |
| 5 | ashish | null | null |
Right Outer Join
The right outer join returns a result table with the matched data of two tables then remaining rows of the right table and null for the left table's columns.
Right Outer Join Syntax is,
select column-name-list from table-name1 RIGHT OUTER JOIN table-name2 on table-name1.column-name = table-name2.column-name;
Right outer Join Syntax for Oracle is,
select column-name-list from table-name1, table-name2 on table-name1.column-name(+) = table-name2.column-name;
Example of Right Outer Join
The class table,
| ID | NAME |
|---|---|
| 1 | abhi |
| 2 | adam |
| 3 | alex |
| 4 | anu |
| 5 | ashish |
The class_info table,
| ID | Address |
|---|---|
| 1 | DELHI |
| 2 | MUMBAI |
| 3 | CHENNAI |
| 7 | NOIDA |
| 8 | PANIPAT |
Right Outer Join query will be,
SELECT * FROM class RIGHT OUTER JOIN class_info on (class.id=class_info.id);
The result table will look like,
| ID | NAME | ID | Address |
|---|---|---|---|
| 1 | abhi | 1 | DELHI |
| 2 | adam | 2 | MUMBAI |
| 3 | alex | 3 | CHENNAI |
| null | null | 7 | NOIDA |
| null | null | 8 | PANIPAT |
Full Outer Join
The full outer join returns a result table with the matched data of two table then remaining rows of both left table and then the right table.
Full Outer Join Syntax is,
select column-name-list from table-name1 FULL OUTER JOIN table-name2 on table-name1.column-name = table-name2.column-name;
Example of Full outer join is,
The class table,
| ID | NAME |
|---|---|
| 1 | abhi |
| 2 | adam |
| 3 | alex |
| 4 | anu |
| 5 | ashish |
The class_info table,
| ID | Address |
|---|---|
| 1 | DELHI |
| 2 | MUMBAI |
| 3 | CHENNAI |
| 7 | NOIDA |
| 8 | PANIPAT |
Full Outer Join query will be like,
SELECT * FROM class FULL OUTER JOIN class_info on (class.id=class_info.id);
The result table will look like,
| ID | NAME | ID | Address |
|---|---|---|---|
| 1 | abhi | 1 | DELHI |
| 2 | adam | 2 | MUMBAI |
| 3 | alex | 3 | CHENNAI |
| 4 | anu | null | null |
| 5 | ashish | null | null |
| null | null | 7 | NOIDA |
| null | null | 8 | PANIPAT |
No comments:
Post a Comment