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

SQL Part II.docx

Uploaded by

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

SQL Part II.docx

Uploaded by

royisha1509
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Structure Query Language

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)

Non Equi - Join


The relationship between the emp and salgrade tables is a non equi join because no column in emp table
directly corresponds to a column in the salgrade table. The relationship is obtained using an operator other
than = (equal).
Example: Select e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.losal and s.hisal;
[N.B Operators like <= and >= can be used but between is the simplest. We need to specify the low
value first and then the high value.]

Rules for Joining tables


The number of tables minus one = minimum number of join conditions.
If two candidate keys together form a primary key then we cannot use the rule of joins.

1 | Page Dr. Anirban Goswami


Structure Query Language

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.

2 | Page Dr. Anirban Goswami


Structure Query Language

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.

Rules for Set Operators


1. Select statements must select the same number of columns.
2. Corresponding columns must be of same data type.
3. Duplicate rows are automatically eliminated (Distinct cannot be used).
4. Column names from the first query appear in the result.
5. Order by clause appears at the end of the statement.
6. Order by column position only.
7. Set operators can be used in sub queries.
8. Select statements are executed from top to bottom.
9. Multiple set operators are possible with parenthesis if necessary.

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.

*************************

3 | Page Dr. Anirban Goswami

You might also like