You are on page 1of 23

SQL Part 2 Schaum Chapters 4, 5, 6 and 7

Theme for the Day:

Built-In Functions
dual table (unique to Oracle) Sequences Group Functions Date Types Single-row Subqueries Multiple-row Subqueries
Dr. Philip Cannata Data Management 1

SQL Part 2 Schaum Chapters 4


Built-In Functions

Individual Numeric
abs(m) update emp set sal = -5 where sal = 800

select sal from emp


select abs(sal) from emp power(m,n)

round(m,n)
trunc(m,n) mod(m, n)

select sal, trunc(sal/3) * 3, sal - (trunc(sal/3) * 3), mod(sal, 3) from emp


Dr. Philip Cannata Data Management 2

Selected Oracle Numeric Functions

Dr. Philip Cannata

Data Management

SQL Part 2 Schaum Chapters 4


Built-In Functions Character see Schaum page 140 concat(string1, string2) || (similar to + in java) select 'something ' || concat(ename, concat(' ',job)) from emp substr(string, begin, count)

inticap(string)
select initcap('something ' || concat(ename, concat(' ',job))) from emp

Dr. Philip Cannata

Data Management

Selected Oracle String Functions

Dr. Philip Cannata

Data Management

Selected Oracle Conversion Functions

Dr. Philip Cannata

Data Management

Selected Oracle Conversion Functions (continued)

Dr. Philip Cannata

Data Management

SQL Part 2 Schaum Chapters 4


Built-In Functions
Dealing with NULL values NVL(m,n) select sal, comm, sal*comm from emp

select sal, comm, sal*nvl(comm, 0) from emp


update emp set job = NULL where ename = 'SMITH' select ename, job from emp where sal < 1000 select ename, nvl(job, 'CEO') from emp where sal < 1000 NVL2(x, y, z) - select ename, nvl2(comm, 0, 1) from emp

coalesce(m,n) - select ename, coalesce(comm, 0, 1, 2) from emp


Dr. Philip Cannata Data Management 8

Oracle Sequence

Dr. Philip Cannata

Data Management

Oracle Sequence Examples

Dr. Philip Cannata

Data Management

10

SQL Part 3 Schaum Chapters 6


dual table

Describe dual

Name

Null? Type

-------------------------------------------- -------- --------------------------------DUMMY VARCHAR2(1)

select power(2,3) from dual

POWER(2,3)
---------8 1 row selected.
Dr. Philip Cannata Data Management 11

SQL Part 2 Schaum Chapters 5


Group Functions
count(*) count(column-name)

count(distinct column-name)
select count(distinct job) from emp max(column-name)

min(column-name)
avg(column-name) sum(column-name)
Dr. Philip Cannata Data Management 12

SQL Part 2 Schaum Chapters 5


This wont work select job, max(sal) from emp select job, max(sal) from emp * ERROR at line 1: ORA-00937: not a single-group group function This will work - select job, max(sal) from emp group by job How about this? - select job, max(sal) from emp group by job having sal > 2000 Or this? - select job, max(sal) from emp group by job having max(sal) > 2000 Or this? - select job, max(sal) from emp group by job having job in ('CLERK', 'SALESMAN')
Dr. Philip Cannata Data Management 13

SQL Part 2 Schaum Chapters 5


This wont work select job, max(sal) from emp select job, max(sal) from emp * ERROR at line 1: ORA-00937: not a single-group group function This will work - select job, max(sal) from emp group by job How about this? - select job, max(sal) from emp group by job having sal > 2000 Or this? - select job, max(sal) from emp group by job having max(sal) > 2000 Or this? - select job, max(sal) from emp group by job having job in ('CLERK', 'SALESMAN')
Dr. Philip Cannata Data Management

No

Yes

Yes
14

SQL Part 3 Schaum Chapters 6


Date Types sysdate date + 1 add_months(date, number) last_day(date) months_between(date1, date2) Learn for your own edification

next_day(date, day_number)
greatest(date1, date2, , dateN) least(date1, date2, , dateN)

Dr. Philip Cannata

Data Management

15

Selected Oracle Date/Time Functions

Dr. Philip Cannata

Data Management

16

Selected Oracle Date/Time Functions (continued)

Dr. Philip Cannata

Data Management

17

SQL Part 3 Schaum Chapters 7


Single-row Subqueries select * from emp where sal = (select round(avg(sal) + 500, -3) from emp)
Round to nearest 1000

select * from emp where sal < (select avg(sal) from emp)
select * from emp where sal > (select avg(sal) from emp) select * from emp where sal <> (select round(avg(sal) + 500, -3) from emp) order by ename

Dr. Philip Cannata

Data Management

18

SQL Part 3 Schaum Chapters 7


Multiple-row Subqueries
select distinct nvl(comm, 110) * 10 from emp select * from emp where sal in (select distinct nvl(comm, 110) * 10 from emp) order by sal select * from emp where sal not in (select distinct nvl(comm, 110) * 10 from emp) order by sal select * from emp where sal = any (select distinct nvl(comm, 110) * 10 from emp) order by sal select * from emp where sal <> any (select distinct nvl(comm, 110) * 10 from emp) order by sal

Dr. Philip Cannata

Data Management

19

SQL Part 3 Schaum Chapters 7


Multiple-row Subqueries

Operator

Description

> ANY
< ANY > ALL < ALL = ANY <> ANY <> ALL

More than the lowest value returned by the subquery


Less that the highest value returned by the subquery More than the highest value returned by the subquery Less than the lowest value returned by the subquery Same as IN Same as select * Same as NOT IN

Dr. Philip Cannata

Data Management

20

SQL Part 3 Schaum Chapters 7


Multiple-row Subqueries select * from emp where sal > any (select distinct nvl(comm, 110) * 10 from emp) order by sal select * from emp where sal > all (select distinct nvl(comm, 110) * 10 from emp)
select sal from emp where sal not in (select distinct a.sal from emp a, emp b where a.sal < b.sal)

Dr. Philip Cannata

Data Management

21

SQL Part 3 Schaum Chapters 7


What will this return?
select sal from emp where sal not in (select a.sal from emp a, emp b where a.sal < b.sal) What will this return? select sal from emp where sal > all (select a.sal from emp a, emp b where a.sal < b.sal) What will this return? select max(sal) from emp
Dr. Philip Cannata Data Management 22

SQL Part 2 Schaum Chapters 4, 5, 6 and 7

Learn all the functions in the tables on Schaum pages 132, 140, 151, 176, 219 and 220

You should do all the problems in Schaum Chapters 4, 5, 6 and 7 for proper practice in using SQL.

Dr. Philip Cannata

Data Management

23

You might also like