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

2008.Snum number(4) references Salespeople(snum)).2) not null date number (4) foreing key number (4) foreing key SQL> Create table Orders(Onum number(4) primary key.'3-OCT1990'.Odate date.'3-OCT1990'.2004.16.2006.2) not null.2003.5160.'3-OCT1990'.00.69.1004).1007).2007.1002).1003). SQL> Insert into Orders values (3005.Amt number(7.1713. SQL> Insert into Orders values (3001. SQL> Insert into Orders values (3008. SQL> Insert into Orders values (3009.'5-OCT1990'. SQL> Insert into Orders values (3003.1098. SQL> Insert into Orders values(3002. . SQL> Insert into Orders values (3007.75.2008.'4-OCT1990'.23. SQL> Insert into Orders values (3006.1900.1001).2002.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.19.10.'3-OCT1990'.75.4723.45.'3-OCT1990'.18.1007).'4-OCT1990'.2001.1002).767.1001).Cnum number(4) references Customers(Cnum).

23 04-OCT-90 3007 75.16 03-OCT-90 2008 1007 3009 1713.69 3003 767. and date for all rows in the Orders table.45 03-OCT-90 3006 1098.75 04-OCT-90 2004 1002 3008 4723 05-OCT-90 2006 1001 3010 1309.1002). SQL> select * from orders.45 03-OCT-90 2003 1002 3006 1098.1 03-OCT-90 3005 5160.69 03-OCT-90 3003 767.1 03-OCT-90 2007 1004 3005 5160.95 06-OCT-90 2004 1002 ASSIGNMENT 2 1) Write a select command that produces the order number. SQL> select onum. ONUM AMT ---------- ---------- 3001 18.23 04-OCT-90 2002 1003 3007 75.19 03-OCT-90 3002 1900.SQL> Insert into Orders values (3010.amt.6 05-OCT-90 .16 03-OCT-90 3009 1713.1309.2004.95.19 3002 ODATE CNUM SNUM --------- ---------- ---------- 03-OCT-90 2008 1007 03-OCT-90 2001 1001 1900. amount.odate from orders ONUM AMT ODATE ----- ---------- --------- 3001 18.75 04-OCT-90 3008 4723.'6-OCT1990'.

---------- SNUM COMM ---------.comm from salespeople.---------.12 sanjose serrer 1002 .---------.95 06-OCT-90 3011 9891.sname. SQL> select city.---------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. CNUM CNAME CITY RATING SNUM ---------. snum. SQL> select * from customers where snum in (select snum from salespeople where snum=1001).13 london motika 1004 .rating from customers where city='sanjose'.86 06-OCT-90 2) Write a query that produces all rows from the Customers table for which the salesperson’s number is 1001.11 barcelona rifkin 1007 .snum.1 rifkin 4) Write a select command that produces the rating followed by the name of each customer in San Jose.---------.---------- london peel 1001 .3010 1309. comm. CITY SNAME ---------.14 new york 1003 . SQL> select cname. . sname.

. ONUM -----3002 AMT ---------- ODATE --------- CNUM SNUM ---------.45 03-OCT-90 2003 1002 3006 1098.16 03-OCT-90 2008 1007 3009 1713.95 06-OCT-90 2004 1002 3011 9891.1 03-OCT-90 2007 1004 3005 5160. SNUM ---------1003 1001 1002 1007 1004 6) Write a query that will give you all orders for more than Rs.10.---------- 1900. select * from orders where amt>1000.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 . 1.23 04-OCT-90 2002 1003 3008 4723.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.000. SQL> select distinct snum from orders.6 05-OCT-90 2006 1001 3010 1309.

city from salespeople where city='london' and comm>. unless they are located in Rome. SQL> select * from customers where rating <=100 or city='rome'. SNAME CITY ---------- ---------- peel london motika London 8) Write a query on the Customers table whose output will exclude all customers with a rating <= 100.45 03-OCT-90 2003 1002 .10. ONUM AMT ---------.SQL> select sname.---------- ODATE --------- CNUM SNUM ---------- ---------- 3001 18. 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)).19 03-OCT-90 2001 1001 3005 5160.69 03-OCT-90 2008 1007 3003 767. SQL> Select * from Orders where (amt<1000 or not (odate='3-oct-1990' and cnum>2003)).

