Quality Testing

Quality is delighting customers

Any one explain about JOINs & types of JOINS in Data base.

Views: 111

Reply to This

Replies to This Discussion

Hi Suvarna,

Joins are the methods used to access related data from one or more tables. there are five types of join
1. Equi join(i.e. Inner join)
2. self join
3. Outer Join
4. Left Outer Join
5. Right Outer Join


Inner Join: Combine information from two or more tables by comparing all values that meet the search criteria in the designated column or columns of one table with all the values in corresponding columns of the other table or tables. This kind of join which involve a match in both columns are called inner joins.

Self Join: Self join is just like any other join, except that two instances of the same table will be joined in the query.

Outer Join: Outer Join Is one in which you want both matching and non matching rows to be returned

Left outer Join: A Left outer Join one of the JOIN operations that allow you to specify a join clause. It preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table.

Right Outer Join: A right outer join will return all the rows that an inner join returns plus one row for each of the other rows in the second table that did not have a match in the first table. It is the same as a left outer join with the tables specified in the opposite order

Regards
Gowtham
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.

Tables in a database are often related to each other with keys.

A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.


Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same name.

Next, we have the "Orders" table:


Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id" column refers to the persons in the "Persons" table without using their names.

Notice that the relationship between the two tables above is the "P_Id" column.

Different SQL JOINs:

Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.

a. JOIN: Return rows when there is at least one match in both tables
b. LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
c. RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
d. FULL JOIN: Return rows when there is a match in one of the tables

SQL INNER JOIN Keyword:

The INNER JOIN keyword return rows when there is at least one match in both tables.

SQL INNER JOIN Syntax


PS: INNER JOIN is the same as JOIN.

SQL INNER JOIN Example:

The "Persons" table:


The "Orders" table:


Now we want to list all the persons with any orders.

We use the following SELECT statement:


The result-set will look like this:


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.

SQL LEFT JOIN Keyword:

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).

SQL LEFT JOIN Syntax


PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.

SQL LEFT JOIN Example

The "Persons" table:


The "Orders" table:


Now we want to list all the persons and their orders - if any, from the tables above.

We use the following SELECT statement:


The result-set will look like this:


The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in the right table (Orders).

SQL RIGHT JOIN Keyword:

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).

SQL RIGHT JOIN Syntax


PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

SQL RIGHT JOIN Example

The "Persons" table:


The "Orders" table:


Now we want to list all the orders with containing persons - if any, from the tables above.

We use the following SELECT statement:


The result-set will look like this:


The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table (Persons).

SQL FULL JOIN Keyword:

The FULL JOIN keyword return rows when there is a match in one of the tables.

SQL FULL JOIN Syntax


SQL FULL JOIN Example

The "Persons" table:


The "Orders" table:


Now we want to list all the persons and their orders, and all the orders with their persons.

We use the following SELECT statement:


The result-set will look like this:


The FULL JOIN keyword returns all the rows from the left table (Persons), and all the rows from the right table (Orders). If there are rows in "Persons" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in "Persons", those rows will be listed as well.
Good and KK ji

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2020   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service