Software Testing

k2G9Eo9
RdZQRt6

Popular Posts

JOINS

JOINS

Joinss
JOIN is same as INNER JOIN and means to only show records common to both tables. Whether the records are common is determined by the fields in the join clause. 

 
Employee
Location
EmpIDEmpNameemp_manager_id
13SandeepNULL
9Kailash1
4Ram1
17Nitin5
25Yogesh2
EmpIDEmpLoc
13San Jose
9Los Angeles
4Pune, India
17Chennai, India
39Bangalore, India

INNER JOIN:

Inner join returns only those records/rows that match/exists in both the tables. 

SQL Query:

SELECT e.emp_id, e.Emp_name, l.Emp_loc
FROM employee AS e
INNER JOIN location AS l
ON e.emp_id=l.emp_id

After Execution: 

EmpIDEmp_NameEmpLoc
13SandeepSan Jose
9KailashLos Angeles
4RamPune, India
17NitinChennai, India

LEFT JOIN:

LEFT JOIN is same as LEFT OUTER JOIN and A left outer join retains all of the rows of the “left” table means to show all records from the left table (i.e. the one that precedes in SQL statement) regardless of the existence of matching records in the right table. 

SQL Query:

SELECT e.emp_id, e.Emp_name, l.Emp_loc
FROM employee AS e
LEFT JOIN location AS l
ON e.emp_id=l.emp_id

After Execution: 

EmpIDEmp_NameEmpLoc
13SandeepSan Jose
9KailashLos Angeles
4RamPune, India
17NitinChennai, India
23YogeshNULL

 

RIGHT JOIN:

RIGHT JOIN is same as RIGHT OUTER JOIN and it returns all the rows from the right table means shows all records from the second (right) table and only matching records from first (left) table are displayed in the result set means opposite of LEFT JOIN. 

SQL Query:

SELECT e.emp_id, e.Emp_name, l.Emp_loc
FROM employee AS e
RIGHT JOIN location AS l
ON e.emp_id=l.emp_id

After Execution: 

EmpIDEmp_NameEmpLoc
13SandeepSan Jose
9KailashLos Angeles
4RamPune, India
17NitinChennai, India
NULLNULLMumbai, India

FULL OUTER JOIN:

A standard SQL FULL OUTER join is like a LEFT or RIGHT join, except that it includes all rows from both tables, matching them where possible and filling in with NULLs where there is no match. I’ll illustrate that for clarity. Here are two of my favorite tables, apples and oranges: 

Basic syntax as follow:
SELECT t1.column1, t1.column2, t1.column3, t2.column1, t2.column2
FROM table1 t1
FULL OUTER JOIN table2 t2 
ON t1.common_field = t2.common_field
SQL syntax:

SELECT e.Emp_id, e.Name, e.Surname, d.Dept_Name
FROM employee e
FULL OUTER JOIN department d 
ON e.dept_id = d.dept_id
NOTE: If your Database does not support FULL JOIN like MySQL does not support FULL JOIN, then you can use UNION ALL clause to combine two JOINS as follows:
MySQL Query:

SELECT e.Emp_id, e.Name, e.Surname, e.Salary, d.Dept_Name
FROM employee e
LEFT JOIN department d 
ON e.dept_id = d.dept_id
union all 
SELECT e.Emp_id, e.Name, e.Surname, e.Salary, d.Dept_Name
FROM employee e
RIGHT JOIN department d 
ON e.dept_id = d.dept_id

After Execution: 

EmpIDEmp_NameEmpLoc
13SandeepSan Jose
9KailashLos Angeles
4RamPune, India
17NitinChennai, India
23YogeshNULL
NULLNULLMumbai, India

CROSS JOIN:

This join is a Cartesian join that does not necessitate any condition to join. The resultset contains records that are a multiplication of record number from both the tables. 

SQL Query:

SELECT e.*,d.* FROM employee e, department d

After Execution:

EmpIDEmp_NameEmpLoc
13SandeepSan Jose
9KailashSan Jose
4RamSan Jose
17NitinSan Jose
23YogeshSan Jose
13SandeepLos Angeles
9KailashLos Angeles
4RamLos Angeles
17NitinLos Angeles
23YogeshLos Angeles
13SandeepPune, India
9KailashPune, India
4RamPune, India
17NitinPune, India
23YogeshPune, India
13SandeepMumbai, India
9KailashMumbai, India
4RamMumbai, India
17NitinMumbai, India
23YogeshMumbai, India
13SandeepChennai, India
9KailashChennai, India
4RamChennai, India
17NitinChennai, India
23YogeshChennai, India

SELF JOIN:

You can join a single table to itself.  In this case, you are using the same table twice. 
Example Table:
Emp_IDEmp_NameEmp_Mngr_Id
1SandeepNULL
2Kailash1
3Ram1
4Nitin2
5Yogesh2
6Sneha5
7Bhavesh5
SQL Query:

SELECT e1.Emp_name 'manager', e2.Emp_name 'employee'
FROM employees e1 JOIN employees e2
ON e1.emp_id=e2.emp_mngr_id
After Execution: 

Emp_IDManagerEmployee
1SandeepKailash
1SandeepRam
2KailashNitin
2KailashYogesh
5YogeshSneha
5YogeshBhavesh

No comments:

Post a Comment