You are on page 1of 31

RDBMS Using Oracle

Lecture week 5

Lecture Overview

CASE Expression
Group Functions

AVG
MAX
MIN
SUM
COUNT
Etc

Working with Date


Decode Function
INSERT, UPDATE and DELETE commands
Commit and Rollback, Alter Table Command

CASE Expression

CASE Expression is new to Oracle9i and can be


used to drive IFTHENELSE logic in SQL.

Syntax
CASE Expression
WHEN <compare value> THEN <return value>..
[ELSE <return value>..]
END

The CASE expression begins with CASE keyword and


end with keyword END.

The ELSE clause is optional, the WHEN clause can be


repeated 128 times.

SAL
--------800
1600
1250
2975
1250
2850
2450
3000
5000

select SAL, CASE WHEN SAL < 1000 then 'LOW


WHEN SAL < 2000 then 'MID
CASE
WHEN SAL < 3000 then 'HIGH
---------ELSE ' VERY HIGH
LOW
MID
END
MID
from emp
HIGH
MID
HIGH
HIGH
VERY HIGH
VERY HIGH

You can use any of the following

= != <> < <= > >= IN

Practice

Show names and marks by writing a SQL query


on bit4_results and show LOW for marks less
then 5, MID for marks less then 7 but more
then 4.99, and HIGH for all above and equal
to 7.

NOTE:-

bit4_results is stored in my login


i.e. kamran and you all have select access on it.

CASE Expression
Another way to apply CASE in SQL
Select deptno, CASE deptno
WHEN 10 THEN SALES
WHEN 20 THEN ACCOUNTS
WHEN 30 THEN ADMIN
ELSE OTHER
END
DEPTNO
CASEDEPT
from emp;
----------------20
30
10

ACCOUNTS
ADMIN
SALES

<CASE> Other Example


select SAL, CASE WHEN SAL in (100, 200, 300, 400, 500) then 'LOW'
WHEN SAL =< 2000 then 'MID'
WHEN SAL =< 3000 then 'HIGH'
ELSE ' VERY HIGH'
END
from emp;

You can use any of the following

= != <> < <= > >= IN

Group Functions

Group Functions

There are many Group Functions available in


SQL. By using these functions we can
Find Average
Count number of records
Find maximum value stored in any column
Find minimum value stored in any column
Sum of values stored in columns
etc

Average Function

Suppose you want to find the average salary of


all employees (table is emp)
SQL> select avg(SAL) from emp;
SQL> select sal from emp;

SQL> select avg(sal) from emp;


AVG(SAL)
---------583.333333

SAL
---------800
900
800
600
200
200
6 rows selected.

D
I
F
F
E
R
E
N
C
E

Suppose you want to find the average salary of


clerks (table is emp)
SQL> select avg(sal) from emp where job = 'CLERK';
AVG(SAL)
---------625

SQL> select avg(sal), avg(distinct sal) from emp;


AVG(SAL)
---------2073.21429

AVG(DISTINCT SAL)
---------------2064.58333

SUM Function

By using SUM function we can find total of any


numeric column.

SUM Function
To find total of SAL column of emp table
SQL> select sum(sal) from emp;
SUM(SAL)
---------3500
SUM can different according to available SALARY column

SUM Function

To find total salary and total commission of


employees who are CLERKS

SQL> select sum(sal), sum (comm) from emp


where job = 'CLERK';
SUM(SAL)
---------2500

SUM(COMM)
---------120

SQL> select sal, comm from emp


where job = 'CLERK';
SAL COMM
---------- -------------------800 20
900 20
600 70
200 10

Finding Highest Value

To find highest salary MAX function is used

For example:
SQL> select max(sal) from emp;
MAX(SAL)
---------900

SQL> select sal from emp;


SAL
---------800
900
800
600
200
200

Finding lowest Value

To find lowest salary MIN function is used

Example:

SQL> select min(sal) from emp;


MIN(SAL)
---------200

SQL> select sal from emp;


SAL
---------800
900
800
600
200
200

Write a query to find the highest and lowest


salaries, and the difference between them?

Maximum Minimum
Salary
Salary

Difference

SQL> select max(sal), min(sal) , max(sal) - min(sal) from


emp;
MAX(SAL) MIN(SAL)
------------------5000
800

MAX(SAL)-MIN(SAL)
----------------4200

Display & Working


with DATE values
Date values normally follow standard format
(DD-MON-YY) e.g 12- JAN- 82.

Following query will display Employees


HIREDATE in standard format.
SQL> select ENAME, HIREDATE from emp;
ENAME
HIREDATE
---------- --------SMITH
17-DEC-80
ALLEN
20-FEB-81
WARD
22-FEB-81
JONES
02-APR-81
MARTIN 28-SEP-81
BLAKE 01-MAY-81
CLARK
09-JUN-81

We can specify any


different format by
using TO_CHAR()
function.

To display employees hire dates in a format like


01/15/83, enter:

SQL> select ENAME, To_CHAR (HIREDATE, 'MM/DD/YY')


as MY_DATE from emp
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE

MY_DATE
-------12/17/80
02/20/81
02/22/81
04/02/81
09/28/81
05/01/81

