Professional Documents
Culture Documents
Oracle Fundamentals-4
Oracle Fundamentals-4
Oracle Fundamentals-4
Database System
Oracle8
Database
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
006 Vasanthi K. Tailor 03-MAY-1999 03 004 3750
009 Venkataraman R. Manager 24-JUN-1999 01 001 14250 750
011 Prabhuram S. Designer 25-JUN-1999 03 019 15700 500
012 Denzil Ashok Sales Executive 12-JUL-1999 02 002 8000 750
014 Raghu M. Sales Executive 12-JUL-1999 02 002 7500 750
015 Subramaniam G. Manager 20-AUG-1999 04 001 13200
016 Ganesh P.R. Clerk 03-SEP-1999 01 009 3000
019 Ramarathnam R.S. Manager 16-OCT-1999 03 001 20450 1250
021 Row
Jayanthi M.S. Tailor Column
22-DEC-1999 03 004 3000
022 Prakash D. Sales Executive 22-DEC-1999 02 002 7500 750
023 Junaid Ismail Tailor 08-JAN-2000 03 004 3500
024 Suryakumar Sales Executive 15-FEB-2000 02 002 7250 500
Rows and Columns
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
006 Vasanthi K. Tailor 03-MAY-1999 03 004 3750
009 Venkataraman R. Manager 24-JUN-1999 01 001 14250 750
011 Prabhuram S. Designer 25-JUN-1999 03 019 15700 500
012 Denzil Ashok Sales Executive 12-JUL-1999 02 002 8000 750
014 Raghu M. Sales Executive 12-JUL-1999 02 002 7500 750
015 Subramaniam G. Manager 20-AUG-1999 04 001 13200
016 Ganesh P.R. Clerk 03-SEP-1999 01 009 3000
019 Ramarathnam R.S. Manager 16-OCT-1999 03 001 20450 1250
A
021 row contains information
Jayanthi M.S. about
Tailorone A column contains
22-DEC-1999 03 one type of
004 information3000
entity - a thing,
022 a person
Prakash D. or anSales
event.
Executive about all entities.
22-DEC-1999 02 002 7500 750
023 Junaid Ismail Tailor 08-JAN-2000 03 004 3500
024 Suryakumar Sales Executive 15-FEB-2000 02 002 7250 500
Tables - Another Example
Table name
Column name
DEPARTMENTS
DEPT_NO NAME HEAD LOCATION
01 Admin/Accounts 001 City Office
02 Sales 002 City Office
03 Production 019 Factory
04 Purchase 015 Factory
Table Relationships
SALARY_PAYMENTS
DEPARTMENTS
EMPLOYEES
LOAN_TYPES
LEAVE_TAKEN
LOANS
Relational
Database
Why ‘Relational’?
Why ‘Relational’?
• Not because the tables are ‘related’ - this is a common
misconception.
• Actual reason:
• The database stores and manipulates information in tables.
• Table manipulation is based on a mathematical subject called
‘relational algebra’ (or ‘relational calculus’) where the term
‘relation’ means a table structure.
• Hence the name ‘relational database’.
Structured Query Language (SQL)
• Is the subset of SQL that is used to define the data structures (such
as tables, views etc.) in a relational database.
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
DEPARTMENTS
DEPT_NO NAME HEAD LOCATION
Data structures
Data Manipulation Language (DML)
• Is the subset of SQL that is used to create, modify and delete data in
the tables.
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
DEPARTMENTS
Data DEPT_NO NAME HEAD LOCATION
01 Admin/Accounts 001 City Office
02 Sales 002 City Office
03 Production 019 Factory
04 Purchase 015 Factory
Data Control Language (DCL)
Databas
e
Data type
NAME holds text values HIRE_DATE holds date values SALARY holds numeric values
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
• Data type also determines the kind of operations that can take place
on the column values.
• E.g. Numbers can be added but dates cannot be added; Text values can be
converted to upper case while numbers cannot be.
Quiz
• Number
• Varchar2
• Date
• Char
• Others
• Varchar
• Long
• Raw
• Long Raw
Primary Key
• In order to process any row, we first need to identify (or point out)
the row. E.g.
• To process the salary of a particular employee, we first need to mention which
employee it is.
• For instance, we may say ‘Kumar P.’ or we may say EMP_NO ‘004’.
• This column (EMP_NO) is called the ‘primary key’ of the EMPLOYEES table.
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
SALARY_PAYMENTS
EMP_NO MONTH SALARY
001 APR 99 17750
002 APR 99 7000
… … …
001 MAY 99 35500
002 MAY 99 21000
… … … Composite primary key
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
More on Null
Nulls as Zeroes
Formatting mask
Inserting values for only some columns
INSERT INTO EMPLOYEES VALUES ('001', 'Muthukrishnan K.', 'Director', TO_DATE('15-APR-1999', 'DD-MON-YYYY'), '01',
NULL, 35500, NULL);
INSERT INTO EMPLOYEES VALUES ('002', 'Sridhara Rao', 'Manager', TO_DATE('19-APR-1999', 'DD-MON-YYYY'), '02',
'001', 21000, 1500);
INSERT INTO EMPLOYEES VALUES ('004', 'Kumar P.', 'Supervisor', TO_DATE('03-MAY-1999', 'DD-MON-YYYY'), '03',
'019', 10500, NULL);
INSERT INTO EMPLOYEES VALUES ('005', 'Mani S.', 'Tailor', TO_DATE('03-MAY-1999', 'DD-MON-YYYY'), '03', '004',
3500, NULL);
INSERT INTO EMPLOYEES VALUES ('006', 'Vasanthi K.', 'Tailor', TO_DATE('03-MAY-1999', 'DD-MON-YYYY'), '03', '004',
3750, NULL);
INSERT INTO EMPLOYEES VALUES ('009', 'Venkataraman R.', 'Manager', TO_DATE('24-JUN-1999', 'DD-MON-YYYY'), '01',
'001', 14250, 750);
INSERT INTO EMPLOYEES VALUES ('011', 'Prabhuram S.', 'Designer', TO_DATE('25-JUN-1999', 'DD-MON-YYYY'), '03',
insert into EMPLOYEES values (‘002’, ‘Sridhara Rao’,
'019', 15700, 500);
INSERT INTO EMPLOYEES VALUES
('012', 'Denzil Ashok', 'Sales Executive', TO_DATE('12-JUL-1999', 'DD-MON-YYYY'),
‘Manager’, ‘19-APR-99’,
'02', '002', 8000, 750); NULL, ‘001’, 21000, 1500);
INSERT INTO EMPLOYEES VALUES ('014', 'Raghu M.', 'Sales Executive', TO_DATE('12-JUL-1999', 'DD-MON-YYYY'), '02',
'002', 7500, 750);
INSERT INTO EMPLOYEES VALUES ('015', 'Subramaniam G.', 'Manager', TO_DATE('20-AUG-1999', 'DD-MON-YYYY'), '04',
'001', 13200, NULL);
INSERT INTO EMPLOYEES VALUES ('016', 'Ganesh P.R.', 'Clerk', TO_DATE('03-SEP-1999', 'DD-MON-YYYY'), '01', '009',
3000, NULL);
INSERT INTO EMPLOYEES VALUES ('019', 'Ramarathnam R.S.', 'Manager', TO_DATE('16-OCT-1999', 'DD-MON-YYYY'), '03',
'001', 20450, 1250);
INSERT INTO EMPLOYEES VALUES ('021', 'Jayanthi M.S.', 'Tailor', TO_DATE('22-DEC-1999', 'DD-MON-YYYY'), '03',
'004', 3000, NULL);
INSERT INTO EMPLOYEES VALUES ('022', 'Prakash D.', 'Sales Executive', TO_DATE('22-DEC-1999', 'DD-MON-YYYY'),
'02', '002', 7500, 750);
INSERT INTO EMPLOYEES VALUES ('023', 'Junaid Ismail', 'Tailor', TO_DATE('08-JAN-2000', 'DD-MON-YYYY'), '03',
'004', 3500, NULL);
INSERT INTO EMPLOYEES VALUES ('024', 'Suryakumar', 'Sales Executive', TO_DATE('15-FEB-2000', 'DD-MON-YYYY'),
'02', '002', 7250, 500);
Seeing the rows
select * from
EMPLOYEES;
Meaning:
Show
all columns
from
this table.
Primary Key of
EMPLOYEES table Foreign Key referring
DEPARTMENTS table
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
DEPARTMENTS
DEPT_NO NAME HEAD LOCATION
01 Admin/Accounts 001 City Office
02 Sales 002 City Office
03 Production 019 Factory
04 Purchase 015 Factory
?
Tables, Primary Keys and Foreign Keys - Relationship
• A foreign key in a table can refer to (the primary key of) the same
table itself.
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
• Can you think of some other foreign key that refers its own table?
• Are such self-referring tables common or rare?
Foreign Keys in EMPLOYEES table
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
Primary Key of
EMPLOYEES table
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
• The name of the primary key column can be left out in the constraint
definition if it matches the name of the foreign key column.
• Applies to both column constraints and table constraints.
Primary key column name left out Not so here because the primary key
because it matches foreign key column name (EMP_NO) does not match
column name (DEPT_NO). foreign key column name (MGR_NO).
Next Step: Creating the Foreign Keys for DEPARTMENTS table
• What will happen if the order of the SQL statements was changed?
• Sometimes there is a need to ensure that a column does not have any
duplicate values. E.g.
• No two departments in Patanjali Garments can have the same name.
DEPARTMENTS
DEPT_NO NAME HEAD LOCATION
01 Admin/Accounts 001 City Office
02 Sales 002 City Office
03 Production 019 Factory
04 Purchase 015 Factory
• Many columns must have values that are within a certain range or
that satisfy certain conditions. E.g.
• The salary of an employee has to be greater than 0.
• The hire date of an employee has to be later than 15-APR-1999, the
date when the organization was started.
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
• Steps
• Let only one the latter table have a foreign key reference to the former table.
• Then ALTER the former table to include the foreign key reference to the latter
table.
create table EMPLOYEES
(EMP_NO varchar2(3) not null primary key,
NAME varchar2(25) not null,
JOB varchar2(20) not null,
HIRE_DATE date not null check (HIRE_DATE > ‘15-APR-99’),
DEPT_NO varchar2(2),
MGR_NO varchar2(3) references EMPLOYEES(EMP_NO),
SALARY number(10) not null check (SALARY > 0),
SPEC_ALL number(10));
create table DEPARTMENTS
(DEPT_NO varchar2(2) not null primary key,
NAME varchar2(25) not null unique,
HEAD varchar2(3) references EMPLOYEES(EMP_NO),
LOCATION varchar2(25));
alter table EMPLOYEES add
(foreign key (DEPT_NO) references DEPARTMENTS(DEPT_NO));
Mutually Referring Tables - Solution 2
• Steps
• Let table definitions have no foreign key references.
• After all table definitions, alter the tables to include all foreign key references.
• Ideal solution/ Also professional practice
create table EMPLOYEES
(EMP_NO varchar2(3) not null primary key,
NAME varchar2(25) not null,
JOB varchar2(20) not null,
HIRE_DATE date not null check (HIRE_DATE > ‘15-APR-99’),
DEPT_NO varchar2(2),
MGR_NO varchar2(3),
SALARY number(10) not null check (SALARY > 0),
SPEC_ALL number(10));
create table DEPARTMENTS
(DEPT_NO varchar2(2) not null primary key,
NAME varchar2(25) not null unique,
HEAD varchar2(3),
LOCATION varchar2(25));
alter table EMPLOYEES add
(foreign key (DEPT_NO) references DEPARTMENTS(DEPT_NO),
foreign_key (MGR_NO) references EMPLOYEES(EMP_NO));
alter table DEPARTMENTS add
(foreign key (HEAD) references EMPLOYEES(EMP_NO));
Seeing the Rows
select * from
EMPLOYEES;
Meaning:
Show
all columns
from
this table.
Select: Entire table
• Need: To see all the column details of all the rows in a table.
• See all the details of all the employees.
• Solution:
select * from
EMPLOYEES;
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
006 Vasanthi K. Tailor 03-MAY-1999 03 004 3750
009 Venkataraman R. Manager 24-JUN-1999 01 001 14250 750
011 Prabhuram S. Designer 25-JUN-1999 03 019 15700 500
012 Denzil Ashok Sales Executive 12-JUL-1999 02 002 8000 750
014 Raghu M. Sales Executive 12-JUL-1999 02 002 7500 750
015 Subramaniam G. Manager 20-AUG-1999 04 001 13200
016 Ganesh P.R. Clerk 03-SEP-1999 01 009 3000
019 Ramarathnam R.S. Manager 16-OCT-1999 03 001 20450 1250
021 Jayanthi M.S. Tailor 22-DEC-1999 03 004 3000
022 Prakash D. Sales Executive 22-DEC-1999 02 002 7500 750
023 Junaid Ismail Tailor 08-JAN-2000 03 004 3500
024 Suryakumar Sales Executive 15-FEB-2000 02 002 7250 500
Select: One column
• Need: To see the rows sorted by more than one column (i.e. sorted first by
one column and, within that, sorted by another column.)
• See the employee details ordered by their departments and then by their jobs.
• Solution:
select * from EMPLOYEES order by DEPT_NO, JOB;
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
016 Ganesh P.R. Clerk 03-SEP-1999 01 009 3000
001 Muthukrishnan K. Director 15-APR-1999 01 35500
009 Venkataraman R. Manager 24-JUN-1999 01 001 14250 750
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
012 Denzil Ashok Sales Executive 12-JUL-1999 02 002 8000 750
014 Raghu M. Sales Executive 12-JUL-1999 02 002 7500 750
022 Prakash D. Sales Executive 22-DEC-1999 02 002 7500 750
024 Suryakumar Sales Executive 15-FEB-2000 02 002 7250 500
011 Prabhuram S. Designer 25-JUN-1999 03 019 15700 500
019 Ramarathnam R.S. Manager 16-OCT-1999 03 001 20450 1250
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
006 Vasanthi K. Tailor 03-MAY-1999 03 004 3750
021 Jayanthi M.S. Tailor 22-DEC-1999 03 004 3000
023 Junaid Ismail Tailor 08-JAN-2000 03 004 3500
015 Subramaniam G. Manager 20-AUG-1999 04 001 13200
Select: Effect of changing ordering columns
• Need: To see the rows sorted by more than one column and in a
descending order.
• See the employee details ordered by their departments in the descending order
and then by their jobs in the ascending order.
• Solution:
select * from EMPLOYEES order by DEPT_NO desc, JOB;
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
015 Subramaniam G. Manager 20-AUG-1999 04 001 13200
011 Prabhuram S. Designer 25-JUN-1999 03 019 15700 500
019 Ramarathnam R.S. Manager 16-OCT-1999 03 001 20450 1250
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
006 Vasanthi K. Tailor 03-MAY-1999 03 004 3750
021 Jayanthi M.S. Tailor 22-DEC-1999 03 004 3000
023 Junaid Ismail Tailor 08-JAN-2000 03 004 3500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
012 Denzil Ashok Sales Executive 12-JUL-1999 02 002 8000 750
014 Raghu M. Sales Executive 12-JUL-1999 02 002 7500 750
022 Prakash D. Sales Executive 22-DEC-1999 02 002 7500 750
024 Suryakumar Sales Executive 15-FEB-2000 02 002 7250 500
016 Ganesh P.R. Clerk 03-SEP-1999 01 009 3000
001 Muthukrishnan K. Director 15-APR-1999 01 35500
009 Venkataraman R. Manager 24-JUN-1999 01 001 14250 750
Select: Pattern matching: Matching multiple characters
Warning:
‘ = null’ will not work!
Select: Looking for Not Null
• Better solution:
select * from EMPLOYEES where JOB in
(‘Manager’, ‘Supervisor’, ‘Sales Executive’);
Select: Not in a list of values
• Need: To see the rows where a column’s value is not among a list of
values.
• See the employees who do not work in department ‘01’ or ‘03’.
• Earlier solution:
select * from EMPLOYEES where DEPT_NO <> ‘01’ and
DEPT_NO <> ‘03’;
• Better solution:
select * from EMPLOYEES where DEPT_NO not in
(‘01’, ‘03’);
Select: Range of values
• Better solution:
select * from EMPLOYEES where SALARY between 10000 and
20000;
Select: Not in a range of values
• Need: To see the rows where a column’s value is not within a range
of values.
• See the employees who did not join between ‘1 Jun 1999’ and ‘31 Dec
1999’
• Earlier solution:
select * from EMPLOYEES where
HIRE_DATE < to_date(‘01-JUN-1999’, ‘DD-MON-YYYY’) or
HIRE_DATE > to_date(‘31-DEC-1999’, ‘DD-MON-YYYY’);
• Better solution:
select * from EMPLOYEES where HIRE_DATE not between
to_date(‘01-JUN-1999’, ‘DD-MON-YYYY’) and
to_date(‘31-DEC-1999’, ‘DD-MON-YYYY’);
Modifying the Contents of a Table
Warning:
1) The absence of the where clause will result
in all the rows getting updated.
2) An improper where clause can affect
unwanted rows.
Warning:
1) The absence of the where clause will result
in all the rows getting deleted.
2) An improper delete clause can delete rows
that we may not want to remove.
Action
Operation Non-operation
(Causes state change) (No state change)
Action
Operation Non-operation
(Causes state change) (No state change)
SQL DML
Action
Operation Non-operation
(Causes state change) (No state change)
SQL DDL
• Changes made to the rows of a table (through inserts, updates and deletes)
are actually not made final in the database until they are committed.
• Changes are also not visible to other users until committed.
• Committing makes the changes final and also makes the changes visible to
other users.
insert into EMPLOYEES values (‘001’, ‘Muthukrishnan
K.’, ‘Director’, ‘15-APR-99’, NULL, NULL, 35500, NULL);
1 row inserted.
commit;
commit complete
The Muthukrishnan row is added to the table but not made permanent.
The row is available to this user but not to other users.
1 row inserted.
rollback;
rollback complete
1 row inserted.
1 row inserted.
1 row updated.
rollback;
rollback complete
commit;
commit complete
rollback;
rollback complete
Commits all the previous uncommitted changes (2 inserts).
commit;
commit complete
rollback;
rollback complete
Warning:
This message is misleading. Since there are no
uncommitted changes, rollback has actually done
nothing.
Committing: Explicit and Implicit
• ‘commit’ command explicitly commits the changes.
• Under certain circumstances, Oracle automatically commits the
changes.
• This is called ‘implicit commit’.
• Commands for which implicit commit takes place:
• quit
• exit
• create table or create view
• drop table or drop view
• grant or revoke
• connect or disconnect
• alter
• audit or noaudit
Quiz
• Power failure
• Computer failure
• Network failure
• Hard disk crash
• Database crash
Commit and Rollback: A Scenario
set autocommit on
insert into EMPLOYEES values (‘001’, ‘Muthukrishnan K.’,
‘Director’, ‘15-APR-99’, NULL, NULL, 35500, NULL);
1 row inserted.
Warning:
The truncate command cannot be rolled
back. Hence be cautious in using it.
More on Selecting Rows
Selecting Distinct Entries
JOB
--------------------
Director
Manager
Supervisor
Tailor
Tailor
Manager
Designer
Sales Executive
Sales Executive
Manager Many entries appear several times.
Clerk
Manager
Tailor
Sales Executive
Tailor
Sales Executive
16 rows selected.
Selecting Distinct Entries
JOB
--------------------
Clerk
Designer
Director
Manager
Sales Executive
Supervisor
Tailor
7 rows selected.
NAME SALARY+SPEC_ALL
------------------------- --------------- Expression
Muthukrishnan K.
Sridhara Rao 22500
Kumar P.
Mani S.
Vasanthi K.
By the way, why are many
Venkataraman R. 15000
Prabhuram S. 16200
total salaries null? We will
Denzil Ashok 8750 see later.
Raghu M. 8250
Subramaniam G.
Ganesh P.R.
Ramarathnam R.S. 21700
Jayanthi M.S.
Prakash D. 8250
Junaid Ismail
Suryakumar 7750
Expressions
NAME 'WORKSINDEPARTMENTNO:' DE
-------------------- ----------------------- -- Literal
Muthukrishnan K. works in department no: 01
Sridhara Rao works in department no: 02
Kumar P. works in department no: 03
Mani S. works in department no: 03
Vasanthi K. works in department no: 03
...
Function Purpose
SUM(column) Finds the sum of a column's values.
MAX(column) Finds the maximum value in a column.
MIN(column) Finds the minimum value in a column.
AVG(column) Finds the average value in a column.
COUNT(*) Counts the number of rows.
COUNT(column) Counts the number of non-null values in a column.
STDDEV(column) Finds the standard deviation in a column.
VARIANCE(column) Finds the variance in a column.
Function Purpose
ROUND(value, precision) Rounding of value to the given precision
EXP(value) 'e' raised to the value (evalue)
POWER(value, exponent) Value raised to the exponent (valueexponent)
MOD(value, divisor) Modulus of value with respect to given divisor
ABS(value) Absolute value
LN(value) Natural logarithm of value
LOG(value) Base 10 logarithm of value
SIN(value) Trigonometric sine of value
Important String Functions
Function Purpose
|| Concatenates two strings
CONCAT Concatenates two strings. Same as ||
LENGTH Number of characters in the string
LOWER String in lowercase
UPPER String in uppercase
SUBSTR Extracts the substring from 'string'
INSTR Finds the location of a substring in a string
INITCAP String in InitCap format
SOUNDEX Finds words that sound like given string
Important Date Functions
Function Purpose
MONTHS_BETWEEN Number of months between two dates
ADD_MONTHS Adds months to a given date
LAST_DAY Last date of the month that the given date is in
Important Generic Functions
Function Purpose
NVL(value, substitute) Substitute for value if value is null
GREATEST(value1, value2, …) Greatest of the values (For numbers, the largest number;
for strings, the last in the alphabetical order; for dates, the
latest date)
LEAST(value1, value2, …) Least of the values (For numbers, the smallest number;
for strings, the first in the alphabetical order; for dates,
the earliest date)
DECODE Conditional value
Important Conversion Functions
Function Purpose
TO_CHAR Converts a number or date to a character string
TO_DATE Converts a string or number to a date
TO_NUMBER Converts a string to a number
Function types
Group-Value Functions
Select: Applying functions on rows
SUM(SALARY)
-----------
177600
Select: Average of a column
AVG(SALARY)
-----------
11100
Select: Maximum of a column
MAX(SPEC_ALL)
-------------
1500
Select: Counting the rows
COUNT(*)
----------
16
COUNT(NAME)
-----------
16 P.Srinivasan:
Introduce
DISTINCT
Group-value functions - List
Function Purpose
sum(column) Finds the sum of a column's values.
max(column) Finds the maximum value in a column.
min(column) Finds the minimum value in a column.
avg(column) Finds the average value in a column.
count(*) Counts the number of rows.
count(column) Counts the number of non-null values in a column.
stddev(column) Finds the standard deviation in a column.
variance(column) Finds the variance in a column.
Group-value functions and Nulls
COUNT(SPEC_ALL)
---------------
8
Tips:
COUNT(column) is very useful to find the
number of non-null entries in a column.
Multiple group functions in a select statement
AVG(SALARY+NVL(SPEC_ALL,0))
---------------------------
11521.875
DISTINCT in group functions
COUNT(MGR_NO)
-------------
15
Reminder:
count(column) will not include
nulls in the counting process.
DISTINCT in group functions
COUNT(DISTINCTMGR_NO)
---------------------
5
• Until now, we had grouped all rows in a table and found out
something about the group.
• Sometimes we may want to group the rows in the table on some
column and then find something about the groups. E.g.
• We may want to group the employees according to their departments
and find the number of employees in each department.
• We may want to group the employees based on their jobs and find the
maximum salary in each job.
• We may want to group the employees based on their bosses and find
the number of subordinates for every boss.
• This is done by using the GROUP BY clause.
Group By
DE COUNT(*)
-- ----------
01 3
02 5
03 7
04 1
• Need: Group the employees based on their jobs and find the
maximum salary in each job.
• Solution:
select JOB, MAX(SALARY) from EMPLOYEES group by JOB;
JOB MAX(SALARY)
-------------------- -----------
Clerk 3000
Designer 15700
Director 35500
Manager 21000
Sales Executive 8000
Supervisor 10500
Tailor 3750
Group By - Rules for the selected columns
• Once the rows are grouped together, only group-values (like sum,
average, maximum etc.) of columns can be obtained. The row values
cannot be obtained.
• Hence all items in the select statement should be group-value
functions; the only exception is the grouping column itself.
Once the rows are grouped by job, the individual salaries are not
available. Hence this statement will cause an error.
Grouping - The Concept of its Working
DE MAX(SALARY)
-- -----------
01 35500
02 21000
03 20450
04 13200
1 When you group by a column, the rows are segregated into different
groups based on that column.
EMP NAME JOB HIRE_DATE DE MGR SALARY
SPEC_ALL
--- -------------------- -------------------- --------- -- --- ----------
---------
001 Muthukrishnan K. Director 15-APR-99 01 35500
009 Venkataraman R. Manager 24-JUN-99 01 001 14250
750
016 Ganesh P.R. Clerk 03-SEP-99 01 009 3000
2 The group function is then applied for every group and one output row is
produced for every group.
EMP NAME JOB HIRE_DATE DE MGR SALARY
SPEC_ALL
--- -------------------- -------------------- --------- -- --- ----------
---------
001 Muthukrishnan K. Director 15-APR-99 01 35500
009 Venkataraman R. Manager 24-JUN-99 01 001 14250
750
016 Ganesh P.R. Clerk 03-SEP-99 01 009 3000
DE MAX(SALARY)
-- -----------
01 35500
02 21000
03 20450
04 13200
Group By
DE COUNT(*)
-- ----------
01 1
02 4
03 3
04 1
The where clause first restricts the rows that are grouped.
Then the rows are grouped and the group functions are performed.
Selecting Specific Groups
Note:
WHERE clause restricts rows while
HAVING clause restricts groups.
Having clause
DE AVG(SALARY)
-- -----------
01 17583.3333
04 13200
DE AVG(SALARY)
-- -----------
02 10250
03 8628.57143
Having - Rules for the conditions
• Much like the rules for the selected columns in grouping operations,
the conditions in the HAVING clause should also be only group-
value functions.
select DEPT_NO, AVG(SALARY) from EMPLOYEES group by
DEPT_NO having COUNT(*) >= 5;
DE AVG(SALARY)
-- -----------
02 10250
03 8628.57143
DE AVG(SALARY)
-- -----------
01 17583.3333
02 10250
• Better alternative:
select DEPT_NO, AVG(SALARY) from EMPLOYEES
where DEPT_NO in ('01', '02') group by DEPT_NO;
DE AVG(SALARY)
-- ----------- This approach is faster because it
01 17583.3333 eliminates rows even before grouping!
02 10250
Grouping on multiple columns
DE YEAR COUNT(*)
-- ---- ----------
01 1999 3
02 1999 4
02 2000 1
03 1999 6
03 2000 1
04 1999 1
DE AVG(SALARY)
-- -----------
01 17583.3333
02 10250
03 8628.57143
04 13200
DE AVG(SALARY)
-- -----------
01 17583.3333 Ordered explicitly
04 13200
02 10250
03 8628.57143
Ordering - Rules for the ordering columns
• Much like the rules for the selected columns and the having
conditions in grouping operations, the order by clause can only
use either group-value functions or the grouping columns.
select DEPT_NO, AVG(SALARY)
from EMPLOYEES
group by DEPT_NO
order by AVG(SALARY) desc;
DE AVG(SALARY)
-- -----------
01 17583.3333
04 13200
02 10250
03 8628.57143
The order by clause can use only group functions or the grouping columns.
Combining all clauses
JOB AVG(SALARY)
-------------------- -----------
Clerk 3000
Tailor 3416.66667
Sales Executive 7666.66667
Supervisor 10500
Order of Execution
1) First the relevant rows are chosen based on the where clause
where HIRE_DATE < TO_DATE('01-JAN-2000', 'DD-MON-YYYY')
• One row is formed for every group. The intermediate result looks
like this:
JOB AVG(SALARY)
-------------------- -----------
Clerk 3000
Designer 15700
Director 35500
Manager 17225
Sales Executive 7666.66667
Supervisor 10500
Tailor 3416.66667
Order of Execution
JOB AVG(SALARY)
-------------------- -----------
Clerk 3000
Sales Executive 7666.66667
Supervisor 10500
Tailor 3416.66667
order by AVG(SALARY);
JOB AVG(SALARY)
-------------------- -----------
Clerk 3000
Tailor 3416.66667
Sales Executive 7666.66667
Supervisor 10500
• Fortunately, the SQL syntax itself clearly depicts the order of execution:
JOB AVG(
-------------------- ----
Clerk
Tailor 341
Sales Executive 766
Supervisor
Dummy slide (not part of presentation)
EMPLOYEES
EMP_NO NAME JOB HIRE_DATE DEPT_NO MGR_NO SALARY SPEC_ALL
001 Muthukrishnan K. Director 15-APR-1999 01 35500
002 Sridhara Rao Manager 19-APR-1999 02 001 21000 1500
004 Kumar P. Supervisor 03-MAY-1999 03 019 10500
005 Mani S. Tailor 03-MAY-1999 03 004 3500
006 Vasanthi K. Tailor 03-MAY-1999 03 004 3750
009 Venkataraman R. Manager 24-JUN-1999 01 001 14250 750
011 Prabhuram S. Designer 25-JUN-1999 03 019 15700 500
012 Denzil Ashok Sales Executive 12-JUL-1999 02 002 8000 750
014 Raghu M. Sales Executive 12-JUL-1999 02 002 7500 750
015 Subramaniam G. Manager 20-AUG-1999 04 001 13200
016 Ganesh P.R. Clerk 03-SEP-1999 01 009 3000
019 Ramarathnam R.S. Manager 16-OCT-1999 03 001 20450 1250
021 Jayanthi M.S. Tailor 22-DEC-1999 03 004 3000
022 Prakash D. Sales Executive 22-DEC-1999 02 002 7500 750
023 Junaid Ismail Tailor 08-JAN-2000 03 004 3500
024 Suryakumar Sales Executive 15-FEB-2000 02 002 7250 500