RDBMS Using Oracle Lecture week 5 CASE Expression Group Functions Lecture Overview 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 IF THEN ELSE 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. select SAL, CASE WHEN SAL < 1000 then 'LOW WHEN SAL < 2000 then 'MID WHEN SAL < 3000 then 'HIGH ELSE ' VERY HIGH END from emp SAL CASE --------- ---------- 800 LOW 1600 MID 1250 MID 2975 HIGH 1250 MID 2850 HIGH 2450 HIGH 3000 VERY HIGH 5000 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 from emp; DEPTNO CASEDEPT ---------- -------- 20 ACCOUNTS 30 ADMIN 10 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.
Suppose you want to find the average salary of clerks (table is emp) D I F F E R E N C E SQL> select avg(sal) from emp where job = 'CLERK'; AVG(SAL) ---------- 625 SQL> select avg(sal), avg(distinct sal) from emp; AVG(SAL) AVG(DISTINCT SAL) ---------- ---------------- 2073.21429 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 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) SUM(COMM) ---------- ---------- 2500 120 SUM Function 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 Salary Minimum Salary Difference SQL> select max(sal), min(sal), max(sal) - min(sal) from emp; MAX(SAL) MIN(SAL) MAX(SAL)-MIN(SAL) ---------- ---------- ----------------- 5000 800 4200 Display & Working with DATE values Date values normally follow standard format (DD-MON MON-YY) e.g 12- JAN- 82.
Following query will display Employee s 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 employee s 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 MY_DATE ---------- -------- SMITH 12/17/80 ALLEN 02/20/81 WARD 02/22/81 JONES 04/02/81 MARTIN 09/28/81 BLAKE 05/01/81
SQL> select ENAME, To_CHAR (HIREDATE, 'DD/MM/YYYY') MY_DATE from emp ENAME MY_DATE ---------- ---------- SMITH 17/12/1980 ALLEN 20/02/1981 WARD 22/02/1981 JONES 02/04/1981 MARTIN 28/09/1981 BLAKE 01/05/1981 CLARK 09/06/1981 SQL> select ENAME, To_CHAR (HIREDATE, 'Month DD,YYYY') as MY_DATE from emp; ENAME MY_DATE ---------- ----------------- SMITH December 17,1980 ALLEN February 20,1981 WARD February 22,1981 JONES April 02,1981 MARTIN September 28,1981 BLAKE May 01,1981
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 YEAR --------- ---- 17-DEC-80 1980 20-FEB-81 1981 22-FEB-81 1981 02-APR-81 1981 28-SEP-81 1981 SQL>select ENAME, To_CHAR (HIREDATE, 'Month DD,YYYY HH:MI PM') as MY_DATE from emp ENAME MY_DATE ---------- -------------------------- SMITH December 17,1980 12:00 AM ALLEN February 20,1981 12:00 AM WARD February 22,1981 12:00 AM JONES April 02,1981 12:00 AM
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 DEPTNO DECODE ---------- ---------- ----- SMITH 20 B ALLEN 30 Other WARD 30 Other JONES 20 B MARTIN 30 Other BLAKE 30 Other CLARK 10 A SQL> select ename, deptno from emp; ENAME DEPTNO ---------- ---------- SMITH 20 ALLEN 30 WARD 30 JONES 20 MARTIN 30 BLAKE 30 CLARK 10
SQL> select ename, deptno, decode(deptno, 10, This is 10', 20, 'B', C') from emp; ENAME DEPTNO DECODE ---------- ---------- ----- SMITH 20 B ALLEN 30 C WARD 30 C JONES 20 B MARTIN 30 C BLAKE 30 C CLARK 10 This is 10 RDBMS Inserting, Updating & Deleting rows in a table Commit and Rollback Alter table Structure
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) ;
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 SAL COMM DEPTNO ----- ---------- ------- ---------- --------- ---------- ---------- ---------- 1458 GEO SALESMAN 7698 20-FEB-80 1500 800 10 1459 TANG MANAGER 7698 22-FEB-90 1650 600 30
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 JOB MGR HIREDATE SAL DEPTNO ------ ---------- --------- ---------- --------- ---------- ---------- 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 123 JON CLERK 500 30
SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ------ ---------- --------- ---------- --------- ---------- ---------- 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 123 JON CLERK 500 30 SQL> ROLLBACK; SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ------ ---------- --------- ---------- --------- ---------- ---------- 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 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.
SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ------ ---------- --------- ---------- --------- ---------- ---------- 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 123 JON CLERK 500 30 SQL> ROLLBACK; SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO ------ ---------- --------- ---------- --------- ---------- ---------- 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 123 JON CLERK 500 30 Updating Fields in a table
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.
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
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 );
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 expression;
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; )
DELETE command Try This Delete record from emp table, whose deptno is 10 and hiredate is 09-JUN JUN-81. MODIFYING TABLE
Changing a column s width CREATE TABLE TEST ( ID NUMBER(2), NAME VARCHAR(14) ); SQL> desc test; Name Null? Type ------------------------------- -------- ---- ID NUMBER(2) NAME VARCHAR2(14) Changing a column s width To allow ID column of TEST table to accept numbers with upto nine digits SQL> ALTER TABLE TEST MODIFY (ID NUMBER (9) );
Changing a column s 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 column s 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 );
Changing Data-type 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 type etc We can always increase a column width or change its number of decimal places. We can decrease a column s 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) ;
Rules about changing a column width/data-type 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
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)
Thanks