Lab 5
Lab 5
Lab 5
DEPT_ID
10
15
20
24
25
30
UNION ALL: returns all rows returned by either query including duplicates.
Example:-select dept_id from EMP UNION ALL select dept_id from dept;
1 Assisted By FIKRU T.
Using the Set Operators
DEPT_ID
10
20
15
15
30
10
20
25
30
24
INTERSECT: returns all distinct rows or columns rows returned by both queries.
Note: A row must exist in both query outputs to be returned in the final result.
DEPT_ID
10
20
30
MINUS: produce rows returned by the first query but not by the second.
Example1: select dept_id from EMP MINUS select dept_id from dept;
2 Assisted By FIKRU T.
Using the Set Operators
DEPT_ID
15
Example2: select dept_id from dept MINUS select dept_id from EMP;
DEPT_ID
24
25
Using the UNION operator, display the location ID, department name, and the state
where it is located.
You must match the data type (using the TO_CHAR function or any other conversion
functions) when columns do not exist in one or the other table.
UNION
SELECT dept_id,null,dept_name,0,null,location_id
FROM dept;
3 Assisted By FIKRU T.
Using the Set Operators
The ORDER BY clause can appear only once at the end of the compound query.
Component queries cannot have individual ORDER BY clauses.
ORDER BY clause recognizes only the columns of the first SELECT query.
By default, the first column of the first SELECT query is used to sort the output in an
ascending order.
UNION
OPERATORS IN ORACLE
ADDITION
2950
800
5500
2000
1600
SUBSTRACTI
ON
1950
-200
4500
1000
600
PRODUCT
1225000
150000
2500000
750000
550000
Division: returns the quotient of the specified rows from the specified columns.
5 Assisted By FIKRU T.
Using the Set Operators
QOUTI ENT
4.9
.6
10
3
2.2
6 Assisted By FIKRU T.
Using the Set Operators
LIKE Operator
7 Assisted By FIKRU T.
Using the Set Operators
Like operator is a pattern matching operator. ‘%’ is used to denote any number of unknown
characters.
Example: select empname,empno,job, hiredate,salary from emp where empname LIKE '%K';
Example: select empname, empno, job, hiredate, commission from EMP where empname
LIKE 'K%'
To retrieve rows where the empname starts with ’S’ and ends with ‘T’.
Logical Operators
AND: ensures that rows satisfying both the conditions are selected.
8 Assisted By FIKRU T.
Using the Set Operators
OR: ensures that a row satisfying any one of the conditions is retrieved.
NOT: ensures that a row not satisfying the condition specified in the query is retrieved.
IN: one of a set of values tells the DBMS, that one of the values must be in the
specified value list.
9 Assisted By FIKRU T.
Using the Set Operators
NOT IN: values must not be in the specified values in the specified list.
Example: select * from EMP where salary BETWEEN 1500 AND 2450;
Example: select * from EMP where salary NOT BETWEEN 1500 AND 2450;
10 Assisted By FIKRU T.
Using the Set Operators
The operator concatenates the second string to the ends of the first.
CONCATINATION
CLARK (MANAGER )
SCOTT (ANALYST )
KING (PRESIDENT )
TURNER (SALESMAN )
ADAMS (CLERK )
11 Assisted By FIKRU T.