SQL Part II.docx
SQL Part II.docx
Join in SQL
A join is used when a SQL query requires data from more than one table on database. Rows in one table
may be joined to rows in another table according to common values existing in corresponding columns.
There are two main types of join condition:
1) Equi Join
2) Non Equi Join
Equi-Join
The relationship between the Emp and Dept table are equi-join because the values in the deptno column of
both the tables are same. For equi-join we use “ = ” operator.
Syntax: Select column(s) from tables where join condition is.
To join the two tables EMP and DEPT, enter
Select ename, job, dname from emp, dept where emp.deptno = dept.deptno;
To select between the deptno. Column in the emp and dept table we have to write
Select dept.deptno, ename, job, dname from emp, dept where dept.deptno = emp.deptno order by
dept.deptno;
To use table alias (Temporary labels) we write:
Select e,ename, d.deptno, d.dname from Emp e, dept d where e.deptno = d.deptno order by d.deptno;
Product
When a join condition is invalid or omitted completely the result is a cross product and each row in the emp
table is linked to each row in the dept table. (Cartesian Product)
Exercise:
1) Display all employee names and their department name in department name order.
2) Display all employee names, department number and name.
3) Display the name, location and department of employees whose salary is more than 1500 a month.
4) Display ename, job, sal and grade of all employees.
5) Display ename, job, sal and grade of all employees in grade 3.
6) Display ename, sal and location of all employees in Dallas.
7) List employee name, job, salary, grade and department name for everyone in the company except clerks.
Sort on salary displaying the highest salary first.
8) List ename, job, annual_sal, deptno, dname and grade of all employees who earn 36000 a year or who
are clerks.
Outer Join
If a row doesnot satisfy a join condition then that row will not appear in the query result. In the equi- join of
emp and dept table department 40 doesnot appear. This is because there are no employees in department 40.
The missing rows can be returned if an outer join operator is used in the join condition. The operator is the
plus sign enclosed in parenthesis (+) and is placed on the side of the join table which is deficient in
information. The operator has the effect of creating one or more Null rows, to which one or more rows from
the non-deficient table can be joined. One null row is created for every additional row in the non- deficient
table.
Example: Select e.ename, d.deptno, d.dname from emp e, dept d where e.deptno (+) = d.deptno and
d.deptno in (30, 40);
Restrictions in Outer- Join:
1) We may not outer-join the same table to more than one other table in a single Select statement.
2) A condition involving an outer – join may not use the IN operator or be linked to another condition
by the OR operator.
Self Join
A table can be joined to itself using table alias, as if they are two separate tables.
This allows rows in a table to be joined to rows in the same table.
Select e.ename emp_name,
e.sal emp_sal,
m.ename mgr_name,
m.sal mgr_sal,
from emp e, emp m,
where e.mgr=m.empno and
e.sal < m.sal.
The above query displays all employees who earn less than their manager.
Emp has been given an alias e and m.
Set Operators
Union:
To return all distinct rows retrieved by either of the queries.
Select job from emp where deptno=10 Union select job from emp where deptno = 30.
To return all rows including duplicates retrieved by either of the queries.
Select job from emp where deptno=10 Union ALL select job from emp where deptno = 30.
Intersect:
To return only rows retrieved by both of the queries.
Select job from emp where deptno=10 INTERSECT select job from emp where deptno = 30.
Minus
To return all rows retrieved by first query that are not in the second.
Select job from emp where deptno=10 MINUS select job from emp where deptno = 30.
Exercise:
1) Display deptno and dname of all departments that has no employee.
2) List all employees by their name and number along with their manager’s name and number.
3) Find all employees who joined the company before their manager.
*************************