Sie sind auf Seite 1von 33

An Introduction to SQL using Oracle

Example queries and exercises

University of Hertfordshire
Department of Computer Science

Relational Database Management Systems


CONTENTS
Basic Model of Relational DBMS
Available Relational DBMS
SQL Data Definition Language
SQL Data Manipulation Language Introduction
Retrieving Data
Inserting and Updating
Deleting
Queries with dates
Pattern Matching
Joining a table with itself
Further SQL examples
Data Independence
Views
Updating Views
Advantages of Views
SQL Practice
SQL Practice Answers
Retrieval with SQL Reference

HATFIELD HOSPITAL DATABASE


PATIENT
PNO
p3
p7
p2
p8
p4
p1

PNAME
mansell
dury
currie
gooch
gooch
minogue

TITLE
mr
mrs
mrs
mr
mrs
miss

DOB
23-May-61
05-Jun-64
13-Jan-55
12-Apr-53
03-Jun-59
03-Aug-69

CHILDREN
2
0
3
1
1
0

DOI
20-Apr-87
12-Mar-91
10-Jul-88
04-Sep-90
05-Aug-89

COST
0.15
5.00
1.89
0.90
1.66

GP
Dr.Williams
Dr.Taylor
Dr.Thatcher
Dr.Spock
Dr.Spock
Dr.Williams

DRUG
DNO
d1
d2
d5
d7
d6

DNAME
sweet dreams
bliss
fly high
split
slow down

UNIT
tab
mg
mg
tab
gm

DOSE
PNO
p4
p2
p4
p1
p7
p8
p4
p4
p1
p4
p1
p8
p4

DNO
d5
d6
d5
d1
d1
d7
d6
d7
d7
d2
d6
d2
d1

DOSEDATE
01-Feb-94
12-Jul-94
10-Sep-94
02-Oct-94
20-Oct-94
05-Nov-94
30-Nov-94
02-Jan-95
03-Mar-95
01-Apr-95
05-May-95
31-May-95
05-Jun-95

QTY
5
3
5
3
6
2
2
8
6
3
2
1
6

Note that this database was created some years ago, and so 02-Jan-95 was interpreted
by Oracle at creation time to mean 02-Jan-1995. If you type the string 02-Jan-95 now
it will assume you are referring to 02-Jan-2095.
I might recreate the database, in which case the above will not apply.

REFERENCE ONLY
This shows you how the tables were created, you do not need to do this to yourself as
you can copy populated tables from your lecturer
ORACLE SQL Data Definition Language (DDL)
CREATING A TABLE
BASIC FORMAT OF COMMAND:
CREATE TABLE tablename (columnname format
{,columnname format}
{,primary key/foreign key format});
eg
CREATE TABLE Patient
(Pno
VARCHAR2(4) NOT NULL,
Pname
VARCHAR2(15) NOT NULL,
Title
VARCHAR2(4),
Dob
DATE,
Children
NUMBER(2),
GP
VARCHAR2(15),
Primary Key (Pno) );
CREATE TABLE Drug
(Dno
VARCHAR2(4) NOT NULL,
Dname
VARCHAR2(20),
Unit
VARCHAR2(3),
Doi
DATE,
Cost
NUMBER(6,2),
Primary Key (Dno) );
CREATE TABLE Dose
(Pno
VARCHAR2(4) NOT NULL,
Dno
VARCHAR2(4) NOT NULL,
DoseDate
DATE
NOT NULL,
Qty
NUMBER(4),
Primary Key (Pno,Dno,Dosedate),
Foreign Key (Pno) references Patient(Pno),
Foreign Key (Dno) references Drug(Dno) );
ORACLE DATA TYPES
When a table is created in Oracle it needs to know what type of data is to be stored in
each column (attribute).
The main data types recognised by Oracle are:
char(n)
fixed length character string of n characters
number(n,d)
number made up of n digits with d decimal places.
number(n)
whole number made up of n digits
date
varchar2(n)
variable length character string having maximum length
n
3