75 04-OCT-90 2004 1002 . SQL> select * from orders where odate=(select odate from orders where odate=’03-oct-1990’ or odate=’04-oct-1990’ ).23 04-OCT-90 2002 1003 3007 75.19 03-OCT-90 2001 1001 3002 1900.69 03-OCT-90 2008 1007 3003 767. 1990. ONUM AMT ODATE CNUM SNUM ---------- ---------- --------- ---------- ---------- 3001 18.45 03-OCT-90 2003 1002 3006 1098.6 05-OCT-90 2006 1001 3010 1309.86 06-OCT-90 2004 1001 ASSIGNMENT 3 1) Write two different queries that would produce all orders taken on October 3rd or 4th.16 03-OCT-90 2008 1007 3009 1713.75 04-OCT-90 2004 1002 3008 4723.1 03-OCT-90 2007 1004 3005 5160.3009 1713.95 06-OCT-90 2004 1002 3011 9891.23 04-OCT-90 2002 1003 3007 75.

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’. CNUM CNAME ---------- RATING ---------. . 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.1. SQL> select * from customers where ascii(upper(substr(cname.2) Write a query that selects all of the customers serviced by Peel or Motika. SQL> select * from customers where cname like 'c%'. SQL> select * from customers where snum in(select snum from salespeople where sname='peel' or sname='motika').---------- 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’.1) )) between 65 a nd 71.

. SQL> select cnum. COUNT(ONUM) ----------5 7) Write a query that counts the number of different non-NULL city values in the Customers table.16 03-OCT-90 2008 1007 3009 1713.95 06-OCT-90 2004 1002 3011 9891.6 05-OCT-90 2006 1001 3010 1309.min(amt) from orders group by cnum.69 03-OCT-90 2008 1007 3003 767. ONUM AMT ODATE CNUM SNUM ---------- ---------- --------- 3001 18.1 03-OCT-90 2007 1004 3005 5160. COUNT (CITY) ----------7 8) Write a query that selects each customer’s smallest order.75 04-OCT-90 2004 1002 3008 4723.45 03-OCT-90 2003 1002 3006 1098. SQL> select count(onum) from orders where odate='3-oct90'.SQL> select * from orders where amt is not null.---------- 6) Write a query that counts all orders for October 3. SQL> select count (city) from customers where city is not null.86 06-OCT-90 2004 1001 ---------.23 04-OCT-90 2002 1003 3007 75.19 03-OCT-90 2001 1001 3002 1900.

12 from orders order by snum.snum. the salesperson number. SQL> select max(city) from customers group by city.19 2006 4723 2007 1900. MIN(CNAME) giovanni 10) Write a query that selects the highest rating in each city.45 2004 75.12 ---------- ---------- ---------- .23 9) Write a query that selects the first customer.75 2008 18.CNUM MIN (AMT) ---------. Write a query on the orders table that will produce the order number.1 2003 5160.69 2002 1713.---------2001 767. SQL> select min(cname) from customers where cname like 'g %'. in alphabetical order. SQL> select onum. whose name begins with G. MAX(CITY) ---------london rome sanjose 11) Assume each salesperson has a 12% commission. and the amount of the salesperson’s commission for that order. ONUM SNUM AMT*.amt*.

Output the rating field first.254 3007 1002 3009 1003 205. the highest rating is : ( 100) for the city (rome). ‘FORTHECITY ('||CITY||').cnum from customers order by rating.cname. Put the output in this form: For the city (city).5876 3002 1004 228. the highest rating is : (300) 13) Write a query that lists customers in descending order of rating. the highest rating is : (200) for the city (sanjose) the highest rating is : (300) for the city (berlin). SQL> select rating.0628 3011 1001 3008 1001 566.76 3010 1002 157. RATING CNAME CNUM ---------.194 3005 1002 619.---------- ---------- 100 hoffman 2001 100 pereira 2007 100 clemens 2006 200 giovanni 2002 200 liu 2003 .3003 1001 92. THEHIGHESTRATING IS: ('||MAX (RATING) ||’)’ -------------------------------------------------------------------------------for the city (London). the highest rating is : (rating). the highest rating is : (' || max(rating)||’)’ from customers group by city.0256 9. followed by the customer’s name and number. SQL> select 'for the city (' ||city|| ').09 12) Write a query on the Customers table that will find the highest rating in each city.012 1187.

