0% found this document useful (0 votes)
10 views

Join Query

query

Uploaded by

pushpa kesarwani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Join Query

query

Uploaded by

pushpa kesarwani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Dr.

Pushpa Choudhary

JOIN QUERY
SQL (Join Query)
Join Query:
 It is used to relate two or more tables in the form of Parent/Child
relationship.

 Only one select statement is required to define multiple tables but


all the tables must be related with the help of a condition known as
Join Condition.

 How to define Join condition


Number of Join Conditions = Number of Tables – 1

 If Join Conditions is not defined then output will be Multiple of


Records from all the Tables.
SQL (Join Query)
Type of Joins:
There are following types of Joins:
• Equi Join or inner join
• Non-equi Join
• Self Join
• Outer Join
left outer join
right outer join
full outer join

Equi Join:
When two tables relation is based on Common Columns / Values (or based on
Equality). For example: EMP and DEPT tables are related via DEPTNO common
column (like emp.deptno = dept.deptno).

Example:

SELECT dept.dname, emp.ename


FROM dept, emp
WHERE dept.deptno = emp.deptno;
Cross JOIN or Cartesian Product

This type of JOIN returns the Cartesian product of rows from the tables in Join. It will return a
table which consists of records which combines each row from the first table with each row of
the second table.
SELECT column_COLUMN_LIST FROM table_NAME1 CROSS JOIN
table_NAME2;
SELECT * FROM EMP CROSS JOIN DEPT;
OR
SELECT * FROM EMP,DEPT;
INNER Join or EQUI Join

This is a simple JOIN in which the result is based on matched data as per the equality condition
specified in the SQL query.

SELECT dept.dname, emp.ename FROM dept INNER JOIN emp


ON dept.deptno = emp.deptno;

SELECT * FROM dept INNER JOIN emp ON EMP.DEPTNO=DEPT.DEPTNO;


COMMON COLUMN BETWEEN TABLES DISPLAY AS PER TABLE RECORD THAT IS
FROM BOTH TABLE.
SQL INNER JOIN Keyword
• SQL INNER JOIN Keyword:

• The INNER JOIN keyword selects records


that have matching values in both tables.
• INNER JOIN Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = SELECT emp.ename,
table2.column_name; dept.dname,salgrade.grade
FROM ((emp
SELECT emp.ename, dept.dname INNER JOIN dept ON emp.deptno =
dept.deptno)
FROM emp
INNER JOIN salgrade ON emp.sal between
INNER JOIN dept ON emp.deptno =
salgrade.losal and salgrade.hisal);
dept.deptno;
Natural JOIN

Natural Join is a type of Inner join which is based on column having same name and same
datatype present in both the tables to be joined.

SELECT dept.dname, emp.ename FROM dept NATURAL JOIN emp;

SELECT * FROM dept NATURAL JOIN emp;


COMMON COLUMN BETWEEN TABLE WILL DISPLAY ONLY ONE TIME
SQL (Join Query)
Non-equi Join:
When two tables relation is based on Range of Values (or based on other than Equality). For
example: EMP and SALGRADE tables are related via SAL from emp
table and LOSAL & HISAL from salgrade table (like emp.sal BETWEEN salgrade.losal AND
salgrade.hisal).
Example:
SELECT emp.ename, salgrade.grade
FROM emp, salgrade
WHERE emp.sal BETWEEN salgrade.losal AND salgrade.hisal;
OR
SELECT emp.ename “employee_name”, salgrade.grade “salary_grade”
FROM emp, salgrade
WHERE emp.sal BETWEEN salgrade.losal AND salgrade.hisal;
SQL (Join Query)
Self Join :
When a table is related to itself because it keeps data for two entities within it. Like
EMP table keeps Employee and Manager entities. In case of self join table alias are
must since same table is used two or more times. For example:

• Display Employee Name & Manager Name for all employees.

Select E.ename, M.ename from EMP E,EMP M


Where E.mgr=M.empno;

OR
Select E.ename "employee_name", M.ename "manager_name" from EMP E,EMP M
Where E.mgr=M.empno;

• Display those employees who joined the company before their Managers

Select E.ename "employee_name", e.hiredate, M.ename "manager_name", m.hiredate from


EMP E, EMP M
Where E.mgr=M.empno and e.hiredate<m.hiredate;
SQL (Join Query)
Outer Join:
When we need to select Matching Records as well as Non Matching Records from
two tables. For Outer Join Query we need to use Outer Join Operator (+). For
Example:
• Display all Department names and Employee names including the Department not
having any employee.
SELECT D.dname, E.ename FROM emp E, dept D
WHERE e.deptno (+) = d.deptno;

• Display all Employees name and Managers name including the Employee not
having a Manager.
SELECT E.ename, M.ename FROM emp E, emp M
WHERE E.mgr = M.empno(+);

You might also like