Académique Documents
Professionnel Documents
Culture Documents
Inner Join; Using Table Aliases; Outer Join; Self Join; Non-Equi Join Subqueries -
Simple Subqueries; Multilevel Subqueries; Correlated Subqueries; Predicates
Set Operators - Set Operator Examples
Aliases:
An alias in oracle is another name used for a column or a table inside SELECT query.
They are accessible only inside the query where they are created. Once the query
execution is over alias will perish. An alias can be a single word or can be more than
one word. If alias used is more than one word it must be enclosed in double quotes
example:
current_date or "current date"
Student_name or "Student name"
Aliases are used in places when ever there is multiple usage of longer table names.
example :
STUDENT_MASTER_RECORD can be aliased as SMR
ACCOUNT_MASTER_TABLE as AMT .
To retrieve desired data from more than one table we use join.
We will have more than one table name after FROM key word in
SELECT statement. A SELECT statement on multiple tables without
a proper JOIN condition will lead to a cartesian product.(i.e.
No.Of Output rows = No.of rows in table1 X No of rows in table2....)
Types Of Joins:
1) EQUI JOIN: The equi join is normally used to join tables with primary key
foreign key relation ships.
Example :
SQL> SELECT ENAME,JOB,DEPTNO FROM EMP;
10 CLARK ACCOUNTING
10 KING ACCOUNTING
10 MILLER ACCOUNTING
20 SMITH RESEARCH
20 ADAMS RESEARCH
20 FORD RESEARCH
20 SCOTT RESEARCH
20 JONES RESEARCH
30 ALLEN SALES
30 BLAKE SALES
30 MARTIN SALES
30 JAMES SALES
30 TURNER SALES
30 WARD SALES
In the above query "E" and "D" are the alias names for the tables EMP
and DEPT respectively. As column DEPTNO is there in both the tables, table
name is used along with column name DEPTNO. This is to avoid the ambiguity
2) NON-EQUI JOIN :
A join condition where any relation operator other than "=" equal to
operator is used.
3) OUTER JOIN :
In EQUI JOIN rows that does not satisfy specified condition would not be displayed.
Example: DEPTNO 40 is not displayed in the previous example
because there are no employees in it. If we want to diplay its detail also
then we have to use OUTER JOIN.Otherwise OUTER JOIN is imilar to EQUI JOIN
except for the difference it uses outer join (+) operator. (A plus within
parenthesis) towards the side not having required data. Outer join operator will
substitute null values when there are no values available.
4) SELF JOIN:
When we join a table to itself it is called self join.To join a
table itself means that each row of the table is combined with itself
and with every other row of the table. The self join can be seen as join
of two copies of the same table.
Above query uses EMP table to display names of the employees and their managers
ENAME NVL(M.ENAME,'HEISMANA
---------- ---------------------
FORD JONES
SCOTT JONES
JAMES BLAKE
TURNER BLAKE
MARTIN BLAKE
WARD BLAKE
ALLEN BLAKE
MILLER CLARK
ADAMS SCOTT
CLARK KING
BLAKE KING
JONES KING
SMITH FORD
KING He is managed by none
SUBQUERY :
A query within another quey. A select statement whose output is substituted in the
condition of another select statement .(A query is a statement written for returning
specific data). The subquery is executed only once. A subquery is enclosed in
parenthesis
The SELECT statement inside parenthesis is called subquery and the one which
uses the values returned by it as main query.
ENAME
----------
SCOTT
FORD
CORRELATED QUERY:
In a correlated subquery the table used in outer query refers to the table used in the
inner query. The correlated subquery is executed repeatedly once
for each row of the main query table.
EMPNO ENAME
-------- ----------
7839 KING
SET OPERATORS
They combine results from two or more queries into one result.Data type
of all selected colums must be of same type.
UNION: It returns rows of first query plus rows of second query but avoids
duplicates.(UNION ALL will give duplicates also).
JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN
JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN
JOB
---------
CLERK
MANAGER
JOB
---------
PRESIDENT