You are on page 1of 27

ASSIGNMENT 1

Creating sample tables and


inserting values.
A)

SALESPEOPLE
Snum

number (4)

Sname

varchar2 (10) not null

City

primary key

varchar2 (10)

Comm

number (3, 2)

SQL> Create table Salespeople (Snum number (4) primary


key, Sname varchar2 (10) not
null, City varchar2(10),Comm number(3,2));
Table created.

SQL> Insert into Salespeople values


(1001,'Peel','London',.12);
SQL> Insert into Salespeople values
(1002,'Serres','Sanjose',.13);
SQL> Insert into Salespeople values
(1004,'Motika','London',.11);
SQL> Insert into Salespeople values
(1007,'Rifkin','Barcelona',.15);
SQL> Insert into Salespeople values
(1003,'Axelrod','Newyork',.10);
OUTPUT-:
SQL> select * from salespeople;
SNUM SNAME
1001

Peel

CITY
London

COMM
.12

1002

Serres

1004

Motika London

.11

1007

Rifkin

.15

1003

Axelrod Newyork

B)

Sanjose

.13

Barcelona

.1

CUSTOMERS
Cnum

number (4)

Cname

primary key

varchar2 (10) not null

City

varchar2 (10)

Rating

number (4)

Snum

number (4) foreing key

SQL> Create table Customers(Cnum number(4) primary key,


Cname varchar2(10) not null,City varchar2(10),Rating
number(4),Snum number(4) references Salespeople(snum));
SQL> Insert into Customers values
(2001,'Hoffman','London',100,1001);
SQL> Insert into Customers values
(2002,'Giovanni','Rome',200,1003);
SQL> Insert into Customers values
(2003,'Liu','Sanjose',200,1002);
SQL> Insert into Customers values
(2004,'Grass','Berlin',300,1002);
SQL> Insert into Customers values
(2006,'Clemens','London',100,1001);
SQL> Insert into Customers values
(2007,'Pereira','Rome',100,1004);

SQL> select * from Customers;

CNUM CNAME

CITY

RATING

SNUM

---------- ---------- ---------- ---------- ---------2001 Hoffman

London

100

1001

2002 Giovanni

Rome

200

1003

2003 Liu

Sanjose

200

1002

2004 Grass

Berlin

300

1002

2006 Clemens

London

100

1001

2008 Cisneros

Sanjose

300

1007

2007 Pereira

Rome

100

1004

C)

ORDERS
Onum

number (4)

primary

key
Amt
Odate
Cnum
Snum

number (7,2)

not null

date
number (4)

foreing key

number (4)

foreing

key

SQL> Create table Orders(Onum number(4) primary key,Amt


number(7,2) not null,Odate date,Cnum number(4) references
Customers(Cnum),Snum number(4) references
Salespeople(snum));
SQL> Insert into Orders values (3001,18.69,'3-OCT1990',2008,1007);
SQL> Insert into Orders values (3003,767.19,'3-OCT1990',2001,1001);
SQL> Insert into Orders values(3002,1900.10,'3-OCT1990',2007,1004);
SQL> Insert into Orders values (3005,5160.45,'3-OCT1990',2003,1002);
SQL> Insert into Orders values (3006,1098.16,'3-OCT1990',2008,1007);
SQL> Insert into Orders values (3009,1713.23,'4-OCT1990',2002,1003);
SQL> Insert into Orders values (3007,75.75,'4-OCT1990',2004,1002);
SQL> Insert into Orders values (3008,4723.00,'5-OCT1990',2006,1001);

SQL> Insert into Orders values (3010,1309.95,'6-OCT1990',2004,1002);


SQL> select * from orders;
ONUM

AMT

----------

----------

3001

18.69

3003

767.19

3002

ODATE

CNUM

SNUM

---------

----------

----------

03-OCT-90

2008

1007

03-OCT-90

2001

1001

1900.1

03-OCT-90

2007

1004

3005

5160.45

03-OCT-90

2003

1002

3006

1098.16

03-OCT-90

