Sie sind auf Seite 1von 4

Mock Test (RDBMS)

QUERIES
1)
SQL> select a.authorid,a.authorname from author a inner join manuscripts m on a.
authorid=m.authorid group by a.authorid,a.authorname having count(a.authorid) in
(select min(authorid_count) from (select count(authorid) as authorid_count from
manuscripts group by authorid));
AUTH
---A105
A101
A103

AUTHORNAME
-------------------Alan Knuth
Richard Stale
Ralph Moore

2)
SQL> select a.authorname,m.noofcopies from author a inner join manuscripts m on
a.authorid=m.authorid where m.noofcopies in (select max(noofcopies) from manuscr
ipts);
AUTHORNAME
NOOFCOPIES
-------------------- ---------Richard Stale
500
Barbara Morgan
500
ELSE
SQL> select authorid,sum(noofcopies) from manuscripts group by authorid having s
um(noofcopies)=(select max(sum(noofcopies)) from manuscripts group by authorid);
AUTH SUM(NOOFCOPIES)
---- --------------A102
835

3)
SQL> select authorid,authorname from author where authorid in ( select authorid
from manuscripts group by authorid having count(distinct genre)=(select count(di
stinct genre) from publicationtypes));
AUTH AUTHORNAME
---- -------------------A102 Barbara Morgan

4)
SQL> select a.authorid,a.authorname,m.bookname,m.genre from author a inner join
manuscripts m on a.authorid=m.authorid where m.amount in (select max(amount) fro
m manuscripts);
AUTH AUTHORNAME
BOOKNAME
GENRE
---- -------------------- -------------------- -------------------A103 Ralph Moore
Mind Games
Fiction

5)
SQL> select m.bookid,m.bookname from manuscripts m inner join publicationtypes p
on m.genre=p.genre where costpercopy in (select max(costpercopy) from publicati
ontypes);
BOOK
---B101
B106

BOOKNAME
-------------------C Programming
Clinical Science

OR
SQL> select bookid,bookname from manuscripts where genre in (select genre from p
ublicationtypes where costpercopy in (select max(costpercopy) from publicationty
pes));
BOOK
---B101
B106

BOOKNAME
-------------------C Programming
Clinical Science

PL/SQL SECTION
create or replace procedure sp_Validation(
p_authorid author.authorid%type,
p_genre publicationtypes.genre%type,
p_bookname manuscripts.bookname%type,
p_noofcopies manuscripts.noofcopies%type,
p_outvar out pls_integer)
is
v_count number;
v_amountpayable author.amountpayable%type;
begin
select count(*) into v_count from author where authorid=p_authorid;
if v_count=0 then
p_outvar := -1;
return;
end if;
select count(*) into v_count from publicationtypes where genre=p_genre;
if v_count=0 then
p_outvar := -2;
return;
end if;
if p_bookname is null then
p_outvar := -3;
return;
end if;
if p_noofcopies <= 0 then
p_outvar := -4;
return;
end if;
select amountpayable into v_amountpayable from author where authorid=p_authorid;
if v_amountpayable!=0 and p_genre='Journal' then
p_outvar := -5;
elsif v_amountpayable>10000 and p_genre='Fiction' then

p_outvar := -6;
elsif v_amountpayable>15000 and p_genre='Educational' then
p_outvar := -7;
else
p_outvar := 0;
end if;
end sp_Validation;
/
Procedure created.
SQL> create or replace function sf_CalculateAmount(
2 p_genre publicationtypes.genre%type,
3 p_noofcopies manuscripts.noofcopies%type)
4 return number
5 is
6 v_costpercopy publicationtypes.costpercopy%type;
7 v_amount number;
8 begin
9 select costpercopy into v_costpercopy from publicationtypes where genre=p_g
enre;
10 v_amount := p_noofcopies * v_costpercopy;
11 return v_amount;
12 end sf_CalculateAmount;
13 /
Function created.
SQL> create or replace procedure sp_Insert_Update(
p_authorid author.authorid%type,
p_bookname manuscripts.bookname%type,
p_genre publicationtypes.genre%type,
p_noofcopies manuscripts.noofcopies%type,
p_outvar out pls_integer)
is
v_count number;
v_bookid manuscripts.bookid%type;
v_lastbookid manuscripts.bookid%type;
v_amount manuscripts.amount%type;
begin
select count(*) into v_count from manuscripts;
if v_count=0 then
v_bookid := 'B100';
else
select max(bookid) into v_lastbookid from manuscripts;
v_bookid := ('B'||(substr(v_lastbookid,2)+1));
end if;
v_amount := sf_CalculateAmount(p_genre,p_noofcopies);
insert into manuscripts values(v_bookid,p_authorid,p_bookname,p_genre,p_noofco
pies,v_amount);
update author set amountpayable=amountpayable+v_amount where authorid=p_author
id;
p_outvar := 0;
end sp_Insert_Update;
/
Procedure created.

Das könnte Ihnen auch gefallen