REFERENCE ONLY
ORACLE SQL DDL
CREATING AN INDEX
BASIC FORMAT OF COMMAND:
CREATE [UNIQUE] INDEX indexname
ON tablename(columnname{,columname});
eg

CREATE UNIQUE INDEX Pnum ON Patient(Pno);


CREATE UNIQUE INDEX Presc ON Dose
(Pno,Dno,Dosedate);
CREATE INDEX Gps ON Patient(Gp);

DATA MANIPULATION LANGUAGE DML


Four main requirements:
SQL
Add data to the database

Insert

Change data already in the database

Update

Delete data from the database

Delete

Get data out of the database

Select

BASIC FORMAT:
SELECT
FROM
WHERE

a1,a2,.
r1,r2,.
p;

[attribute(s)]
[relation(s)]
[predicate]

Select Examples:
1.

List all the details of all the drugs in the database.


select
from

*
drug;

Result is output of the complete drug table.


2.

Get the drug names of all drugs in the database together with their code
numbers.
select
from

dname,dno
drug;

Result is output of all rows in drug table but just showing the two columns:
DNAME
sweet dreams
bliss
..

DNO
d1
d2
..

3.

Retrieve the names of all drugs in tablet form.


select
from
where

dname
drug
unit='tab';

Result:
DNAME
sweet dreams
split
4.

I need drug nos of drugs in tablet form that cost more than 50p each.
select
from
where
and

dno
drug
unit='tab'
cost>0.50;

Result:
DNO
d7
5.

List patient nos of patients who have Dr. Spock, Dr. Taylor, or Dr. Thatcher as
their GPs.
select
from
where
or
or

pno
patient
gp='Dr.Spock'
gp='Dr.Taylor'
gp='Dr.Thatcher';

Alternatively:
select
from
where

6.

Get the patient nos of all patients except those of Drs Spock, Taylor and
Thatcher.
select
from
where

7.