2008

1007

3009

1713.23

04-OCT-90

2002

1003

3007

75.75

04-OCT-90

2004

1002

3008

4723

05-OCT-90

2006

1001

3010

1309.95

06-OCT-90

2004

1002

ASSIGNMENT 2
1) Write a select command that produces the order number, amount,
and date for all rows in the Orders table.
SQL> select onum,amt,odate from orders
ONUM

AMT

ODATE

-----

----------

---------

3001

18.69

03-OCT-90

3003

767.19

03-OCT-90

3002

1900.1

03-OCT-90

3005

5160.45

03-OCT-90

3006

1098.16

03-OCT-90

3009

1713.23

04-OCT-90

3007

75.75

04-OCT-90

3008

4723.6

05-OCT-90

3010

1309.95

06-OCT-90

3011

9891.86

06-OCT-90

2) Write a query that produces all rows from the Customers table for
which the salespersons number is 1001.
SQL> select * from customers where snum in (select snum
from salespeople where
snum=1001);

CNUM CNAME

CITY

RATING

SNUM

---------- ---------- ---------- ---------- ---------2001 hoffman

london

100

1001

2006 clemens

london

100

1001

3) Write a query that displays the Salespeople table with the columns in
the following order: city, sname, snum, comm.
SQL> select city,sname,snum,comm from salespeople;
CITY

SNAME

---------- ----------

SNUM COMM
---------- ----------

london

peel

1001

.12

sanjose

serrer

1002

.13

london

motika

1004

.11

barcelona rifkin

1007

.14

new york

1003

.1

rifkin

4) Write a select command that produces the rating followed by the


name of each customer in San Jose.
SQL> select cname,rating from customers where
city='sanjose';

CNAME

RATING

----------

----------

liu

200

cisneros

300

5) Write a query that will produce the snum values of all salespeople
(suppress the duplicates) with orders in the Orders table.
SQL> select distinct snum from orders;
SNUM
---------1003
1001
1002
1007
1004

6) Write a query that will give you all orders for more than Rs. 1,000.
select * from orders where amt>1000;

ONUM
-----3002

AMT
----------

ODATE
---------

CNUM

SNUM

---------- ----------

1900.1

03-OCT-90

2007

1004

3005 5160.45

03-OCT-90

2003

1002

3006 1098.16

03-OCT-90

2008

1007

3009 1713.23

04-OCT-90

2002

1003

3008

4723.6

05-OCT-90

2006

1001

3010 1309.95

06-OCT-90

2004

1002

3011 9891.86

06-OCT-90

2004

1001

7) Write a query that will give you the names and cities of all salespeople
in London with a commission above .10.

SQL> select sname,city from salespeople where


city='london' and comm>.10;

SNAME

CITY

----------

----------

peel

london

motika

London

8) Write a query on the Customers table whose output will exclude all
customers with a rating <= 100, unless they are located in Rome.
SQL> select * from customers where rating <=100 or city='rome';
CNUM
----------

CNAME
----------

CITY
----------

RATING

SNUM

----------

----------

2001 hoffman

london

100

1001

2002 giovanni

rome

200

1003

2006 celeneros

london

100

1002

2007 cisneros

rome

100

1004

9) What will be the output from the following query?


Select * from Orders
where (amt < 1000 OR
NOT (odate = 03-OCT-1990
AND cnum > 2003));
SQL> Select * from Orders where (amt<1000 or not
(odate='3-oct-1990' and cnum>2003));

ONUM

AMT

---------- ----------

ODATE
---------

CNUM

SNUM

----------

----------

3001

18.69

03-OCT-90

2008

1007

3003

767.19

03-OCT-90

2001

1001

3005

5160.45

03-OCT-90

2003

1002

3009

1713.23

04-OCT-90

2002

1003

3007

75.75

04-OCT-90

2004

1002

3008

4723.6

05-OCT-90

2006

1001

3010

1309.95

06-OCT-90

2004

1002

3011

9891.86

06-OCT-90

2004

1001

