Oracle 11 G (SQL) : Quality Thought Technologies
Oracle 11 G (SQL) : Quality Thought Technologies
(SQL)
1. Database Introduction
- Data
- Database
- Metadata
- Database Management System
2. Data types in Oracle
3. SQL statements
DDL, DML, DCL, TCL, Data retrieval
4. Create table statement
5. Populating data into tables
- Insert statement
6. Data retrieval using SELECT statement
7. Arithmetic operators, operator precedence
- Addition (+)
- subtraction (-)
- multiply (*)
- divide (/)
8. Handling null values
9. Working with aliases
- column aliases
- table aliases
10. Concatenation operator (||)
11. Suppressing duplicate rows
- distinct keyword
12. Filtering of records
- Where clause
13. Logical operators
- AND, OR, NOT
14. Comparison operators
=, <>/!=/^=, <, >, <=, >=
15. SQL * Plus operators
- BETWEEN … AND …
- IN
- IS NULL
- LIKE
16. Ordering information
ORDER BY
17. SQL functions
18. Working with dates
Data
Table name,
Table owner,
details about the columns (Values allowed, length
or size) and the
physical storage size on disk.
Data Base
If the data is less than the original specified size, blank
pads are applied.
Modify column: Alter table <table_name> We can decrease the width
Modify ( column datatype) of a column if contains only
Null values and if the table has
no data
We can chage the datatype if
the column contains NULL’s
Drop:
Any views and synonyms will remain nut are kept in invalid state.
Syntax:
Rename <oldname> to <newname>
Truncation a table:
It is used to remove all rows from a table and to release the storage space
Syntax:
Syntax:
Note: all data types may not have the width property.
no two columns in the same table can have the same
name
Filter data:
2. IN operator:
3. Is null operator:
= equality operator
<>,!=, ^= not equality operator
> Greater than operator
< less than operator
<= , > = less than or equal to operator , greater than or equal to
Applying logical operators to filters:
And:
It returns true if both or all component conditions are TRUE.
It returns false if either is False, else return unknown.
Or:
It returns TRUE if either of the component condition is TRUE.
Not:
It returns true if the following condition is False.
It returns False if the following condition is TRUE.
ALTER command:
ADD
MODIFY
DROP
ALTER TABLE <table_name> ADD (Columnname Datatype,--,--)
SYNTAX:
Drop table table_name
CASCADE CONSTRAINTS
Syntax:
Rename <oldtable_Name> to
<New_Table_name> ;
Commands :-
INSERT : to add records into the table
UPDATE : to change column value in the table
DELETE : to remove rows from the table
Syntax:
UPDATE <table_name> SET <specification>
WHERE clause.
SQRT(4)
----------
2
SQRT(2)
----------
1.41421356
ROUND(19)
----------
19
SQL> SELECT ROUND(19.4) FROM DUAL;
ROUND(19.4)
-----------
19
SQL> SELECT ROUND(19.5) FROM DUAL;
ROUND(19.5)
-----------
20
ROUND(19.254,2)
---------------
19.25
ROUND(19.255,2)
---------------
19.26
ROUND(19.256,2)
---------------
19.26
TRUNC
TRUNC(19)
----------
19
TRUNC(19.4)
-----------
19
TRUNC(19.5)
-----------
19
Conti…
TRUNC
Conti…
TRUNC(19.124,2)
---------------
19.12
TRUNC(19.125,2)
---------------
19.12
TRUNC(19.126,2)
---------------
19.12
Character Functions
1. UPPER ( )
2. LOWER ( )
3. INITCAP ( )
4. LENGTH ( )
5. REVERSE ( )
6. CONCAT ( )
7. LPAD( )
8. RPAD( )
9. TRIM( )
10. LTRIM( )
11. RTRIM ( )
12. ASCII( )
13. CHR ( )
14. VSIZE ( )
15. SUBSTR( )
16. INSTR ( )
ENAME REVERSE(EN
---------- ----------
SMITH HTIMS
ALLEN NELLA
WARD DRAW
JONES SENOJ
MARTIN NITRAM
BLAKE EKALB
CLARK KRALC
SCOTT TTOCS
KING GNIK
TURNER RENRUT
ADAMS SMADA
JAMES SEMAJ
FORD DROF
MILLER RELLIM
ENAME TRIM(ENAME
---------- ----------
SMITH SMITH
ALLEN ALLEN
WARD WARD
JONES JONES
MARTIN MARTIN
BLAKE BLAKE
CLARK CLARK
SCOTT SCOTT
KING KING
TURNER TURNER
ADAMS ADAMS
JAMES JAMES
FORD FORD
MILLER MILLER
ASCII('A')
----------
97
SQL> select ASCII('A') from dual;
ASCII('A')
----------
65
SQL> select CHR(97) from dual;
C
-
a
C
-
A
SQL> desc emp;
Name Null? Type
------------------- -------- -------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SUB
---
Raj
SUBS
----
Jesh
SUBSTR
------
Rajesh
ENAME SUBS
---------- ----
SMITH SMIT
ALLEN ALLE
WARD WARD
JONES JONE
MARTIN MART
BLAKE BLAK
CLARK CLAR
SCOTT SCOT
KING KING
TURNER TURN
ADAMS ADAM
JAMES JAME
FORD FORD
MILLER MILL
SQL> select instr('Database','a',1,1) from dual;
INSTR('DATABASE','A',1,1)
-------------------------
2
INSTR('DATABASE','A',1,1)
-------------------------
0
INSTR('DATABASE','A',1,2)
-------------------------
4
SQL> select instr('Database','a',1) from dual;
INSTR('DATABASE','A',1)
-----------------------
2
SQL> select instr('Database','a',2) from dual;
INSTR('DATABASE','A',2)
-----------------------
2
SQL> select instr('Database','a',3,1) from dual;
INSTR('DATABASE','A',3,1)
-------------------------
4
SQL> select instr('Database','a',3,2) from dual;
INSTR('DATABASE','A',3,2)
-------------------------
6
Date Functions
Function Description
2) Months_between( D1 , D2)
3) Add_Months( D ,+/- N )
3) Last_day (D)
4) Next_day ( D , ‘Day’)
SYSDATE
---------
14-JUL-07
ADD_MONTH
SYSDATE
---------
---------
14-SEP-07
14-JUL-07
SQL> SELECT ADD_MONTHS(SYSDATE,-2)
FROM DUAL;
SQL> SELECT SYSDATE + 10 FROM
DUAL; ADD_MONTH
---------
SYSDATE+1 14-MAY-07
--------- SQL> SELECT ADD_MONTHS(SYSDATE,1.9)
FROM DUAL;
24-JUL-07
ADD_MONTH
SQL> SELECT SYSDATE - 10 FROM
---------
DUAL;
14-AUG-07
SQL> SELECT ADD_MONTHS(SYSDATE,0)
SYSDATE-1 FROM DUAL;
--------- ADD_MONTH
04-JUL-07 ---------
LAST_DAY
LAST_DAY(
---------
31-JUL-07
LAST_DAY(
---------
31-AUG-07
SYSDATE
---------
14-JUL-07
TO_CHAR(S
---------
SATURDAY
NEXT_DAY(
---------
16-JUL-07
Quality Thought Technologies
NEXT_DAY
NEXT_DAY(
---------
16-JUL-07
NEXT_DAY( 1--SUNDAY
--------- 2-MONDAY
15-JUL-07
1. MIN ( )
2. MAX ( )
3. SUM ( )
4. AVG ( )
5. COUNT ( * / Col )
MIN(SAL)
----------
800
MAX(SAL)
----------
5000
SUM(SAL)
----------
29025
SUM(SAL)
----------
8750
Aggregate Functions
SQL> Select Avg(Sal) from emp;
AVG(SAL)
----------
2073.21429
AVG(SAL)
----------
2916.66667
COUNT(*)
----------
14
COUNT(COMM)
---------------------
4
Aggregate Functions
DNO SUM(SAL)
---------- -------------------- ----------
10 8750
20 10875
30 9400
SQL> SELECT DEPTNO , JOB , SUM (SAL) FROM EMP GROUP BY DEPTNO;
SELECT DEPTNO , JOB , SUM (SAL) FROM EMP GROUP BY DEPTNO
*
ERROR at line 1:
ORA-00979: not a GROUP BY expression
DNO SUM(SAL)
---------- -------------------- ----------
10 8750
20 10875
30 9400
DEPTNO SUM(SAL)
---------- ----------
20 10875
10 8750
DEPTNO SUM(SAL)
---------- ----------
30 9400
10 8750
Aggregate Functions
SQL> select DEPTNO , SUM(SAL) FROM EMP
GROUP BY DEPTNO
HAVING SUM(SAL) < 10000 ORDER BY DEPTNO;
DEPTNO SUM(SAL)
---------- ----------
10 8750
30 9400
-----------------------------------------------------------------------------------------------------------------------------------------------------
DEPTNO SUM(SAL)
---------- ----------
10 8750
30 9400
DEPTNO COUNT(*)
------------------- ---------------
10 3
20 5
30 6
DATE
23-DEC-2025
DATE
23-DEC-2050
DATE
23-DEC-2077
TO_
---
Jul
TO_CHAR(S
---------
JULY
SQL>SELECT HIREDATE,
TO_CHAR(HIREDATE,'DDD,DD/MM/YYYY HH:MI:SS') FROM EMP;
TO_CHAR(SYSD
------------
04 : 13 : 53
TO_CHAR(SYSD
------------
04 : 14 : 08
TO_CHAR(SYSD
------------
16 : 14 : 19
TO_CHAR(SYSDATE,
----------------
04 : 15 : 06 PM
TO_CHAR(SYSDATE,
----------------
16 : 15 : 16 PM
To_Date(String, Str_Format)
14 rows selected.
no rows selected
UNION
UNION ALL
INTERSEC
MINUS
UNION ALL
Combines the results of two select statements into one result set
including the duplicates.
MINUS
Takes the result set of one SELECT statement, and removes
those rows that are also returned by a second SELECT statement.
INTERSECT
Returns only those rows that are returned by each of two SELECT
statements.
The number of columns and there Data type should be match in all the
Queries.
8 rows selected.
UNION
SQL> select * from dept
2 UNION
3 select * from dept;
11 rows selected.
-----------------------------------------------------------------------------------------
INTERSECT
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ERROR – Number of Columns
D DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
JOINS:
JOINs are used to retrieve information from multiple tables.
TYPES OF JOINS:
1. EQUI-JOIN :
2. NON-EQUI JOIN :
3. OUTER JOIN
* LEFT OUTER JOIN
* RIGHT OUTER JOIN
* FULL OUTER JOIN
4. SELF JOIN
EMP_NAME MGR_NAME
---------- ----------
SMITH FORD
ALLEN BLAKE
WARD BLAKE
JONES KING
MARTIN BLAKE
BLAKE KING
CLARK KING
SQL> select * from emp where sal = (select Min(sal) from emp);
ENO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
(select Max(sal) from emp);
SQL> select * from emp where sal =
ENO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10
SQL> select * from emp where sal <= (select Avg(sal) from emp);
ENO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- -------------------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
Multi Row Sub Query
SQL> select * from emp where deptno in ( select deptno from dept);
14 rows selected.
SQL> select * from dept where deptno in ( select Distinct(deptno) from emp );
Types Of INDEX
1) UNIQUE INDEX
2) BEE TREE INDEX
3) BIT MAP INDEX
4) FUNCTIONED INDEX
5) REVERSE INDEX
VIEW
COLUMNS OF A TABLE.
SELECT
E.Ename Employee,
E.Sal "Employee's Salary",
SE.Grade EmpGrade,
M.Sal "Manager's Salary",
SM.Grade MGRGrade,
Dname
FROM Emp E JOIN Dept D
ON E.Deptno = D.Deptno JOIN Emp M ON
E.MGR = M.Empno JOIN SalGrade SE ON
E.Sal BETWEEN SE.LoSal AND SE.HiSal JOIN SalGrade SM ON
M.Sal BETWEEN SM.LoSal AND SM.HiSal
CREATE VIEW EMP_MGR_SALGRADE_INFO
AS
SELECT
E.Ename Employee,
E.Sal "Employee's Salary",
SE.Grade EmpGrade,
M.Sal "Manager's Salary",
SM.Grade MGRGrade,
Dname
FROM Emp E JOIN Dept D
ON E.Deptno = D.Deptno JOIN Emp M ON
E.MGR = M.Empno JOIN SalGrade SE ON
E.Sal BETWEEN SE.LoSal AND SE.HiSal JOIN SalGrade SM ON
M.Sal BETWEEN SM.LoSal AND SM.HiSal
TYPES OF VIEWS:
SIMPLE VIEWS:
COMPLEX VIEW:
Materialized Views ;-
Syntax:
********
FunctionName(column/expression, [arg1, arg2, ..])