pno
patient
gpin
('Dr.Spock','Dr.Taylor',Dr.Thatcher')

pno
patient
gpnotin
('Dr.Spock',Dr.Taylor','Dr.Thatcher');

I want the patient names of patients who have been given drug d7.
select
from
where
and

pname
patientp,dosed
p.pno=d.pno
d.dno='d7';
6

beware: finding the names of patients who have NOT been given drug d7 takes
a bit of thought
8.

Get the patient names of Dr. Spock's patients who have been given drug d7.
select
from
where
and
and

9.

Find the patient names of patients who have been given drugs costing more
than 1.75 per unit.
select
from
where
and
and

10.

pname
patientp,dosed
p.pno=d.pno
d.dno='d7'
p.gp='Dr.Spock';

pname
patient
p.pno =
d.dno =
dr.cost

p, dose d, drug dr
d.pno
dr.dno
> 1.75 ;

I know you gave me patient names of patients who have been given drug d7 in
query 7 but I've lost the result so please do it again.
select
from
where

pname
patient
pnoin
(selectpno
fromdose
wheredno='d7');

Now you know a way to find who has not taken drug d7.
11.

I've also lost the results of query 9.


select
from
where

pname
patient
pnoin
(selectpno
fromdose
wherednoin
(selectdno
fromdrug
wherecost>1.75));

AGGREGATE FUNCTIONS
Common set or aggregate functions can be used in SQL
The ones supported in most systems are:
COUNT
SUM
AVG
MAX
MIN

= count the no. of rows


= total the column
= average of column
= maximum value in column
= minimum value in column

NB These functions cannot be used in the 'WHERE' clause of a SQL statement.


12.

Calculate the average unit cost of drugs in tablet form.


select
from
where

avg(cost)as"meancost"
drug
unit='tab';

Output:
meancost
0.53
13.

How many GPs have patients at the hospital?


select
from

count(distinctgp)
patient;

Output:
count
4
14.

What is the total quantity of d7 prescribed?


selectsum(qty)as"d7total"
from
dose
where
dno='d7';

15.

What is the maximum dose of d7 ever given?


selectmax(qty)as"d7max"
from
dose
where
dno='d7';

16.

Now get the total quantities of each of the drugs that we have prescribed.
select
dno,sum(qty)as"total"
from dose
groupby
dno;
8

Output
dno
d1
d2
d5
d6
d7
17.

total
15
4
10
7
16

Fine, but I really only wanted those drugs with 10 or more units used.
select
from
groupby
having

dno,sum(qty)as"total"
dose
dno
sum(qty)>=10;

Output:
dno
d1
d5
d7
18.

total
15
10
16

I'd like the output in descending order of the total column


select
from
groupby
having
orderby

dno,sum(qty)as"total"
dose
dno
sum(qty)>=10
totaldesc;

Output:
dno
d7
d1
d5
19.

total
16
15
10

I don't want any drugs that have been given to patient p7 to be included in the
totals
select
from
where
groupby
having
orderby

dno,sum(qty)as"total"
dose
pno!='p7'
dno
sum(qty)>=10
totaldesc;

Output
dno

total
9

d7
d5

16
10

MODIFYING THE DATABASE INSERTING & UPDATING


21.

I want to add a new drug to the database.


insert into drug values
('d3','zonk','tab','19-Jun-1995', 1.11);

22.

I want to add several new drugs to the database.


insert into drug values
('&dno','&dname','&unit','&doi','&cost);

/*sqlplus will prompt for input values*/


23.

I want to add a new patient but all I have is his pno and name.
insert into patient (pname,pno)
values ('major','p9') ;

24.

I now have some further data on this patient that I want to put in the table.
update patient
set title = 'mr', dob = '3-Nov-1941'
where pno = 'p9' ;

25.

For some reason all of Dr Williams' patients have had their dose quantities
recorded wrongly!
update dose
set qty = qty 1
where pno in
(select pno
from patient
where gp = 'Dr.Williams');

MODIFYING THE DATABASE DELETING


26.

I want to delete all the data on patient p2 from the dose table
delete
from dose
where pno = 'p2' ;

27.

I want to empty the dose table.


delete
from dose;

/* NB Very dangerous be careful ! */


10

11

QUERIES WITH DATES


28. List drug names of drugs introduced from 1989 onwards
select
from
where

dname
drug
doi > '31-Dec-1988' ;

29. List drug names of drugs more than 5 years old together with their age in days
select
from
where

dname, sysdate doi as "Age in days"


drug
months_between (sysdate, doi) > 60 ;

30. List patient nos of patients who were given drugs within 5 years of their
introduction
select
from
where
and

pno
drug dr, dose ds
dr.dno = ds.dno
months_between(dosedate,doi) <= 60 ;

31. How old is each patient?


select
from

pname,trunc((sysdate - dob)/365.25) as "Age"


patient;

12

PATTERN MATCHING
32. List patients of Dr. Tailor or is it Dr. Taylor
select
from
where

*
patient
gp like 'Dr.Ta_lor%';

note the necessary trailing %


note also the strings within quotes are caSe seNsiTive

33. List the data we have on drugs with the word 'dreams' in their name.
select
from
where

*
drug
dname like '%dreams%';

34. All we know about this patient was they were born after Jan 1st 1970 and they
have the letters 'nog' somewhere in their name;
select
from
where
and

*
patient
pname like '%nog%'
dob > '1-jan-1970' ;

35. I'm not interested in the drugs that have names starting with 'slow', but I want data
on the others.
select
from
where

*
drug
dname not like 'slow%' ;

36. I want information on Dr. William's patients but I'm not sure how the names of
GPs are stored in the database
select
from
where

*
patient
upper(gp) like '%WILLIAMS%';

13

JOINING A TABLE WITH ITSELF


Sometimes it is necessary to join a table with itself. To do this you must create two
different names (aliases) for the table, as shown:
Employee
name
boss
Hubert
Harriet
Heather Hubert
Harriet

salary
20k
15k
18k

37. List of names of employees who earn more than their boss.
Select
from
where
and

e.name
employee e, employee b
e.boss = b.name
e.salary > b.salary ;

Employee (e)
name
boss
Hubert
Harriet
Heather Hubert
Harriet

salary
20k
15k
18k

Employee (b)
name
boss
Hubert
Harriet
Heather Hubert
Harriet

salary
20k
15k
18k

intermediate table
e.name e.boss e.salary
Hubert Harriet 20k
Heather Hubert 15k

b.name
Harriet
Hubert

b.boss
Harriet

b.salary
18k
20k

Result:
Name
Hubert
FURTHER SQL EXAMPLES:
38.

Give names and patient nos of 'married' couples.


select
from
where
and
and

pa.pname as "Mr", pa.pno,


pb.pname as "Mrs", pb.pno
patient pa, patient pb
pa.pname = pb.pname
pa.title = 'mr'
pb.title = 'mrs' ;
14

39.

Get patient names if given d7.


(Alternative to using IN)
select
from
where

40.

pname
patient p
exists
(select
from
where
and

*
dose d
p.pno = d.pno
d.dno = 'd7') ;

What about patients who have not been given d7 ?


(Alternative to using NOT IN)
select
from
where

41.

pname
patient p
not exists
(select
from
where
and

*
dose d
p.pno = d.pno
d.dno = 'd7) ;

Which patients have been given all the drugs?


select pname
from
patient p
where not exists
(select *
from
drug dr
where not exists
(select *
from
dose ds
where ds.pno = p.pno
and
ds.dno = dr.dno) ;

42.

List wanted of patients together with the drugs they have been given (include
those who have not been given any drugs).
select
from

pno,dno
dose

union
select
from
where

pno,'none'
patient p
not exists
(select
from
where

*
dose d
p.pno=pno) ;

(NB You can achieve the same thing with an OUTER JOIN SQLPlus has a simple
version of an outer join)

15

Using null
- Say incomplete details of a drug are put in:
insert into drug(dno,cost)
values ('d10',2.99);

43

- Then if we want to know which drugs have not had a name entered for them:
select dno
from drug
where dname is null;

44

- Or if only want data on drugs which have names entered:


select
from
where

dno
drug
dname is not null;

CALCULATIONS
45

What is the total cost of each drug used?


select
from
where
group

46

dr.dno, sum (qty * cost) as "total cost"


drug dr, dose ds
dr.dno = ds.dno
by dr.dno;

For each drug how much is the maximum quantity given above the average
quantity?
select
from
group

dno, max(qty) avg(qty) as "above mean"


dose
by dno;

16

DATA INDEPENDENCE
LOGICAL DATA INDEPENDENCE
- a change to the logical structure of the data should result in no need to change the
logic of existing uses of the data.
PHYSICAL DATA INDEPENDENCE
- a change to the mechanisms for accessing and storing data should result in no need
to change the logic of existing uses of the data.
VIEWS
A view is a virtual table
ie a table that does not exist in its own right but looks to the user as if it did.
A view is a way to let each user see the parts of the database that they need, in the
format that is most easily understood by them.

1.

Joan is only ever interested in drugs costing more that 1 per unit.

a)

Create a suitable view:


create view deardrugs (dno,unit,doi,cost)
as
select
dno,unit,doi,cost
from
drug
where
cost > 1.00 ;

b)

Use the view as if it were a table


select
from
where

*
deardrugs
doi >'1-Jan-1989';

What the DBMS does behind the scenes:


=

select
from
where
and

dno,unit,doi,cost
drug
doi >'1-Jan-1989'
cost > 1.00;)

Output
dno
d2
d6

unit
mg
gm

doi
12-Mar-91
5-Aug-89

cost
5.00
1.66

17

2.

Jane is only interested in prescriptions of 5 or more units and wants to use her
own names for the columns and wants them in a different order.
create view bigdose(drug,qty,patient,dategiven)
as
select
dno,qty,pno,dosedate
from
dose
where
qty >=5;

3.

Jim also is only interested in what is in Jane's view but doesn't need patient
data and also wants to use his own terms
create view newbigdose(drugno,dose,date)
as
select
drug,qty,dategiven
from
bigdose
where
dategiven > '1-Jan-1991' ;

4.

Jack wants all the data in the dose table but frequently needs the patients'
names as well
create view patdose(pno,pname,dno,dosedate,qty)
as
select
p.pno,pname,dno,dosedate,qty
from
patient p,dose d
where
p.pno = d.pno;
note in this case, as the attributes in the view have the same
names as those in the defining query, you could omit the
attribute names shown in italics.

5.

Jo doesn't deal in codes and often wants the names of patients, which drugs
they have been given and how much.
create view patdrug(patname,drug,qty)
as
select
pname, dname,qty
from
patient p,drug dr,dose ds
where
p.pno = ds.pno
and
ds.dno = dr.dno;

patname
dury
..
minogue
minogue
minogue

drug
sweet dreams
.
sweet dreams
split
slow down

qty
6
.
3
6
2

18

SQL UPDATING VIEWS


Most Relational DBMS do not allow modification of views if:
-

view has more than one base table

any columns in the view are derived from an expression or aggregate (set)
function

Inserts are not allowed if


-

the insert is in conflict with the where condition of the view and the WITH
CHECK OPTION is specified

there is a column in the underlying base table defined as NOT NULL which is not
in the view

Updates are not allowed if


-

attempting to update a column in conflict with the view's qualification and the
WITH CHECK OPTION is specified

Example 1)
create view drugcost(dno,cost)
as
select dno, cost from drug;

dno
cost
d1
0.15
d2
5.00
d5
1.89
d7
0.90
d6
1.66
OK to modify this view because
a)
it is drawn from only one base table
b)
none of the missing columns is NOT NULL
Example 2)
create view agecost(doi,cost)
as
select doi, cost from drug:

doi
20-Apr-87
12-May-91
10-Jul-88
4-Sep-90
5-Aug-89

cost
0.15
5.00
1.89
0.90
1.66

19

Here we can
- update e.g. update agecost set cost = 1.15 where cost = 0.15;
- delete e.g. delete from agecost where cost = 5.00;
- but we CANNOT insert ( because the key, which is not included in the view is
NOT NULL)
Example 3)
There are potential problems if the view has a restriction on the values in a column.
Updating that value might mean the whole row disappears from the view
and cannot be retrieved. Putting 'with check option' into the definition of
the view prevents this.
create view deardrugs (dno,unit,doi,cost)
as
select
dno,unit,doi,cost
from
drug
where
cost > 1.00
with
check option;

dno
d2
d5
d6

unit
mg
mg
gm

Then update
set
where

doi
12-Mar-91
10-Jul-88
5-Aug-89

cost
5.00
1.89
1.66

deardrugs
cost = 0.50
dno = 'd2';

will not succeed.

20

Multitable views : in general multitable view such as patdose or patdrug cannot


be updated. You might find some installations of Oracle will make certain updates to
multitable views where it can map back unamiguously to base tables.
ADVANTAGES OF VIEWS
a)

provide some logical independence

eg If the drug table is split in two for performance reasons eg.