---------3001 3006 18.69 ODATE CNUM SNUM --------- ---------- ---------- 03-OCT-90 1098.16 03-OCT-90 2008 2008 1007 1007 . SQL> select * from orders where cnum in(select cnum from customers where cname='cisneros'). Assume you do not know his customer number (cnum).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. SUM (AMT) ODATE ---------- --------- 11201.83 06-OCT-90 8944.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). ONUM AMT ---------.odate from orders group by odate order by sum(amt) desc.

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. 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 cname.---------1002 CITY COMM ---------.---------- serres sanjose .rating from customers where cnum in(select cnum from orders where amt>(select avg(amt) from orders)).2) Write a query that produces the names and ratings of all customers who have above-average orders. SNUM SNAME ---------.---------. SQL> select * from salespeople where comm in(select max(comm) from salespeople where comm <>(select max(comm) from salespeople)).

customers where city='london' and orders.SQL> select * from orders where amt>any(select amt from orders.---------. CNUM CNAME CITY RATING SNUM ---------.---------.88 06-OCT-90 2006 1001 3005 5160. SQL> select snum.95 06-OCT-90 2004 1002 3006 1098. SQL> select * from customers where rating >= any(select rating from customers where snum =(select snum from salespeople where sname='serres')).1 03-OCT-90 2007 1004 3009 1713.cnum) .---------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. SNUM SUM(AMT) .23 04-OCT-90 2002 1003 3010 1309.sum(amt) from orders group by snum having sum(amt)>(select max( amt) from orders).45 03-OCT-90 2003 1002 3008 4723 04-OCT-90 2006 1001 3002 1900.---------- 6) Write a query that selects all customers whose ratings are equal to or greater than ANY of Serres’.cnum=customers.---------. ONUM AMT ODATE CNUM SNUM ---------- ---------- --------- 3011 9891.16 03-OCT-90 2008 1007 ---------.

'High Rating' from customers where rating>200 2 union 3 select cname.07 8) Create a union of two queries that shows the names. while the others will have the words “Low Rating”.city. SQL> select sname as "NAME".rating.rating. Put the results in alphabetical order. cities.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. NAME NUMBER ---------- ---------- peel 1001 serres 1002 .city.'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. and ratings of all customers.cnum from customers where cnum in(select cnum from orders havi ng count(cnum)>1 group by cnum). Those with rating of 200 or greater will also have the words “High Rating”.---------. SQL> select cname.---------1001 15382.

Retain duplicates between the last two queries but eliminate any redundancies between either of them and the first. . the cnums of all customers in San Jose. 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. Have the first select the snums of all salespeople in San Jose. the second. and the third the onums of all orders on October 3.rifkin 1007 clemens 2006 grass cisneros 2004 2008 10) Form a union of three queries. 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'.

SQL> select onum.snum and c.cname from orders.cnum.c. ONUM CNAME ---------.cnum=customers.onum from salespeople s.orders o wher e s.o. 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 .cnum=o.---------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.cname.snum=o.sname.customers where orders. customers c.cnum. SQL> select s.

snum=c . SNUM ---------- commision ---------- 1001 1187.2935 1002 9.snum=s.cname.sname.14 rifkin 4) Write a query that calculates the amount of the salesperson’s commission on each order by a customer with a rating above 100.snum and comm>. SQL> select c.o.snum.cnum and c.snum and o.8475 1002 1007 670.comm"commision" from salespeople s. CNAME SNAME COMM ---------- ---------- ---------- liu serrer .orders o where o. Output the customer’s name.motika cisneros 3002 3) Write a query that produces all customers serviced by salespeople with a commission above 12%.13 celeneros serrer . Exclude combinations . SQL> select s.8585 153.customers c. and the salesperson’s rate of commission. the salesperson’s name.rating>100.13 pereira .customers c where s.s.amt*s.323 5) Write a query that produces all pairs of salespeople who are living in the same city.s.7424 1007 2.12.13 graes serrer .cnum=c.comm from salespeople s.6166 1002 171.0232 1002 170.