ASSIGNMENT 3
1) Write two different queries that would produce all
orders taken on October 3rd or 4th, 1990.
SQL> select * from orders where odate=(select odate from
orders where odate=03-oct-1990 or odate=04-oct-1990 );
ONUM

AMT

ODATE

CNUM

SNUM

----------

----------

---------

----------

----------

3001

18.69

03-OCT-90

2008

1007

3003

767.19

03-OCT-90

2001

1001

3002

1900.1

03-OCT-90

2007

1004

3005

5160.45

03-OCT-90

2003

1002

3006

1098.16

03-OCT-90

2008

1007

3009

1713.23

04-OCT-90

2002

1003

3007

75.75

04-OCT-90

2004

1002

2) Write a query that selects all of the customers


serviced by Peel or Motika.
SQL> select * from customers where snum in(select snum
from salespeople where sname='peel' or sname='motika');
CNUM CNAME
----------

RATING

---------- ----------

2001 hoffman
2007

CITY

----------

london

cisneros

SNUM
----------

100

rome

1001
100

1004

3) Write a query that will produce all the customers


whose names begin with a letter from A to G.
SQL> select * from customers where
ascii(upper(substr(cname,1,1) )) between 65 a
nd 71;

CNUM CNAME
----------

----------

CITY
----------

2002 giovanni

rome

2004 graes

berlin

2006 celeneros london


2007 cisneros

rome

RATING
---------200
300
100
100

SNUM
---------1003
1002
1002
1004

4) Write a query that selects all customers whose


names begin with the letter C.
SQL> select * from customers where cname like 'c%';
CNUM CNAME
----------

----------

CITY
----------

2006 celeneros

london

2007 cisneros

rome

RATING
---------100
100

SNUM
---------1002
1004

5) Write a query that selects all orders except those


with zeroes or NULLs in the amt field.

SQL> select * from orders where amt is not null;


ONUM

AMT

ODATE

CNUM

SNUM

----------

----------

---------

3001

18.69

03-OCT-90

2008

1007

3003

767.19

03-OCT-90

2001

1001

3002

1900.1

03-OCT-90

2007

1004

3005

5160.45

03-OCT-90

2003

1002

3006

1098.16

03-OCT-90

2008

1007

3009

1713.23

04-OCT-90

2002

1003

3007

75.75

04-OCT-90

2004

1002

3008

4723.6

05-OCT-90

2006

1001

3010

1309.95

06-OCT-90

2004

1002

3011

9891.86

06-OCT-90

2004

1001

---------- ----------

6) Write a query that counts all orders for October 3.

SQL> select count(onum) from orders where odate='3-oct90';


COUNT(ONUM)
----------5

7) Write a query that counts the number of different non-NULL city

values in the Customers table.


SQL> select count (city) from customers where city is not
null;
COUNT (CITY)
----------7

8) Write a query that selects each customers smallest order.

SQL> select cnum,min(amt) from orders group by cnum;

CNUM MIN (AMT)


---------- ---------2001

767.19

2006

4723

2007

1900.1

2003

5160.45

2004

75.75

2008

18.69

2002

1713.23

9) Write a query that selects the first customer, in alphabetical order,


whose name begins with G.
SQL> select min(cname) from customers where cname like 'g
%';

MIN(CNAME)
giovanni

10) Write a query that selects the highest rating in each city.
SQL> select max(city) from customers group by city;
MAX(CITY)
---------london
rome
sanjose

11) Assume each salesperson has a 12% commission. Write a


query on the orders table that will produce the order number, the
salesperson number, and the amount of the salespersons
commission for that order.
SQL> select onum,snum,amt*.12 from orders order by snum;
ONUM

SNUM

AMT*.12

----------

----------

----------

3003

1001

92.0628

3011

1001

3008

1001

566.76

3010

1002

157.194

3005

1002

619.254

3007

1002

3009

1003

205.5876

3002

1004

228.012

1187.0256

9.09