drugbasics (dno,dname)
drugdetails (dno,unit,doi,cost)
then

create view drug (dno,dname,unit,doi,cost)


as
select
db.dno,dname,unit,doi,cost
from
drugbasics db,drugdetails dd
where
db.dno = dd.dno;

This view will allow users to see the data as before.


b)

Allow the same data to be seen by different users in different ways

c)

Simplify users' perception and hence make data manipulation easier

eg Get names of drugs given to patient minogue.


i)

without a view:

ii)

with a view:

select
from
where
and
and

select
from
where

d)

dname
patient p,drug dr,dose ds
p.pno = ds.pno
ds.dno = dr.dno
pname= 'minogue' ;

dname
patdrug
patname = 'minogue';

Provide some degree of security

21

SQL PRACTICE (Using the Hatfield Hospital Database)


SECTION A
1.

List all the information in the drug table.

2.

List patient titles and names.

3.

List the quantity and date of drugs given to patient 'p4'

4.

List full details of female patients.

5.

Get patient names of patients who are not patients of Dr.Spock.

6.

What was the date of birth of Mr. Gooch?

7.

List names of drugs which cost less than 1.00 per unit.

8.

List names of drugs that are in 'mg' or 'gm' units together with their date of
introduction.

9.

List patient nos of patients who have between 1 and 3 children.

