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

Set Operators: 1. Union

Set operators combine the results of two queries into a single result. There are four set operators: UNION combines results and removes duplicates; UNION ALL combines results but keeps duplicates; INTERSECT returns rows common to both queries; and MINUS returns rows that are in the first query but not the second.

Uploaded by

Sanju Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Set Operators: 1. Union

Set operators combine the results of two queries into a single result. There are four set operators: UNION combines results and removes duplicates; UNION ALL combines results but keeps duplicates; INTERSECT returns rows common to both queries; and MINUS returns rows that are in the first query but not the second.

Uploaded by

Sanju Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

SET OPERATORS

-Combine the results of two queries into single one


-Rule:
queries using set operators should have same number of columns and
corresponding columns should be of same data types.
1. UNION
Returns all distinct rows by both queries
< query 1> UNION < query2>;
2. UNION ALL
Returns all rows by both the queries including duplicates
< query 1> UNION ALL< query2>;
3. INTERSECT
Returns common rows by both the queries
< query 1> INTERSECT < query2>;
4. MINUS
Returns distinct rows in the first query
< query 1> MINUS < query2>;
<query 1> <set operator> <query 2>
The set operators are:
• union [all] returns a table consisting of all rows either appearing in the result of <query1> or in the result of
<query 2>. Duplicates are automatically eliminated unless the clause all is used.
• intersect returns all rows that appear in both results <query 1> and <query 2>.
• minus returns those rows that appear in the result of <query 1> but not in the result of <query 2>.
Example: Assume that we have a table EMP2 that has the same structure and columns as the table EMP:

• All employee numbers and names from both tables:


select EMPNO, ENAME from EMP
union
select EMPNO, ENAME from EMP2;
• Employees who are listed in both EMP and EMP2:

select * from EMP


intersect
select * from EMP2;
• Employees who are only listed in EMP:

select * from EMP


minus
select * from EMP2;

Each operator requires that both tables have the same data types for the columns to which the operator is
applied.
JOINS

To combine the datas from many tables.


It is performed by WHERE Clause which combines the specified rows of the tables.

Type Sub type Description


Simple join Equi join ( = ) Joins rows using equal value of the column
Non – equi join (<, <=, >, >=, !=, < > ) Joins rows using other relational
operators(except = )

Self join -- ( any relational operators) Joins rows of same table


Outer join Left outer join ((+) appended to left operand in Rows common in both tables and uncommon
join condition) rows have null value in left column
Right outer join ((+) appended to right operand Vice versa
in join condition)
SUB QUERIES
- nesting of queries
- a query containing a query in itself
- innermost sub query will be executed first
- the result of the main query depends on the values return by sub query
- sub query should be enclosed in parenthesis
1. Sub query returning only one value
a. relational operator before subquery.
SELECT … WHERE < column name > < relational op.> < subquery>;
2. Sub query returning more than one value
a. ANY
main query displays values that matches with any of the values returned
by
sub query
SELECT .. WHERE < column name > < relational op.> ANY
(<subquery>);
b. ALL
main query displays values that matches with all of the values returned by
sub query
SELECT .. WHERE < column name > < relational op.> ALL (<subquery>);
c. IN
main query displays values that matches with any of the values returned
by
sub query
SELECT … WHERE < column name > IN (<subquery>);
d. NOT IN
main query displays values that does not match with any of the values
returned by sub query
SELECT … WHERE < column name > NOT IN (<subquery>);

e. EXISTS
main query executes only if the sub query returns any few rows
<main query> EXISTS (<sub query>);

f. NOT EXISTS
main query executes only if the sub query does not return any rows
<main query> NOT EXISTS (<sub query>);

g. CONTAINS
A query selects what is selected by its correlated sub query
(<query>) CONTAINS (<query>);

h. EXCEPT
A query selects what is not selected by its correlated sub query
(<query>) EXCEPT (<query>);
i. GROUP BY CLAUSE
used to group same value in a column together and apply a group function to
it
Rule:
Select attributes and group by clause attributes should be same
Select <column1>, <column2> from <table name>
Where <conditions>
GROUP BY <column2>, <column1>;

j. HAVING CLAUSE
used to apply a condition to group by clause
Select <column1>, <column2> from <table name>
Where <conditions>
GROUP BY <column2>, <column1>
HAVING < conditions>;

k. ORDER BY CLAUSE
Used along with where clause to display the specified column in ascending
order or descending order .Default is ascending order
Select <column1>, <column2> from <table name>
Where <conditions>
ORDER BY <columns> DESC / ASC;
Example 1: List the name and salary of employees of the department 20 who
are leading a project that started before December 31, 1990:

select ENAME, SAL from EMP


where EMPNO in
(select PMGR from PROJECT
where PSTART < ’31-DEC-90’)
and DEPTNO =20;

Example 2: List all employees who are working in a department located in


BOSTON:

select * from EMP


where DEPTNO in
(select DEPTNO from DEPT
where LOC = ’BOSTON’);

Example 3: List all those employees who are working in the same department
as their manager

select * from EMP E1


where DEPTNO in
(select DEPTNO from EMP [E]
where [E.]EMPNO = E1.MGR);
Conditions of the form <expression> <comparison operator> [any|all] <subquery> are used
to compare a given <expression> with each value selected by <subquery>.

• For the clause any, the condition evaluates to true if there exists at least on row selected
by the subquery for which the comparison holds. If the subquery yields an empty result
set, the condition is not satisfied.
• For the clause all, in contrast, the condition evaluates to true if for all rows selected by
the subquery the comparison holds. In this case the condition evaluates to true if the
subquery does not yield any row or value.

Example 1: Retrieve all employees who are working in department 10 and who earn at
least as much as any (i.e., at least one) employee working in department 30:

select * from EMP where SAL >= any


(select SAL from EMP where DEPTNO = 30) and DEPTNO = 10;
Example 2: List all employees who are not working in department 30 and who earn more than
all employees working in department 30:

select * from EMP where SAL > all


(select SAL from EMP where DEPTNO = 30) and DEPTNO <> 30;
Note: For all and any, the following equivalences hold:
in  = any | not in  <> all or != all

Often a query result depends on whether certain rows do (not) exist in (other) tables. Such
type of queries is formulated using the exists operator.
Example 1: List all departments that have no employees:

select * from DEPT where not exists


(select * from EMP where DEPTNO = DEPT.DEPTNO);
SQL> select * from emp
2 where exists( select * from dependent where id = eid and id=101);
ID SALARY NAME DOB ADDR
101 6000 George 01-JAN-75 vellore
SQL> select * from emp
2 where not exists( select * from dependent where id = eid );
ID SALARY NAME DOB ADDR
104 6000 George 01-JAN-75 vellore
100 6000 Thomas 01-JAN-75 vellore
SQL> select addr from emp
2 group by addr
3 having count(*) > 2;
ADDR
--------------------
vellore
SQL> select * from emp order by name desc;
ID SALARY NAME DOB ADDR DOJ
104 6000 George 01-JAN-75 vellore
102 6000 Punit 01-JAN-75 vellore
SQL> select * from emp order by name asc;
ID SALARY NAME DOB ADDR DOJ
102 6000 Punit 01-JAN-75 vellore
104 6000 George 01-JAN-75 vellore

You might also like