Professional Documents
Culture Documents
SALESPEOPLE
Snum
number (4)
Sname
City
primary key
varchar2 (10)
Comm
number (3, 2)
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
City
varchar2 (10)
Rating
number (4)
Snum
CNUM CNAME
CITY
RATING
SNUM
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
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
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
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.
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
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
RATING
---------- ----------
2001 hoffman
2007
CITY
----------
london
cisneros
SNUM
----------
100
rome
1001
100
1004
CNUM CNAME
----------
----------
CITY
----------
2002 giovanni
rome
2004 graes
berlin
rome
RATING
---------200
300
100
100
SNUM
---------1003
1002
1002
1004
----------
CITY
----------
2006 celeneros
london
2007 cisneros
rome
RATING
---------100
100
SNUM
---------1002
1004
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
---------- ----------
767.19
2006
4723
2007
1900.1
2003
5160.45
2004
75.75
2008
18.69
2002
1713.23
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
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)
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.
---------
11201.83
06-OCT-90
8944.59
03-OCT-90
6511.98
04-OCT-90
ASSIGNMENT 4
AMT
---------- ---------3001
3006
18.69
ODATE
CNUM
SNUM
---------
----------
----------
03-OCT-90
1098.16 03-OCT-90
2008
2008
1007
1007
RATING
----------
----------
liu
200
clemens
100
SNAME
---------- ---------1002
CITY
COMM
---------- ----------
serres
sanjose
.13
details
of
CITY
london
RATING
SNUM
----------
----------
100
1001
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
---------- ----------
CITY
RATING SNUM
grass
berlin
sanjose
iovanni rome
300
1002
300
1007
200
200
1002
1003
---------- ---------1001
15382.07
union
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
union all
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.
pereira
3003
hoffman
3002
cisneros
3005
liu
3006
pereira
3009
giovanni
3007
graes
3008
celeneros
3010
graes
3011
graes
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
SNAME
COMM
----------
----------
----------
liu
serrer
.13
graes
serrer
.13
celeneros serrer
.13
pereira
.14
rifkin
commision
----------
1001
1187.0232
1002
170.2935
1002
9.8475
1002
1007
670.8585
153.7424
1007 2.6166
1002
171.323
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.
rows updated.
rows updated.
rows deleted.
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
SNUM
-------
300
300
shows
1002
1007
the
number
of
NUMBER_OF_SALES
---------------
London
New York
Barcelona
San Jose
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
CNAME
----------
----------
Peel
Hoffman
Axelrod
Giovanni
Serres
Serres
Liu
Grass
Peel
Clemens
Rifkin
Cisneros
Motika
Pereira
COMM
----------
1001
.12
1002
.13
1004
.11
1007
.15
1003
.1