10.

List patient nos and dates of birth of patients who have between 1 and 3
children and whose GP is Dr.Williams.

SECTION B
11.

Get the names of patients who have been given drug d1.

12.

Drug names of drugs given to patient p1.

13.

Names of any of Dr.Williams' patients who have been given doses of 5 or


more units of a drug, together with drug no and quantity.

14.

List patient nos of patients who have been given drugs in tablet form.

15.

List names of patients who have been given drugs in tablet form.

16.

Who are the GPs who have patients who have been given bliss?

17.

Get patient name, drug name and dosedate for doses given to patients with two
children or less, of drugs costing more than 1.00, in quantities of 5 or less.

SECTION C
18.

How many drugs have we in the database?

19.

What is the average no of children of patients of Dr.Williams?

20.

What is the lowest cost of a tablet?


22

21.

How many prescriptions has patient Mrs Gooch been given?

22.

What is the total amount of the drug 'split' that has been prescribed?

23.

What is the patient no of the patient who has been given the highest dosage of
drug d7 (in one prescription)?

24.

What is the total number of patients that each GP has in the hospital?

25.

List the total dosage of each different drug given to patient p4 starting with the
highest and leaving out any totals less than 5.

SECTION D
26.

Put in a new patient, Mrs. Lee who has 2 children and was born on 23-May50. ( i.e. 1950)

27.

Modify the database to reflect that all of Dr.Spock's patients have been taken
over by Dr.Owen.

28.

Modify the database to reflect that mrs currie has died and we wish to remove
any reference to her.

SECTION E
29.

Get full details of patients whose names start 'gooc'.

30.

List details of doses given in 1995.

31.

Get names of patients who were less than 30 years old at the start of this year.

32.

List names of patients given drugs in tablet form after their 30th birthday.

33.

List details of patients for whom we have incomplete information.

34.

What is the average cost of prescriptions given to Dr.Williams' patients?

23

(NB. 35-37 are more difficult)


35.

List all the drug names together with quantity prescribed to patients of all GPs
other than Dr. Spock.

36.

List patient names of patients who have the same GP as mr. mansell.

37.

List names of drugs introduced after the drug 'slow down' was.

SECTION F
Set up views which will look like the following to the users:
38.

patdose (PatientName, DrugNo, DateGiven, Qty)

39.

drugdose (DrugName, DateGiven, Qty, DoseCost)


--only prescriptions since 1-jan-1995 should be included

40.

drugsummary (DrugName, TotalQty, TotalCost)


-- NB use the view in 39 above to construct this view

Use these views in the following queries:


41.

Get the names of patients who have been given drug d1 (compare with q11)

42.

What was the date and the cost of the most expensive prescription for the drug
'split' since the beginning of 1995?

43.

Get the drug names, total qty and total cost for drugs in tablet form.

44.

Get the drug name of the drug that has cumulatively been the most costly drug
prescribed since the start of 1995.

45.

Which (if any) of the above views can be used for inserting, updating and
deleting?

24

SQL PRACTICE ANSWERS (ORACLE)


SECTION A
1.

select *
from drug;

2.

select title, pname


from patient;

3.

