SQL Joins
Advertisements
SQL Joins
In general meaning join means combined something, In SQL using join keyword you can join two tables. Joins clause is used to combine rows from two or more tables, based on a common field between them.
There are following joins are available in SQL
- Inner Join
- Left Join
- Right Join
- Full Join
When SQL Join is used ?
- When you need to show record of two table combinedly.
- If you want to access more than one table through a select statement.
- If you want to combine two or more table.
- If you want to joint two or more table based on common field between
- To combine rows from two or more tables, based on a common field between them.
- Inner Join: Returns all rows when there is at least one match in both tables.
- Left Join: Return all rows from the left table, and the matched rows from the right table.
- Right Join: Return all rows from the right table, and the matched rows from the left table.
- Full Join: Return all rows when there is a match in ONE of the tables.
Example of Join
Below we combined two table, table employee and table payment.
Employee table
Emp_id | Name | Age |
---|---|---|
101 | Amit Sukla | 24 |
102 | Rahul jain | 34 |
103 | Sultan Alam | 54 |
104 | Gaurav rawat | 26 |
105 | Hitesh Kumar | 35 |
Payment table
Payment_id | Date | Salary |
---|---|---|
101 | 20-01-2014 | 20000 |
102 | 23-03-2014 | 45000 |
103 | 12-02-2014 | 50000 |
104 | 28-07-2014 | 55000 |
105 | 20-11-2014 | 40000 |
After apply join on these two table, result show like below;
Example
SELECT emp_id, name, age,salary FROM Employee e, Payment p WHERE e.Emp_id =p.Payment_id;
In above syntax we select 4 columns, emp_id, name, age and salary from table Employee and table Payment. And retrieving all rows where the employee identification number is the same in both the Employee and Payment tables.
Payment_id | Name | Age | Salary |
---|---|---|---|
101 | Amit Sukla | 24 | 20000 |
102 | Rahul jain | 34 | 45000 |
103 | Sultan Alam | 54 | 50000 |
104 | Gaurav rawat | 26 | 55000 |
105 | Hitesh Kumar | 35 | 40000 |
Google Advertisment