12) Write a query on the Customers table that will find the highest
rating in each city. Put the output in this form:
For the city (city), the highest rating is :
(rating).
SQL> select 'for the city (' ||city|| '), the highest rating is : (' ||
max(rating)||) from customers group by city;
FORTHECITY ('||CITY||'), THEHIGHESTRATING IS: ('||MAX
(RATING) ||)
-------------------------------------------------------------------------------for the city (London), the highest rating is : ( 100)
for the city (rome), the highest rating is : (200)
for the city (sanjose) the highest rating is : (300)
for the city (berlin), the highest rating is : (300)

13) Write a query that lists customers in descending


order of rating. Output the rating field first, followed
by the customers name and number.
SQL> select rating,cname,cnum from customers order by
rating;
RATING CNAME

CNUM

---------- ----------

----------

100 hoffman

2001

100 pereira

2007

100 clemens

2006

200 giovanni

2002

200 liu

2003

300 cisneros
300

2008

grass

2004

14) Write a query that totals the orders for each day
and places the results in descending order.

SQL> select sum (amt),odate from orders group by odate


order by sum(amt) desc;
SUM (AMT) ODATE
----------

---------

11201.83

06-OCT-90

8944.59

03-OCT-90

6511.98

04-OCT-90

ASSIGNMENT 4

1)Write a query that uses a sub query to obtain all


orders for the customer named Cisneros. Assume you
do not know his customer number (cnum).

SQL> select * from orders where cnum in(select cnum from


customers where cname='cisneros');
ONUM

AMT

---------- ---------3001
3006

18.69

ODATE

CNUM

SNUM

---------

----------

----------

03-OCT-90

1098.16 03-OCT-90

2008
2008

1007
1007

2) Write a query that produces the names and ratings


of all customers who have above-average orders.
SQL> select cname,rating from customers where cnum
in(select cnum from orders where amt>(select avg(amt) from
orders));
CNAME

RATING

----------

----------

liu

200

clemens

100

3) Write a query that produces the details of sales


people who get the 2nd highest commission.
SQL> select * from salespeople where comm in(select
max(comm) from salespeople where comm <>(select
max(comm) from salespeople));
SNUM

SNAME

---------- ---------1002

CITY

COMM

---------- ----------

serres

sanjose

.13

4) Write a query that produces the


customers who give the maximum Order.

details

of

SQL> select * from customers where cnum in (select cnum


from orders where amt=(select max(amt) from orders));
CNUM CNAME

CITY

---------- ---------- ---------2006 clemens

london

RATING

SNUM

----------

----------

100

1001

5) Write a query that selects all orders for amounts


greater than any for the customers in London.

SQL> select * from orders where amt>any(select amt from


orders,customers where city='london' and
orders.cnum=customers.cnum) ;
ONUM

AMT

ODATE

CNUM

SNUM

----------

----------

---------

3011

9891.88

06-OCT-90

2006

1001

3005

5160.45

03-OCT-90

2003

1002

3008

4723

04-OCT-90

2006

1001

3002

1900.1

03-OCT-90

2007

1004

3009

1713.23

04-OCT-90

2002

1003

3010

1309.95

06-OCT-90

2004

1002

3006

1098.16

03-OCT-90

2008

1007

---------- ----------

6) Write a query that selects all customers whose


ratings are equal to or greater than ANY of Serres.
SQL> select * from customers where rating >= any(select
rating from customers where snum =(select snum from
salespeople where sname='serres'));
CNUM CNAME

CITY

RATING SNUM

---------- ---------- ---------- ---------- ---------2004

grass

berlin

2008 cisneros sanjose


2003 liu
2002

sanjose
iovanni rome

300

1002

300

1007

200
200

1002
1003

7) Write a query that selects the total amount in


orders for each salesperson for whom this total is
greater than the amount of the largest order in the
table.
SQL> select snum,sum(amt) from orders group by snum
having sum(amt)>(select max(
amt) from orders);
SNUM SUM(AMT)

---------- ---------1001

15382.07

8) Create a union of two queries that shows the


