Professional Documents
Culture Documents
DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:
DCL
Data Control Language (DCL) statements. Some examples:
TCL
Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be
grouped together into logical transactions.
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 .
There are 2 types of aliases
i) Table alias : It is another name used to refer the table within the
query.
example :
SQL> SELECT ENAME , SAL FROM EMP E WHERE E.DEPTNO = 10;
In the above query "E" is an alias for EMP.
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.
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.
ENAME ENAME
---------- ----------
SCOTT JONES
FORD JONES
ALLEN BLAKE
WARD BLAKE
JAMES BLAKE
TURNER BLAKE
MARTIN BLAKE
MILLER CLARK
ADAMS SCOTT
JONES KING
CLARK KING
BLAKE KING
SMITH FORD
Above query uses EMP table to display names of the employees and their managers
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
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.
Query to diplay name of highest salary taker.
SQL> SELECT EMPNO, ENAME FROM EMP A
WHERE 1 > ( SELECT COUNT(*) FROM EMP B
WHERE A.SAL < B.SAL)
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
JOB
---------
PRESIDENT