10

SQL> select ENAME,

To_CHAR (HIREDATE, 'DD/MM/YYYY')


MY_DATE from emp
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK

MY_DATE
---------17/12/1980
20/02/1981
22/02/1981
02/04/1981
28/09/1981
01/05/1981
09/06/1981

SQL> select ENAME,


To_CHAR (HIREDATE, 'Month DD,YYYY')
as MY_DATE from emp;
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE

MY_DATE
----------------December 17,1980
February 20,1981
February 22,1981
April 02,1981
September 28,1981
May
01,1981

11

We can also extract YEAR from any date


value by using TO_CHAR function
SQL> select hiredate, to_char (hiredate, 'YYYY')
as year emp;
HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81
28-SEP-81

YEAR
---1980
1981
1981
1981
1981

SQL>select ENAME,
To_CHAR (HIREDATE, 'Month DD,YYYY HH:MI PM') as

MY_DATE from emp


ENAME
---------SMITH
ALLEN
WARD
JONES

MY_DATE
-------------------------December 17,1980 12:00 AM
February 20,1981 12:00 AM
February 22,1981 12:00 AM
April 02,1981 12:00 AM

12

DECODE Function

DECODE Function works in a way as a CASE


statement or as a SWITCH statement in C/C++
programming language.

Suppose we want to
display character A instead of deptno 10 in emp
table
and we want to display B instead of deptno 20
and for all other deptno we want to display character
C or word Other.

SQL>

select ename, deptno ,


decode(deptno , 10, 'A' , 20 , 'B' , 'Other')
from emp;

ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK

DEPTNO
---------20
30
30
20
30
30
10

DECODE
----B
Other
Other
B
Other
Other
A

SQL> select ename, deptno


from emp;
ENAME
DEPTNO
---------- ---------SMITH
20
ALLEN
30
WARD
30
JONES
20
MARTIN
30
BLAKE
30
CLARK
10

13

SQL>

select ename, deptno ,


decode(deptno , 10, This is 10' , 20 , 'B' , C')
from emp;
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK

DEPTNO
---------20
30
30
20
30
30
10

DECODE
----B
C
C
B
C
C
This is 10

RDBMS
Inserting, Updating & Deleting rows in a table
Commit and Rollback
Alter table Structure

14

Inserting a row into


a table

The INSERT Command

The insert command inserts one or more rows


in a table. Its format is

INSERT INTO table-name


VALUES
(a list of data values) ;

15

EMP TABLE
Name
Type
------------------------------- -------- ---EMPNO
NUMBER(4)
ENAME
VARCHAR2(10)
JOB
VARCHAR2(9)
MGR
NUMBER(4)
HIREDATE
DATE
SAL
NUMBER(7,2)
COMM
NUMBER(7,2)
DEPTNO
NUMBER(2)

INSERT a row
in EMP table

INSERT INTO EMP


Values (4567, Ali , MANAGER , 7963,
7-APR-80, 1000, 500, 30);
If column names are not mentioned with table name then value
are required to be provide in the same sequence as defined in
table.
SQL> Select * from emp where ename = Ali;

Insert following records in EMP table


EMPNO ENAME JOB
MGR HIREDATE
-------------- ------- ---------- --------1458
GEO
SALESMAN 7698
20-FEB-80
1459
TANG
MANAGER 7698
22-FEB-90

SAL
COMM DEPTNO
---------- ---------- ---------1500
800
10
1650
600
30

16

INSERTING few columns


INSERT INTO EMP (EMPNO, ENAME, JOB, EPTNO, SAL)

VALUES (1234, LEO , MANAGER , 10 , 700);

NULL values will be entered automatically


in remaining columns.

COMMIT & ROLLBACK


INSERT INTO EMP (EMPNO, ENAME, JOB,
DEPTNO, SAL)
VALUES (123, JON , CLERK , 30 , 500);
1 row created.
SQL> select * from emp;
EMPNO ENAME
--------------7900
JAMES
7902
FORD
7934
MILLER
123
JON

JOB
--------CLERK
ANALYST
CLERK
CLERK

MGR
---------7698
7566
7782

HIREDATE SAL
-----------------03-DEC-81
950
03-DEC-81
3000
23-JAN-82
1300
500

DEPTNO
---------30
20
10
30

17

SQL> select * from emp;


EMPNO ENAME
--------------7900
JAMES
7902
FORD
7934
MILLER
123
JON

JOB
--------CLERK
ANALYST
CLERK
CLERK

MGR
---------7698
7566
7782

HIREDATE SAL
-----------------03-DEC-81
950
03-DEC-81
3000
23-JAN-82
1300
500

DEPTNO
---------30
20
10
30

HIREDATE SAL
-----------------03-DEC-81
950
03-DEC-81
3000
23-JAN-82
1300

DEPTNO
---------30
20
10

SQL> ROLLBACK;
SQL> select * from emp;
EMPNO ENAME
JOB
----------------------7900
JAMES
CLERK
7902
FORD
ANALYST
7934
MILLER CLERK

MGR
---------7698
7566
7782