names, cities, and ratings of all customers. Those with
rating of 200 or greater will also have the words High
Rating, while the others will have the words Low
Rating.
SQL> select cname,city,rating,'High Rating' from customers
where rating>200
2

union

3 select cname,city,rating,'Low Rating' from customers


where rating<200;
CNAME

CITY

----------

----------

cisneros

sanjose

clemens

london

grass

berlin

RATING 'HIGHRATING
---------300
100
300

----------High Rating
Low Rating
High Rating

hoffman london

100

Low Rating

pereira

100

Low Rating

rome

9) Write a command that produces the name and


number of each salesperson and each customer with
more than one current order. Put the results in
alphabetical order.
SQL> select sname as "NAME",snum as "NUMBER" from
salespeople where snum in(select snum from orders having
count(snum)>1 group by snum)
2

union all

3 select cname,cnum from customers where cnum


in(select cnum from orders havi
ng count(cnum)>1 group by cnum);
NAME

NUMBER

----------

----------

peel

1001

serres

1002

rifkin

1007

clemens

2006

grass
cisneros

2004
2008

10) Form a union of three queries. Have the first select the snums of all
salespeople in San Jose; the second, the cnums of all customers in San
Jose; and the third the onums of all orders on October 3. Retain
duplicates between the last two queries but eliminate any redundancies
between either of them and the first.
SQL> select snum from salespeople
2 union
3 select cnum from customers where city='sanjose'
4 union all
5 select onum from orders where odate='3-oct-90';
SNUM
---------1001
1002
1003
1004
1007
2003
2008
3001
3003
3002

ASSIGNMENT 5
1) Write a query that lists each order number followed
by the name of the customer who made the order.

SQL> select onum,cname from orders,customers where


orders.cnum=customers.cnum;
ONUM CNAME
---------- ---------3001

pereira

3003

hoffman

3002

cisneros

3005

liu

3006

pereira

3009

giovanni

3007

graes

3008

celeneros

3010

graes

3011

graes

2) Write a query that gives the names of both the


salesperson and the customer for each order along
with the order number.
SQL> select s.sname,c.cname,o.onum from salespeople s,
customers c,orders o wher
e s.snum=o.snum and c.cnum=o.cnum;
SNAME

CNAME

ONUM

----------

----------

----------

peel

hoffman

3003

rifkin

giovanni

3009

serrer

liu

3005

peel

graes

3011

serrer

graes

3010

serrer

graes

3007

peel

celeneros

3008

rifkin

pereira

3006

rifkin

pereira

3001

motika

cisneros

3002

3) Write a query that produces all customers serviced


by salespeople with a commission above 12%. Output
the customers name, the salespersons name, and
the salespersons rate of commission.
SQL> select c.cname,s.sname,s.comm from salespeople
s,customers c where s.snum=c
.snum and comm>.12;
CNAME

SNAME

COMM

----------

----------

----------

liu

serrer

.13

graes

serrer

.13

celeneros serrer

.13

pereira

.14

rifkin

4) Write a query that calculates the amount of the


salespersons commission on each order
by a
customer with a rating above 100.
SQL> select s.snum,o.amt*s.comm"commision" from
salespeople s,customers c,orders
o where o.snum=s.snum and o.cnum=c.cnum and
c.rating>100;
SNUM
----------

commision
----------

1001

1187.0232

1002

170.2935

1002

9.8475

1002
1007

670.8585
153.7424

1007 2.6166
1002

171.323

5) Write a query that produces all pairs of salespeople


who are living in the same city. Exclude combinations

of salespeople with themselves as well as duplicate


rows with the order reversed.
Select a.sname, b.sname
from salespeople a, salespeople b
where a.snum > b.snum and
a.city = b.city;

6)Write a query that produces the names and cities of


all customers with the same rating as Hoffman.
SQL> select c.* from customers s,customers c where
s.cnum=c.cnum and c.rating='1
00';
CNUM

CNAME

CITY

---------- ---------- ----------

RATING

SNUM

---------- ----------

2001

