In this article, you find the following:
Copy these commands from here and past them into SQL*Plus to run them.
Creating tables and inserting data
Log on to Oracle and type the following commands at SQL prompt in SQL*Plus to create the requried tables. The following
script not only creates the tables, it also inserts some data into the tables.
CREATE TABLE PUBLISHERS
(
PUBID NUMBER(3) CONSTRAINT PUBLISHERS_PK PRIMARY KEY,
PNAME VARCHAR2(30),
EMAIL VARCHAR2(50) CONSTRAINT PUBLISHERS_EMAIL_U UNIQUE,
PHONE VARCHAR2(30)
);
CREATE TABLE SUBJECTS
(
SUBID VARCHAR2(5) CONSTRAINT SUBJECTS_PK PRIMARY KEY,
SNAME VARCHAR2(30)
);
CREATE TABLE AUTHORS
(
AUID NUMBER(5) CONSTRAINT AUTHORS_PK PRIMARY KEY,
ANAME VARCHAR2(30),
EMAIL VARCHAR2(50) CONSTRAINT AUTHORS_EMAIL_U UNIQUE,
PHONE VARCHAR2(30)
);
CREATE TABLE TITLES
(
TITLEID NUMBER(5) CONSTRAINT TITLES_PK PRIMARY KEY,
TITLE VARCHAR2(30),
PUBID NUMBER(3) CONSTRAINT TITLES_PUBID_FK REFERENCES PUBLISHERS(PUBID),
SUBID VARCHAR2(5) CONSTRAINT TITLES_SUBID_FK REFERENCES SUBJECTS(SUBID),
PUBDATE DATE,
COVER CHAR(1) CONSTRAINT TITLES_COVER_CHK CHECK ( COVER IN ('P','H','p','h')),
PRICE NUMBER(4)
);
CREATE TABLE TITLEAUTHORS
(
TITLEID NUMBER(5) CONSTRAINT TITLESAUTHORS_TITLEID_FK REFERENCES TITLES(TITLEID),
AUID NUMBER(5) CONSTRAINT TITLESAUTHORS_AUTHID_FK REFERENCES AUTHORS(AUID),
IMPORTANCE NUMBER(2),
CONSTRAINT TITLESAUTHORS_PK PRIMARY KEY(TITLEID,AUID)
);
INSERT INTO SUBJECTS VALUES ('ORA','ORACLE DATABASE 10g');
INSERT INTO SUBJECTS VALUES ('JAVA','JAVA LANGUAGE');
INSERT INTO SUBJECTS VALUES ('JEE','JAVA ENTEPRISE EDITION');
INSERT INTO SUBJECTS VALUES ('VB','VISUAL BASIC.NET');
INSERT INTO SUBJECTS VALUES ('ASP','ASP.NET');
INSERT INTO PUBLISHERS VALUES (1,'WILLEY','WDT@VSNL.NET','91-11-23260877');
INSERT INTO PUBLISHERS VALUES (2,'WROX','INFO@WROX.COM',NULL);
INSERT INTO PUBLISHERS VALUES (3,'TATA MCGRAW-HILL','FEEDBACK@TATAMCGRAWHILL.COM','91-11-33333322');
INSERT INTO PUBLISHERS VALUES (4,'TECHMEDIA','BOOKS@TECHMEDIA.COM','91-11-33257660');
INSERT INTO AUTHORS VALUES (101, 'HERBERT SCHILD','HERBERT@YAHOO.COM',NULL);
INSERT INTO AUTHORS VALUES (102, 'JAMES GOODWILL','GOODWILL@HOTMAIL.COM',NULL);
INSERT INTO AUTHORS VALUES (103, 'DAVAID HUNTER','HUNTER@HOTMAIL.COM',NULL);
INSERT INTO AUTHORS VALUES (104, 'STEPHEN WALTHER','WALTHER@GMAIL.COM',NULL);
INSERT INTO AUTHORS VALUES (105, 'KEVIN LONEY','LONEY@ORACLE.COM',NULL);
INSERT INTO AUTHORS VALUES (106, 'ED. ROMANS','ROMANS@THESERVERSIDE.COM',NULL);
INSERT INTO TITLES VALUES (1001,'ASP.NET UNLEASHED',4,'ASP','12-APR-02','P',540);
INSERT INTO TITLES VALUES (1002,'ORACLE10G COMP. REF.',3,'ORA','1-MAY-05','P',575);
INSERT INTO TITLES VALUES (1003,'MASTERING EJB',1,'JEE','3-FEB-05','P',475);
INSERT INTO TITLES VALUES (1004,'JAVA COMP. REF',3,'JAVA','3-APR-05','P',499);
INSERT INTO TITLES VALUES (1005,'PRO. VB.NET',2,'VB','15-JUN-05','P',450);
INSERT INTO TITLEAUTHORS VALUES (1001,104,1);
INSERT INTO TITLEAUTHORS VALUES (1002,105,1);
INSERT INTO TITLEAUTHORS VALUES (1003,106,1);
INSERT INTO TITLEAUTHORS VALUES (1004,101,1);
INSERT INTO TITLEAUTHORS VALUES (1005,103,1);
INSERT INTO TITLEAUTHORS VALUES (1005,102,2);
Queries with answers
select count(*) from titles where to_char(sysdate,'yyyy') = to_char(pubdate,'yyyy');
select pubid, max(price)
from title
where subid = 'ora'
group by pubid;
select subid, max(price)
from titles
group by subid;
select subid, count(*)
from titles
where price > 500
group by subid;
select subid,pubid, avg(price)
from titles
group by subid,pubid;
select to_char(pubdate,'mm'), count(*) nobooks
from titles
where to_char(sysdate,'yyyy') = to_char(pubdate,'yyyy')
group by to_char(pubdate,'mm');
select titleid, count(*)
from titleauthors
group by titleid;
select count(*)
from titleauthors
where importance = 1 and auid = 103;
select pname, count(*)
from publishers p, titles t
where p.pubid = t.pubid
group by pname;
select pubid
from titles
where subid = 'JAVA'
group by pubid
having count(*) > 3;
select pname, count(*)
from publishers p, titles t
where p.pubid = t.pubid
group by pname;
select auid
from titleauthors ta, titles t
where months_between(sysdate,pubdate) <= 24
group by auid
having count(*) > 3;
select subid from titles
group by subid
having avg(price) between 300 and 400;
select subid,max(price), min(price)
from titles
group by subid;
select aname, max(price)
from authors a, titleauthors ta, titles t
where t.titleid = ta.titleid and a.auid = ta.auid
group by aname;
select aname from authors
where auid in (select auid from titleauthors where titleid in
(select titleid from titles where price < 500) );
select * from authors
where auid in (select auid from titleauthors where titleid in
(select titleid from titles where subid = 'ora') );
select * from subjects
where subid in
( select subid
from titles
group by subid
having min(price) < 300)
and subid in
( select subid
from titles
group by subid
having max(price) > 600);
select * from authors
where auid =
( select auid from titleauthors
where titleid =
(select titleid from titles
where price =
(select max(price) from titles)
)
);
select * from authors
where auid =
( select auid from titleauthors
where titleid =
(select titleid from titles where price > 500)
);
select * from subjects
where subid in
(select subid
from titles
where to_char(pubdate,'yyyy') = to_char(sysdate,'yyyy')
group by subid
having count(*) > 3);
select aname,title
from titles t, authors a, titleauthors ta
where t.titleid = ta.titleid and a.auid = ta.auid
and importance = 1;
select auid,count(*)
from titles
where pubid =
(select pubid from publishers where pname = 'WROX');
select to_char(pubdate,'yyyy') , count(*)
from titles
where titleid in
(select titleid
from titleauthors
group by titleid
having count(*) > 1)
group by to_chat(pubdate,'yyyy');
select * from subjects
where subid in
( select subid
from titles
group by subid
having max(price) - min(price) > 200);
update titles set price =
( select price from titles
where pubdate = ( select max(pubdate) from titles )
where titleid = 1003;
select * from authors
where auid in
( select auid
from titleauthors ta, titles t
where ta.titleid = t.titleid and to_chat(pubdate,'yyyy') = to_char(sysdate,'yyyy') - 1
)
and auid not in
( select auid
from titleauthors ta, titles t
where ta.titleid = t.titleid and to_chat(pubdate,'yyyy') = to_char(sysdate,'yyyy')
);
select * from publishers
where pubid in
( select pubid
from titles
where subid = 'ORA');
)
and pubid not in
( select pubid
from titles
where subid = 'JAVA')
);
select to_char(pubdate,'yyyy') , max(price)
from titles
where titleid in
(select titleid
from titleauthors
group by titleid
having count(*) > 1)
group by to_chat(pubdate,'yyyy');
update titles t set pubdate =
( select min(pubdate)
from titles where subid = t.subid
and to_chat(pubdate,'yyyy') = to_chat(t.pubdate,'yyyy')
)
where titleid = 1003;
Queries with answers for 17-Feb-2006 Batch
select * from emp where ename like '%n' and sal<5000
select ename,sal,job from emp where ename like '%a%a%';
select ename,sal*comm comm from emp where comm is not null;
select * from emp where hire_date>'01-Jan-2000';
select * from emp where sal>5000 and job='clerk'
update emp set sal = sal * 1.1 where deptno = 10;
update emp set hiredate = '05-mar-2001' where empno = 7369;
select ename, round(sal,-2) salary, round(salary * 0.25) hra from emp;
select * from emp where months_between(sysdate,hiredate) <= 6;
select ename,hiredate, last_day(hiredate) + 1 salarydate from emp;
select ename, next_day(hiredate,'sat') , sal from emp where months_between(sysdate,hiredate) > 36;
select ename from emp where length(ename)>=10 and upper(ename) like '%S%I%'
select empno, instr(ename,'i') from emp where ename like '%i%';
select lpad(ltrim(empno,'0'),5,'*') from emp;
select substr(ename, length(ename)-2) from emp;
select ename, deptno || ',' || job from emp;
select ename, to_char(hiredate,'yyyy') from emp;
select ename, to_char(hiredate,'hh24, day, month') from emp;
update emp set hiredate=to_date(to_char(hiredate,'dd-mm-yy')||'10:30','dd-mm-yyhh24:MI') where empno =200;
select last_name from employees where to_char(sysdate,'yyyy')=to_char(hire_date,'yyyy')
alter table titles modify ( price number(5) constraint titles_price_chk check ( price >= 0 ) );
alter table titles drop constraint titles_cover_chk;
alter table titles modify (cover char(1) constraint titles_cover_chk check( cover in ('s','h','p','S','H','P')) );
alter table titles modify ( title varchar(30) constraint titles_title_nn not null);
select auid from titleauthors
group by auid
having count(*) > 3;
select auid from titleauthors where importance=1 group by auid having count(*)>1.
select subid,to_char(pubdate,'yyyy'), count(*)
from titles
group by subid, to_char(pubdate,'yyyy');
select pubid,subid,max(price)
from titles
group by pubid,subid;
select subid,max(pubdate) from titles group by subid.
select to_char(pubdate,'yyyy')
from titles
where subid ='ora'
group by to_char(pubdate,'yyyy')
having count(*) > 2;
select pubid
from titles
group by pubid,subid
having count(*) > 2;
select to_char(pubdate,'month-yyyy')
from titles
where cover = 'h'
group by to_char(pubdate,'month-yyyy')
having count(*) > 2;
select title,sname
from titles t, subjects s
where t.subid = s.subid and pubid = 1;
select title,to_char(pubdate,'yyyy'),pname
from titles t, publishers p
where t.pubid = p.pubid and months_between(sysdate,pubdate) <= 36;
select auid, to_char(pubdate,'yyyy'), count(*)
from titles t, titleauthors ta
where t.titleid = ta.titleid
group by auid,to_char(pubdate,'yyyy');
select pname,title from titles t, publishers p
where t.pubid = p.pubid and titleid in
( select titleid from titleauthors where auid = 103);
select sname,avg(price)
from subjects s, titles t
where s.subid = t.subid
group by sname;
select max(price) - min(price) from titles;
select title, aname
from titles t, authors a, titleauthors ta
where t.titleid = ta.titleid and a.auid = ta.auid and importance = 1;
select aname
from authors
where auid in ( select auid from titleauthors where titleid in
( select titleid from titles where to_char(sysdate,'yyyy') = to_char(pubdate,'yyyy') )
);
select title from titles
where pubid in
( select pubid from publishers where pname like 'W%');
select pname
from publishers
where pubid in
( select pubid from titles where cover = 'h');
select sname from subjects where subid in(select subid from titles group by subid having count(*)>3);
select title
from titles
where titleid in
( select titleid from titleauthors
where auid in
( select auid
from titleauthors
group by auid
having count(*) > 1)
);
select * from publishers
where pubid in
(select pubid from titles t,titleauthors ta where t.titleid=ta.titleid group by pubid having count(distinct auid)>3)
select aname
from authors
where auid in
( select auid from tilesauthors
where titleid in
(select titleid from titles where price > 500)
);
update titles set price = ( select avg(price) from titles) where titleid = 1003;
insert into titleauthors values ( (select max(titleid) from titles),
(select auid from authors where aname = 'Stevens'),1);
delete from authors
where auid not in ( select auid from titleauthors);
Queries with answers for 21-Mar-2006 Batch
select employee_id,job_id,salary from employees
where salary is null;
select job_id,employee_id from employees where job_id like '%S%S%';
select * from employees order by job_id,salary desc;
update employees set salary=10000 where employee_id=205;
insert into departments values(300,'cutsomer-support',null,null);
select employee_id,hire_date,trunc(sysdate-hire_date)/365 Experience from employees;
select employee_id,first_name,hire_date from employees where to_char(hire_date,'mon')='mar';
select first_name,hire_date,last_day(hire_date) FSPAIDON from employees;
select employee_id,first_name,last_name from employees
where length(first_name)>length(last_name);
select first_name,trunc(sysdate-hire_date)Experience from employees
where upper(first_name) like '%I%' and upper(first_name) like '%T%';
update employees set hire_date=to_date('5-JAN-06 10:30','DD-MON-YY HH24:MI')
where employee_id=205;
select title,aname from titles t, authors a,titleauthors ta where t.titleid=ta.titleid and a.auid=ta.auid;
select title,sname from titles t,subjects s
where t.subid=s.subid and to_char(t.pubdate,'yyyy')=to_char(sysdate,'yyyy');
select aname,count(title) from titles t,authors a,
titleauthors ta where ta.auid=a.auid and
ta.titleid=t.titleid and importance=1 group by(aname);
select title from titles t,titleauthors ta,authors a
where t.titleid=ta.titleid and ta.auid=a.auid(+);
select pname,to_char(pubdate,'yyyy'),count(*)
from titles t,publishers p where t.pubid=p.pubid
group by pname,to_char(pubdate,'yyyy');
select sname,to_char(pubdate,'yyyy'), count(*)
from titles t,subjects s where s.subid=t.subid
group by sname, to_char(pubdate,'yyyy');
select title, max(price) from titles group by (title);
select to_char(pubdate,'month') Month,max(price) HighPrice from titles
group by to_char(pubdate,'month');
select sname,count(*) from subjects s,titles t
where t.subid=s.subid and cover='P'
group by sname having count(*)>3;
select titleid from titleauthors ta,authors a
where ta.auid=a.auid
group by titleid having count(*)>2;
select title from titles t,publishers p
where t.pubid=p.pubid
and pname='wrox' and to_char(pubdate,'yyyy')=2005;
update publishers set email='wroxus@usa.net'
where pname='wrox';
select title,sname,pname,aname LeadAuthor from titles t,subjects s,authors a,publishers p,titleauthors ta
where t.subid=s.subid and t.pubid=p.pubid and t.titleid=ta.titleid and ta.auid=a.auid and importance=1;
select aname from authors a,titleauthors ta,titles t
where t.titleid=ta.titleid and ta.auid=a.auid group by(aname),to_char(pubdate,'yyyy')
having count(*)>1;
select title from titles where pubid in
(select pubid from publishers where pname='wrox');
select aname from authors a,titleauthors ta,titles t,subjects s
where a.auid=ta.auid and ta.titleid=t.titleid and t.subid=s.subid and sname
like '%or%';
select pname from publishers where pubid in(select pubid from titles where to_char(pubdate,'yyyy')=to_char(sysdate,'yyyy'));
select title from titles where pubid in (select pubid from publishers
where pname='wrox');
select sname,count(title) from subjects s,titles t
where t.subid=s.subid and price>500 groupby(sname);
select pubid,count(*) from titles where
titleid in(select titleid from titleauthors group by titleid
having count(*)>1)
group by(pubid);
select * from subjects where subid not in(select subid from titles where
to_char(sysdate,'yyyy')=to_char(pubdate,'yyyy'));
select pname from publishers p,titles t where
t.pubid=p.pubid and to_char(pubdate,'yyyy')=2003 and to_char(pubdate,'yyyy')!=2004;
update titles set price=(select avg(price)from subjects s,titles t
where s.subid=t.subid and sname like '%ora%')
where titleid=1003;
insert into titleauthors values(
(select titleid from titles where title = 'Java Comp. Ref'),
(select auid from authors where aname = 'Herbert Schild'),1);
select pname from publishers p,titles t,titleauthors ta, authors a
where p.pubid=t.pubid and t.titleid=ta.titleid and ta.auid=a.auid and aname='herbert schildt' ;
select aname from authors where auid in
(select auid from titleauthors where importance=1);
select aname from authors where auid in
(select auid from titleauthors where titleid in
(select titleid from titleauthors where auid=
(select auid from authors where aname='HERBERT SCHILD')
)
);
select pname, count(*)
from publishers p, titles t
where p.pubid = t.pubid
group by pname;
select subid from titles where titleid in
(select titleid from titleauthors group by titleid having count(*)>1)
group by subid having count(*)>1;
select pubid,subid,count(*) from titles group by(pubid,subid) order by pubid,subid;
select *from subjects where subid not in (select subid from titles
where to_char(sysdate,'yyyy')=to_char(pubdate,'yyyy'));
select title,price from titles main
where 1= (select count(*) from titles where price > main.price);