SQL> INSERT INTO EMP(EMPNO, ENAME,JOB, DEPTNO,


SAL)
VALUES (123, 'JON' , 'CLERK' , 30 , 500)
1 row created.
SQL> commit;
Commit complete.

After entering COMMIT, changes will be


saved permanently.

18

SQL> select * from emp;


EMPNO ENAME
--------------7900
JAMES
7902
FORD
7934
MILLER
123
JON

JOB
--------CLERK
ANALYST
CLERK
CLERK

MGR
---------7698
7566
7782

HIREDATE SAL
-----------------03-DEC-81
950
03-DEC-81
3000
23-JAN-82
1300
500

DEPTNO
---------30
20
10
30

HIREDATE SAL
-----------------03-DEC-81
950
03-DEC-81
3000
23-JAN-82
1300
500

DEPTNO
---------30
20
10
30

SQL> ROLLBACK;
SQL> select * from emp;
EMPNO ENAME
--------------7900
JAMES
7902
FORD
7934
MILLER
123
JON

JOB
--------CLERK
ANALYST
CLERK
CLERK

MGR
---------7698
7566
7782

Updating Fields
in a table

19

UPDATE command

The UPDATE command consists of an


UPDATE clause followed by a SET clause and
an optional WHERE clause.

<< Syntax >>

UPDATE table-name
SET
field 1 = value , field 2 = value , ..
WHERE logical expression ;

UPDATE command

UPDATE emp
SET
sal = 1200
WHERE ename = SMITH ;
This update command will update salary of
SMITH in emp table.

20

UPDATE command

UPDATE emp
SET
sal = 1500 , comm = comm * 2
WHERE job = MANAGER ;

This update command will update salary and


comm of all managers.

What this update command will do ???


UPDATE emp
SET sal = 1500;

It will update the salary column of all rows to 1500


(Table is EMP).
// So try to avoid writing these kind of SQL commands

21

Give a 15% salary increment to all managers of


emp table.

UPDATE emp
SET sal = sal * 1.15
Where job = MANAGER ;

Try This : Give a 15% salary increment to all CLERKS and


ANALYSTS of emp table.
UPDATE emp
SET sal = sal * 1.15
Where (job = ANALYST or job = CLERK);

22

Deleting Records
From a Table

DELETE command

DELETE command contains a FROM clause


followed by an optional WHERE clause:

DELETE from table-name


WHERE logical-expression;

23

DELETE command

Suppose we want to delete the record of SMITH


from emp table, for that we will write following
SQL DELETE command.
DELETE from EMP
Where ename = SMITH ;

SQL> DELETE from EMP;

This statement will remove all records from


emp table.

STOP!
Enter ROLLBACK to undo if you have deleted
all records from a table. ( SQL> rollback; )

24

DELETE command

Try This
Delete record from emp table, whose deptno is
10 and hiredate is 09-JUN-81 .

MODIFYING TABLE

25

Changing a columns width

CREATE TABLE TEST


( ID
NUMBER(2) ,
NAME
VARCHAR(14) );
SQL> desc test;
Name
Null? Type
------------------------------- -------- ---ID
NUMBER(2)
NAME
VARCHAR2(14)

Changing a columns width

To allow ID column of TEST table to accept


numbers with upto nine digits

SQL> ALTER TABLE TEST


MODIFY (ID
NUMBER (9) );

26

Changing a columns width


SQL> ALTER TABLE TEST
MODIFY (ID
NUMBER (9) ) ;
Table altered.
SQL> desc test;
Name
Null? Type
------------------------------- -------- ---ID
NUMBER(9)
NAME
VARCHAR2(14)

Changing a columns width

To allow ID column of TEST table to accept


numbers with 2 decimal places.
SQL> ALTER TABLE TEST
MODIFY (ID
NUMBER (9, 2) );
SQL>Insert

into test values (1234567.25, HELLO);

27

Changing Data-type
SQL> ALTER TABLE TEST
MODIFY (ID
varchar (6) );
Table altered.
SQL> desc test;
Name
Null? Type
------------------------------- -------- ---ID
VARCHAR (6)
NAME
VARCHAR (14)

Rules about changing


a column width/data-type etc

We can always increase a column width or change its


number of decimal places.

We can decrease a columns width or change its data


type only if the column is empty.

You can change a column from NOT NULL to NULL


by adding the NULL word to the end of column
specification.
. MODIFY ( ID NUMBER(9) NULL) ;

28

Rules about changing


a column width/data-type etc

You can change a column from NULL to NOT


NULL only if there are no null values in the
column.

Adding a Column

29

Adding a Column

To add a new column into any existing table we


will follow following procedure.

Suppose we want to add address column in


TEST table (See TEST table on previous slides).
SQL> desc test;
Name
Null? Type
------------------------------- -------- ---ID
NUMBER (9)
NAME
VARCHAR (14)

Adding a Column
SQL> ALTER TABLE TEST
ADD (address varchar(20) );
Table altered.
SQL> desc test;
Name
Null? Type
------------------------------- -------- ---ID
NUMBER (9)
NAME
VARCHAR(14)
ADDRESS
VARCHAR(20)

30

Thanks

31

You might also like