hoffman

london

100

1001

2006

celeneros london

100

1002

2007

cisneros

rome

100

1004

ASSIGNMENT 6
1) Write a command that removes all orders from
customer Clemens from the Orders table.
SQL> delete from orders where cnum =(select cnum from
customers where cname='Clemens');
2 rows deleted.

2) Write a command that increases the rating of all


customers in Rome by 100.
SQL> update customers set rating=rating+100 where
city='Rome';
2

rows updated.

3) Salesperson Serres has left the company. Assign


her customers to Motika.
SQL> update customers set snum=(select snum from
salespeople where sname='Motika
') where snum=(select snum from salespeople where
sname='Serres');
2

rows updated.

4) Assume there is a table called Multicust, with all of


the same column definitions as Salespeople. Write a
command that inserts all salespeople with more than
one customer into this table.
SQL> create table multicust as select * from salespeople;
Table created.

5) Write a command that deletes all customers with


no current orders.

SQL> delete from customers where cnum not in(select


distinct cnum from orders);
0

rows deleted.

6) Write a command that increases by twenty percent


the commissions of all salespeople with total current
orders above Rs. 3,000.
SQL> update salespeople set comm=comm+0.20 where
snum in (select snum from orders group by snum having
sum(amt)>3000);
2 rows updated.

ASSIGNMENT 7
1) Create a view that shows all of the customers who
have the highest ratings.
SQL> CREATE VIEW V_RATING AS SELECT *FROM
CUSTOMERS WHERE RATING IN(SELECT MAX(RATING) FROM
CUSTOMERS);
View created.
SQL> SELECT *FROM V_RATING;
CNUM

CNAME

---------- ---------2004
2008

Grass
Cisneros

CITY
----------

RATING
----------

Berlin
San Jose

2) Create a view that


salespeople in each city.

SNUM
-------

300
300

shows

1002
1007

the

number

of

SQL> CREATE VIEW V_SALES AS


SELECT CITY ,
COUNT(SNUM) AS NUMBER_OF_SALES FROM SALESPEOPLE
GROUP BY CITY;
View created.
SQL> SELECT *FROM V_SALES;
CITY
----------

NUMBER_OF_SALES
---------------

London

New York

Barcelona

San Jose

3) Create a view that shows the average and total


orders for each salesperson after his or her name.
Assume all names are unique.
SQL> CREATE VIEW V_TOT AS SELECT SNAME,SUM(O.AMT)
AS TOTAL,AVG(O.AMT) AS AVRAGE,COUNT(ONUM) AS
NO_OF_ORDER FROM SALESPEOPLE S ,ORDERS O WHERE
S.SNUM=O.SNUM GROUP BY SNAME;
View created.
SQL> SELECT *FROM V_TOT;
SNAME

TOTAL

---------- ---------Peel
Motika
Serres

----------

15382.07 5127.35667
1900.1

1900.1

5546.15 1848.71667

Axelrod 1713.23
Rifkin

AVRAGE NO_OF_ORDER

1116.85

1713.23
558.425

----------3
1
3
1
2

4) Create a view that shows each salesperson with


multiple customers.
SQL> CREATE VIEW V_SC AS SELECT SNAME, CNAME FROM
SALESPEOPLE S, CUSTOMERS C WHERE S.SNUM=C.SNUM;
View created.
SQL> SELECT * FROM V_SC;
SNAME

CNAME

----------

----------

Peel

Hoffman

Axelrod

Giovanni

Serres
Serres

Liu
Grass

Peel

Clemens

Rifkin

Cisneros

Motika

Pereira

5) Create a view of the Salespeople table called Commissions. This view


will include only the snum and comm fields. Through this view, someone
could enter or change commissions, but only to values between .10 and .
20.
SQL> create view commissions as select snum,comm from
salespeople where comm between .10 and .20 with check
option;
View created.

SQL> select * from commissions;


SNUM
----------

COMM
----------

1001

.12

1002

.13

1004

.11

1007

.15

1003

.1

You might also like