select qty,dosedate
from dose
where pno = 'p4';

4.

select *
from patient
where title in ('mrs','miss','ms');

5.

select pname
from patient
where GP != 'Dr.Spock';

6.

select dob
from patient
where pname = 'gooch'
and title = 'mr';

7.

select dname
from drug
where cost < 1.00;

8.

select dname,doi
from drug
where unit = 'mg'
or unit = 'gm'

9.

select pno
from patient
where children between 1 and 3;

or

select pno
from patient
where children >= 1
and children <= 3;

10.

select pno, dob


from patient
where children between 1 and 3
and gp = 'Dr.Williams';

or

select pno, dob


from patient
where children >= 1
and children <= 3
and gp = 'Dr.Williams';

25

SECTION B
11.

select pname
from patient p.dose d
where p.pno = d.pno
and dno = 'd1' ;

or

select pname
from patient
where pno in
(select pno
from dose
where dno = 'd1');

12.

select dname
from drug dr,dose d
where dr.dno = d.dno
and pno = 'p1' ;

or

select dname
from drug
where dno in
(select dno
from dose
where pno = 'p1');

13.

select pname,dno,qty
from patient p,dose d
where p.pno = d.pno
and gp = 'Dr.Williams'
and qty >= 5 ;

14.

select distinct pno


from dose d,drug dr
where d.dno = dr.dno
and unit = 'tab';

or

select distinct pno


from dose
where dno in
(select dno
from drug
where unit = 'tab');

15.

select distinct pname


from patient p, dose d,drug dr
where p.pno = d.pno
and d.dno = dr.dno
and unit = 'tab' ;

or

select distinct pname


from patient
where pno in
(select pno
from dose
where dno in
(select dno
from drug
where unit='tab'));

16.

select distinct gp
from patient p,dose d,drug dr
where p.pno = d.pno
and d.dno = dr.dno
and dname = 'bliss';

or

select distinct gp
from patient
where pno in
(select pno
from dose
where dno in
(select dno
from drug
where dname='bliss'));

26

17.

select pname,dname,dosedate
from patient .dose d,drug dr
where p.pno = d.pno
and d.dno = dr.dno
and cost > 1.00
and qty <= 5
and children <= 2;

SECTION C
18.

select count(*) as "No of drugs"


from drug;

19.

select avg(children) as "avg kids of Dr.Ws pats"


from patient
where gp = 'Dr.Williams';

20.

select min(cost) as "lowest tab cost"


from drug
where unit = 'tab';

21.

select count(*) as "No of presc for Mrs Gooch"


from patient p,dose d
where p.pno = d.pno
and pname = 'gooch'
and title = 'mrs';

22.

select sum(qty) as "split total"


from dose d,drug dr
where d.dno = dr.dno
and dname = 'split';

23.

select pno as "highest d7"


