RDBMS Using Oracle. Lecture week 5. Lecture Overview

Similar documents
CH-15 SIMPLE QUERY AND GROUPING

ABSTRACT INTRODUCTION IMPORTANT CONCEPTS. John Jay King, King Training Resources

Spool Generated For Class of Oracle By Satish K Yellanki

Relational Normalization Theory. Dr. Philip Cannata

5/10/2018. Connor McDonald

Semester 1 Session 3. Database design

PATANJALI RISHIKUL, PRAYAGRAJ

SQL and PL/SQL. Connor McDonald 7/17/2018. The meanest, fastest thing out there. Connor McDonald

nuijten.blogspot.com

Analytic Functions 101

SQL. Connor McDonald 8/13/2018. The meanest, fastest thing out there. Connor McDonald

a allapex SQL Model Clause: a Gentle introduction That s a lie! Alex Nuijten

SQL and PL/SQL. Connor McDonald 4/24/2018. The meanest, fastest thing out there. Connor McDonald

allapex SQL Model Clause: Gentle introduction That s a lie! Alex Nuijten a allapex nuijten.blogspot.com

Spool Generated For Class of Oracle By Satish K Yellanki

CS Reading Packet: "Simple Reports - Part 2"

Oracle MOOC: SQL Fundamentals

a allapex Regular Expressions Alex Nuijten

Anthony Carrick INFS7903 Assignment

2/5/2019. Connor McDonald. Copyright 2018, Oracle and/or its affiliates. All rights reserved.

Column Functions and Grouping

Oracle MOOC: SQL Fundamentals

STEP BY STEP GUIDE FOR SIF (SALARY INFORMATION FILE) CREATION

Grupne funkcije. Grupne funkcije rade nad skupinom redova i vraćaju jedan red za svaku grupisanu grupu

Archive Log Mining. Supplemental logging must be enabled on the source database before generating redo log files that will be analyzed by LogMiner.

Introduction to Relational Databases Part 2: How?

Define Metrics in Reports

Level 3 Develop software using SQL (Structured Query Language) (7266/ ) ( )

PS9018 Position Table

TM1 Salary Module. Data Entry and Report

CUPA Procedures. Date September 29, Theresa Porter, Information Technology Consultant, BOR. Doug Corwin, Information Technology Consultant, BOR

HOLIDAYS HOMEWORK CLASS XI Commerce Stream ENGLISH

Saint Louis University. Business and Finance

Using Oracle VPD In The Real World

LECTURE9-PART2: DATA MANIPULATION IN SQL, ADVANCED SQL QUERIES AND VIEW

tackling time troubles ORA-01883: overlap was disabled during a region transition

Lecture9: Data Manipulation in SQL, Advanced SQL queries

IF Function Contin.. Question 1

Metadata Matters. Thomas Kyte

SQL Programming 1 SQL1 1

barcode_label version BoostMyShop

SQL> exec sample_font

Adaptive Cursor Sharing Short answer to sharing cursors and optimizing SQL

CP 94bis Statement of amounts due with weight brackets

Lab 20: Excel 3 Advanced

Database Systems CSE 414

University Finance IBM Cognos 10 Planning Financial Performance Management Solution

UMBC CMSC 461 Database Management Systems. Course Project

Analytic functions allow the rows in a result set to 'peek' at each other, avoiding the need for joining duplicated data sources.

=LEFT(B1,SEARCH("-",B1)-1)

Autologue User s Manual Month End Processing. Table Of Contents

RIT. Oracle Manager Self-Service Manual Revised 05/10/17. Contents

TL 9000 Quality Management System. Measurements Handbook. BRR Examples

Employee and Manager Self Service. UHR Employee Development

ExpressMaintenance Release Notes

B u s and Bus4590

Live Chat at BGE Enabling real-time 2-way customer communication. Gabriel Nuñez Senior echannel Program Manager Customer Projects & System Support

The Expenses Supervisor page is available to any user with a FASIS Administration login.

COURSE LISTING. Courses Listed. with Business Intelligence (BI) Crystal Reports. 26 December 2017 (18:02 GMT)

Job Assignment Default Labor Change Process Guidelines For Originators

Department of Transportation Rapid City Region Office 2300 Eglin Street P.O. Box 1970 Rapid City, SD Phone: 605/ FAX: 605/

ETH Zurich Spring Semester Systems Group Lecturer(s): Gustavo Alonso, Ce Zhang Date: March 27/March 31, Exercise 5: SQL II

CSE 344 APRIL 2 ND GROUPING/AGGREGATION

Incrementally Loading Exalytics using Notepad

Lo siento, no hablo español :-(

July Copyright 2018 NetSuite Inc.

SQL> exec sample_font

Oracle BI 12c: Create Analyses and Dashboards Ed 1

Manager Self-Service Training Guide Created on 5/3/ :14:00 AM

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13

Local 2110 Instructions for Web Time Entry

Time Clock Time Clock

Invoice API. ICS Integrated Customer Services. Software specification. Final updated for Release 3. Accident Compensation Commission

GUIDED PRACTICE: SPREADSHEET FORMATTING

Relational Database design. Slides By: Shree Jaswal

Nested SQL Dr. Zhen Jiang

May 21, 2014 Walter E. Washington Convention Center Washington, DC USA. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Oracle BI 12c: Create Analyses and Dashboards Ed 1

Finch Drinking Water System O. Reg 170/03 Schedule 22 - Summary Report for Municipalities

Level 3 Develop software using SQL (Structured Query Language) ( )

Level 3 Develop software using SQL (Structured Query Language) ( / )

The Human Resources Information System DATA WAREHOUSE

Automate! Lessons Learned to Improve Invoice Match to PO Process

Training Guide: Manage Costing for a Person

Performance tuning using SQL new features. By Riyaj Shamsudeen

HR 8.9 Training DISTRIBUTIONS & REDISTRIBUTIONS P A R T I C I P A N T G U I D E

Human Capital Management: Step-by-Step Guide

CONDITIONAL LOGIC IN THE DATA STEP

Oracle Planning and Budgeting Cloud

Oracle Database 12c Release 2 and 18c New Indexing Features

Workforce Manager Time Sheet Approval Manual

AFFORDABLE CARE ACT SOLUTIONS. powered by

Labor Manpower Requirement System In Hierarchical Tables. Deborah M. White. Northrop Corporation ventura Division

Subqueries. Lecture 9. Robb T. Koether. Hampden-Sydney College. Fri, Feb 2, 2018

UNIVERSITY OF CAMBRIDGE INTERNATIONAL EXAMINATIONS International General Certificate of Secondary Education

Business Intelligence and Data Analysis Workshop. Contents are subject to change. For the latest updates visit

Job Data Activity Report HRB310

Human Resource Management System User Guide

MSITA: Excel Test #1

Salary Planning Procedures User Guide

Transcription:

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