---------- 2001 hoffman london 100 1001 2006 celeneros london 100 1002 2007 cisneros rome 100 1004 .city = b.rating='1 00'.city. salespeople b where a.cnum=c.of salespeople with themselves as well as duplicate rows with the order reversed. CNUM CNAME CITY ---------.cnum and c. SQL> select c. b.snum and a. 6)Write a query that produces the names and cities of all customers with the same rating as Hoffman.sname from salespeople a. Select a.snum > b.customers c where s.sname.---------- RATING SNUM ---------.---------.* from customers s.

. Write a command that inserts all salespeople with more than one customer into this table. 2 rows updated. SQL> delete from orders where cnum =(select cnum from customers where cname='Clemens'). SQL> update customers set rating=rating+100 where city='Rome'.ASSIGNMENT 6 1) Write a command that removes all orders from customer Clemens from the Orders table. SQL> update customers set snum=(select snum from salespeople where sname='Motika ') where snum=(select snum from salespeople where sname='Serres'). with all of the same column definitions as Salespeople. 4) Assume there is a table called Multicust. 2 rows updated. SQL> create table multicust as select * from salespeople. 2) Write a command that increases the rating of all customers in Rome by 100. 3) Salesperson Serres has left the company. 5) Write a command that deletes all customers with no current orders. Assign her customers to Motika. 2 rows deleted. Table created.

View created. SQL> CREATE VIEW V_RATING AS SELECT *FROM CUSTOMERS WHERE RATING IN(SELECT MAX(RATING) FROM CUSTOMERS). COUNT(SNUM) AS NUMBER_OF_SALES FROM SALESPEOPLE GROUP BY CITY.20 where snum in (select snum from orders group by snum having sum(amt)>3000). SQL> update salespeople set comm=comm+0. SQL> SELECT *FROM V_SALES.SQL> delete from customers where cnum not in(select distinct cnum from orders). 0 rows deleted. CNUM CNAME ---------. CITY ---------- NUMBER_OF_SALES --------------- . 6) Write a command that increases by twenty percent the commissions of all salespeople with total current orders above Rs. 2 rows updated.000. 3. SQL> SELECT *FROM V_RATING. ASSIGNMENT 7 1) Create a view that shows all of the customers who have the highest ratings. View created.---------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 .

SNUM=C. SNAME CNAME ---------- ---------- Peel Hoffman Axelrod Giovanni Serres Serres Liu Grass .London 2 New York 1 Barcelona 1 San Jose 1 3) Create a view that shows the average and total orders for each salesperson after his or her name.1 1900.AVG(O.35667 1900.AMT) AS TOTAL. SQL> SELECT * FROM V_SC.07 5127.ORDERS O WHERE S.15 1848. View created. SQL> CREATE VIEW V_TOT AS SELECT SNAME. SNAME TOTAL ---------.SNUM GROUP BY SNAME. SQL> SELECT *FROM V_TOT.---------Peel Motika Serres ---------- 15382. Assume all names are unique. CNAME FROM SALESPEOPLE S.85 1713. View created.AMT) AS AVRAGE.COUNT(ONUM) AS NO_OF_ORDER FROM SALESPEOPLE S . SQL> CREATE VIEW V_SC AS SELECT SNAME.23 Rifkin AVRAGE NO_OF_ORDER 1116.SNUM.425 ----------3 1 3 1 2 4) Create a view that shows each salesperson with multiple customers.23 558.SNUM=O. CUSTOMERS C WHERE S.SUM(O.71667 Axelrod 1713.1 5546.

11 1007 .1 . someone could enter or change commissions. View created. SQL> create view commissions as select snum.10 and .10 and .15 1003 . This view will include only the snum and comm fields.20 with check option. SQL> select * from commissions. but only to values between .12 1002 .Peel Clemens Rifkin Cisneros Motika Pereira 5) Create a view of the Salespeople table called Commissions.comm from salespeople where comm between . 20. SNUM ---------- COMM ---------- 1001 .13 1004 . Through this view.