from dose
where dno = 'd7'
and qty in
(select max(qty)
from dose
where dno = 'd7);

24.

select gp,count(*) as "total patients"


from patient
group by gp;

25.

select dno,sum(qty) as "p4 dosage"


from dose
where pno = 'p4'
group by dno
having sum(qty) >= 5
order by sum(qty) desc;
27

SECTION D
26.

Insert into patient(pno,pname,title,dob,children)


values ('p10,'lee','mrs','23-May-1950',2) ;

27.

Update patient
set gp = 'Dr.Owen'
where gp = Dr.Spock' ;

28.

we have to remove rows from both the dose table and the patient table. One
way is to use a transaction to make sure that both these actions happen,
another way is to have declared a referential contraint with cascade delete
when the tables were created. Here we assume no cascade delete is in place:
set autocommit off;
Delete
from dose
where pno in
(select pno
from patient
where pname = 'currie'
and title = 'mrs') ;
Delete
from patient
where pname = 'currie'
and title = 'mrs' ;
Commit;
Set autocommit on;

SECTION E
29.

select *
from patient
where pname like 'gooc%' ;

30.

select *
from dose
where dosedate >= '1-jan-95'
and dosedate <= '31-dec-1995' ;

31.

select pname
from patient
where trunc((To_Date('1-jan-1995') dob)/365.25)<30;

32.

select pname
from drug dr, dose d, patient p
where dr.dno = d.dno
28

and d.pno = p.pno


and dr.unit = 'tab'
and trunc((dosedate dob)/365.25) >=30 ;
33.

select *
from patient
where title is null or dob is null or children is null
or gp is null;

34.

select avg(qty * cost) "avg william's cost"


from drug dr, dose d, patient p
where dr.dno = d.dno
and d.pno = p.pno
and gp = 'Dr.Williams' ;

35.

select dname,sum(qty)
from drug dr, dose d, patient p
where dr.dno = d.dno
and d.pno = p.pno
and gp != 'Dr.Spock'
group by dname
UNION
select dname, 0
from drug dr
where dno not in
(select dno
from dose d, patient p
where p.pno = d.pno
and gp!= 'Dr.Spock') ;

36.

select pname
from patient
where gp in
(select gp
from patient
where pname = 'mansell' and title = 'mr');

37.

select dname
from drug
where doi>
(select doi
from drug
where dname = 'slow down');

SECTION F
38.

create view patientdose (PatientName,DrugNo,DateGiven,Qty)


as
select pname,dno,dosedate,qty
from dose d, patient p
where p.pno = d.pno ;
29

39.

create view drugdose (DrugName,DateGiven,Qty,DoseCost)


as
select dname,dosedate,qty,(qty*cost)
from drug dr, dose d
where dr.dno = d.dno
and dosedate > '1-jan-95';

40.

create view drugsummary (Drugname,TotalQty,TotalCost)


as
select drugname,sum(qty),sum(dosecost)
from drugdose
group by drugname ;

41.

select patname
from patientdose
where drugno = 'd1'

42.

select dategiven,dosecost
from drugdose
where drugname = 'split'
and dosecost in
(select max(dosecost)
from drugdose
where drugname = 'split')

43.

select drugname,totalqty,totalcost
from drugsummary ds,drug dr
where ds.drugname = dr.dname
and unit = 'tab';

44.

select drugname
from drugdose
group by drugname
having sum(dosecost) in
(select max(sum(dosecost))
from drugdose
group by drugname);

30

RETRIEVAL WITH SQL REFERENCE


SUBSELECT STATEMENT:
select [distinct] expression [as result-columnname]
{,expression [as result-columnname]}
from tablename {,tablename}
[where predicate]
[group by columname {,columnname}
[having predicate]]
FULL SELECT STATEMENT:
Subselect
{union
Subselect}
[order by columnname [asc|desc]
{,columnname [asc|desc]}];
An expression is a column name, a calculation, a set function or a literal.
A predicate is
[not] search condition {and|or [not] search condition}
A search condition is one of the following:
expression comparison-operator expression
expression comparison-operator any|all (subquery)
expression is [not] null
expression [not] like 'pattern'
pattern may contain-and/or % as wild card characters
Expression [not] between expression and expression
Expression [not] in [subquery]
Expression [not] in (expression {,expression})
exists (subquery)
Comparison operators are:
Equal
=
not equal
!=
less than
<
greater than >
less than or equal
<=
greater than or equal >=
Set functions are: function ([distinct] expression)
where the functions are:
count
sum
avg
max
min
31

OTHER USEFUL COMMANDS:


CREATE VIEW
create view viewname [columnname{,columnname}}
as select .
DROP VIEW
drop view viewname {,viewname}
TO GET INFORMATION ON THE STRUCTURE OF A TABLE or VIEW;
describe tablename|viewname
TO GET INFORMATION ON WHAT TABLES YOU HAVE IN THE DATABASE
select * from tabs

Key:
{.} means optionally a further 1 or more
[.] means optional once
A|B means A or B

32

Das könnte Ihnen auch gefallen