Sie sind auf Seite 1von 24

1.

CREATE TABLE salespeople( Snum


varchar2(10),City varchar2(10), Comm

number(4), Sname
number(3,2) );

Table created.

2.INSERT INTO salespeople VALUES


(&snum,'&sname','&city',&comm);
Enter value for snum: 1001
Enter value for sname: peel
Enter value for city: London
Enter value for comm: .12
1 row created.

3.
Enter value for snum: 1004
Enter value for sname: Motika
Enter value for city: London
Enter value for comm: .11
1 row created.

4. SELECT * FROM salespeople;


SELECT UNIQUE snum,onum FROM orders

7. Write a query that will give you the names and


cities of all salespeople in London with a
commission above .10
SELECT sname,city FROM salespeople WHERE upper(city)='LONDON' AND
comm>.10;
SNAME

CITY

---------- ---------peel
Motika

London
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
SELECT * FROM customers WHERE (rating<=100 AND UPPER(city)='ROME') OR
rating>100;
CNUM CNAME

CITY

RATING

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

SNUM

----------

2002 Giovanni

Rome

200

1003

2003 Liu

San Jose

200

1002

2004 Grass

Berlin

2008 Cisneros
2007 Pereira

300
San Jose

Rome

1002
300

100

1007

1004

9. Write two different queries that would produce all orders taken on October 3 rd or
4th, 1990.

SELECT * FROM orders WHERE odate IN ('03-OCT-90','04-OCT-90')


ONUM

AMT

ODATE

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

CNUM

SNUM

3001

18.69 03-OCT-90

3003

767.19 03-OCT-90

3002

1900.1

3005

5160.45

3006

1098.16

03-OCT-90

2008

1007

3009

1713.23

04-OCT-90

2002

1003

3007

2008
2001

03-OCT-90
03-OCT-90

75.75 04-OCT-90

2004

1007
1001
2007
2003

1004
1002

1002

7 rows selected.

SELECT * FROM orders WHERE odate =ANY ('03-OCT-90','04-OCT-90','06-OCT-90')


ONUM

AMT ODATE

CNUM

SNUM

---------- ---------- --------- ---------- ---------3001

18.69 03-OCT-90

2008

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

3010

1309.95 06-OCT-90

2004

1002

3011

9891.88 06-OCT-90

2006

1001

2004

1007

1002

9 rows selected.

10. Write a query that selects all of the customers serviced by Peel or Motika.

(Hint: the snum field relates the two tables to one another).
SELECT cname FROM customers WHERE snum IN(SELECT snum FROM salespeople
WHERE sname=ANY('peel','Motika'));
CNAME
---------Hoffman
Clemens
Pereira

14. Write a query that counts all orders for October 3.


SELECT COUNT(odate)

FROM orders

WHERE odate='03-OCT-1990';

COUNT(ODATE)
5

15. Write a query that counts the number of different non-NULL city values in the
Customers table.
SELECT COUNT(city) FROM customers;
COUNT(CITY)
----------7

16. Write a query that selects each customers smallest order.


SELECT MIN(amt),cnum FROM orders GROUP BY cnum;
MIN(AMT)
---------- ----------

CNUM

767.19

2001

1713.23

2002

5160.45

2003

75.75

2004

4723

2006

1900.1

2007

18.69

2008

8 rows selected.

17. Write a query that selects the first customer, in alphabetical order, whose name
begins with G.
SELECT MIN(cname) FROM customers WHERE cname LIKE 'G%';
MIN(CNAME)
---------Giovanni

18. Write a query that selects the highest rating in each city.
SELECT city, MAX(rating) FROM customers GROUP BY city;
CITY

MAX(RATING)

---------- ----------Berlin

300

London

100

Rome

200

San Jose

300

19. Write a query that counts the number of salespeople registering orders for each
day. (If a salesperson has more than one order on a given day, he or she should be
counted only once.).
SELECT odate,COUNT(DISTINCT snum) FROM orders
GROUP BY odate;
ODATE

COUNT(DISTINCTSNUM)

--------- ------------------03-OCT-90

04-OCT-90

05-OCT-90

06-OCT-90

20. 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.
SELECT onum,snum,amt*0.12 "commision FROM orders;
ONUM

SNUM commision

---------- ---------- ---------3001

1007

2.2428

3003

1001

92.0628

3002

1004

228.012

3005

1002

619.254

3006

1007

131.7792

3009

1003

205.5876

3007

1002

9.09

3008

1001

566.76

3010

1002

157.194

3011

1001

1187.0256

3000
ONUM

1006

SNUM

commision

---------- ---------- ---------3013

1006

12 rows selected.

21. 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).
SELECT city "city",MAX(rating) "highest rating is FROM customers
GROUP BY city ;
city

highest rating is

----------

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

Berlin

300

London

100

Rome

200

San Jose

300

22. Write a query that lists customers in descending order of rating. Output the
rating field first, followed by the customers name and number.
SELECT rating,cname,cnum FROM customers ORDER BY rating DESC;
RATING

CNAME

----------

----------

CNUM
----------

300

Grass

2004

300

Cisneros

2008

200

Giovanni

2002

200

Liu

100

Hoffman

2003
2001

100

Clemens

2006

100

Pereira

2007

7 rows selected.

23. Write a query that totals the orders for each day and places the results in
descending order.
SELECT COUNT(onum),odate FROM orders
COUNT(onum) DESC;

GROUP BY odate ORDER BY

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

03-OCT-90

04-OCT-90

06-OCT-90

05-OCT-90

24. Write a query that lists each order number followed by the name of the
customer who made the order.
SELECT onum,cname FROM customers,orders
WHERE orders.cnum=customers.cnum;

ONUM

CNAME

----------

----------

3003

Hoffman

3009

Giovanni

3005

Liu

3007

Grass

3010

Grass

3008

Clemens

3011

Clemens

3002

Pereira

3001

Cisneros

3006

Cisneros

10 rows selected.

25. Write a query that gives the names of both the salesperson and the customer
for each order along with the order number.
SELECT cname,sname,customers.snum "Order No. FROM salespeople,customers
WHERE salespeople.snum=customers.snum;
CNAME

SNAME

----------

Order No.

----------

Hoffman

peel

1001

Clemens

peel

1001

Liu

Serres

Grass

Serres

1002

Giovanni

Axelrod

1003

Pereira

Motika

1004

Cisneros

Rifkin

1007

----------

1002

7 rows selected.

26. 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.
SELECT cname,sname,comm*100 "Rate(%)

FROM customers,salespeople

WHERE salespeople.snum=customers.snum AND comm>.12

CNAME

SNAME

Rate(%)

----------

----------

----------

Liu

Serres

13

Grass

Serres

13

Cisneros

Rifkin

15

27. Write a query that calculates the amount of the salespersons commission on
each order by a customer with a rating above 100.
SELECT sname,amt*comm "Commision
customers,salespeople,orders

FROM

WHERE customers.snum=salespeople.snum
salespeople.snum=orders.snum
AND rating>100 ;
SNAME

Commision

---------- ---------Serres

670.8585

Serres

670.8585

Serres

170.2935

Serres

170.2935

Serres

9.8475

Serres

9.8475

Axelrod

171.323

Rifkin

2.8035

Rifkin

164.724

9 rows selected.

AND

28. 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 s1.sname FROM salespeople s1,salespeople s2
WHERE (s1.city=s2.city) AND (s1.snum!=s2.snum);
SNAME
---------Motika
peel

29. Write a query that produces the names and cities of all customers with the same
rating as Hoffman.
SELECT cname,city FROM customers
WHERE rating=(SELECT rating FROM customers
WHERE LOWER(cname)='hoffman');
CNAME

CITY

---------- ---------Hoffman

London

Clemens

London

Pereira

Rome

30. Write a query that uses a subquery to obtain all orders for the customer named
Cisneros. Assume you do not know his customer number (cnum).
.

SELECT onum FROM orders WHERE cnum=(SELECT cnum FROM customers WHERE
LOWER(cname)='cisneros');
ONUM
---------3001
3006

31. Write a query that produces the names and ratings of all customers who have
above-average orders.
SELECT cname,rating FROM customers WHERE snum IN(SELECT snum FROM orders
WHERE amt>(SELECT AVG(amt) FROM orders);
CNAME

RATING

----------

----------

Hoffman

100

Clemens

100

Liu
Grass

200
300

32. 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
SELECT amt FROM salespeople WHERE snum IN(SELECT snum FROM orders WHERE
amt>ANY(SELECT MAX(amt) FROM orders GROUP BY snum));
SELECT amt FROM orders WHERE snum IN(SELECT snum FROM orders
WHERE amt>ANY(SELECT MAX(amt) FROM orders GROUP BY snum));
AMT
---------767.19
4723

9891.88
5160.45
1309.95
75.75
1713.23
1900.1
18.69
1098.16

33. Write a query that selects all customers whose ratings are equal to or
greater than ANY of Salesman Serres.
SELECT cname FROM customers
WHERE rating >ANY(SELECT rating FROM customers WHERE snum=
(SELECT snum FROM salespeople WHERE LOWER(sname)='serres'));
CNAME
---------Grass
Cisneros

34. Write a query using ANY or ALL that will find all salespeople who have
no customers located in their city.
SELECT sname FROM salespeople
WHERE city != ALL(SELECT city FROM customers);
SNAME
---------Serres
Rifkin

Axelrod

35. Write a query that selects all orders for amounts greater than any for
the customers in London.
SELECT onum FROM orders
WHERE amt>ALL(SELECT amt FROM orders
WHERE cnum=ANY(SELECT cnum FROM customers WHERE LOWER(city)='london'));
no rows selected

36. Write the above query using MIN or MAX.


SELECT onum FROM orders
WHERE amt>ANY(SELECT MIN(amt) FROM orders WHERE cnum=ANY(SELECT cnum
FROM customers WHERE LOWER(city)='london'));
ONUM
---------3002
3005
3006
3009
3008
3010
3011
7 rows selected.
SELECT onum FROM orders WHERE amt>ANY(SELECT MAX(amt) FROM orders
WHERE cnum=ANY(SELECT cnum FROM customers WHERE LOWER(city)='london'));

no rows selected

37. 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.
SELECT cname,city,Rating,'Low Salary "Ramarks FROM customers WHERE
rating<200 UNION
SELECT cname,city,rating,'HighSalary FROM customers WHERE rating>=200
CNAME
----------

CITY

RATING

Ramarks

----------

Cisneros San Jose

---------- ---------300

HighSalary

Clemens London

100

Low Salary

Giovanni Rome

200

HighSalary

Grass

Berlin

300

HighSalary

Hoffman

London

100

Low Salary

Liu
Pereira

San Jose
Rome

200
100

HighSalary

Low Salary

7 rows selected.

38. 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.
SELECT snum,sname FROM salespeople UNION
SELECT cnum,cname FROM customers GROUP BY cnum,cname
HAVING cnum =ANY(SELECT cnum from orders group by cnum

having count(cnum)>1);
SNUM

SNAME

----------

----------

1001

peel

1002

Serres

1003

Axelrod

1004

Motika

1007

Rifkin

2004

Grass

2006

Clemens

2008

Cisneros

8 rows selected.

39. 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.
(Note: in the sample tables as given, there would be no such redundancy.
This is besides the point.)
SELECT snum "SNUM"FROM salespeople
WHERE lower(city)='san jos UNION
( SELECT cnum FROM customers WHERE
lower(city)='san jose UNION ALL SELECT onum FROM orders WHERE odate='03OCT-1990 );
SNUM
---------1002
2003
2008
3001

3002
3003
3005
3006
8 rows selected.
-----------------------------

40. Write a command that puts the following values, in their given order, into the
salespeople table: city San Jose, name Blanco, comm NULL, cnum 1100.
INSERT INTO salespeople (city,sname,comm,snum) VALUES('San
jose','Blanco',null,1100);
1 row created.
select * from salespeople
SNUM

SNAME

CITY

COMM

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

peel

1002

Serres

San Jos

1004

Motika

London

1007

London

Rifkin

1003

Axelrod

Newyork

1100

Blanco

San jose

.12
.13
.11

Barcelona

.15

.1

6 rows selected.
41. Write a command that removes all orders from customer Clemens from the
Customer table.
SELECT * FROM customers;
CNUM CNAME

CITY

RATING

SNUM

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

2001

Hoffman

London

2002

Giovanni Rome

2003

Liu

San Jose

2004

Grass

Berlin

300

1002

2006

Clemens

London

100

1001

2008

Cisneros San Jose

2007

Pereira

Rome

100

1001

200

1003

200

300
100

1002

1007
1004

7 rows selected.
DELETE customers WHERE LOWER(cname)='clemens';
1 row deleted.
SQL> select * from customers;
CNUM

CNAME

CITY

RATING

SNUM

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

Hoffman

2002

London

100

1001

Giovanni Rome

200

1003

2003

Liu

San Jose

200

1002

2004

Grass

Berlin

2008
2007

300

1002

Cisneros San Jose

300

1007

Pereira

100

1004

Rome

6 rows selected.

42. Write a command that increases the rating of all customers in Rome by 100.
SELECT * FROM customers;
CNUM CNAME

CITY

RATING

SNUM

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

London

100

1001

2002 Giovanni Rome

200

1003

2003 Liu

200

1002

San Jose

2004 Grass

Berlin

300

1002

2008 Cisneros San Jose

300

1007

2007 Pereira

100

1004

Rome

6 rows selected.
UPDATE customers SET rating=rating+100 WHERE LOWER(city)='rome';
2 rows updated.
SQL> select * from customers;
CNUM CNAME

CITY

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

----------

----------

2001 Hoffman

London

100

1001

300

1003

2002 Giovanni Rome

RATING

SNUM
----------

2003 Liu

San Jose

200

1002

2004 Grass

Berlin

300

1002

2008 Cisneros San Jose

300

1007

2007 Pereira

200

1004

Rome

6 rows selected.

43. Salesperson Serres has left the company. Assign her customers to
Motika.
SELECT * FROM salespeople;
SNUM SNAME

CITY

---------- ---------- ---------1001 peel

London

1002 Serres

San Jos

1004 Motika

London

COMM
---------.12
.13
.11

1007 Rifkin

Barcelona

1003 Axelrod

Newyork

1100 Blanco

San jose

.15
.1

6 rows selected.
UPDATE salespeople SET sname='Motika where lower(sname)='serres';
1 row updated.
SELECT * FROM salespeople;
SNUM SNAME

CITY

COMM

---------- ---------- ---------- ---------1001 peel

London

.12

1002 Motika

San Jos

1004 Motika

London

.11

1007 Rifkin

Barcelona

.15

1003 Axelrod

Newyork

1100 Blanco

San jose

.13

.1

6 rows selected.
44. 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.
INSERT INTO multicast
SELECT snum,sname,city,comm FROM
salespeople WHERE snum=ANY(SELECT snum FROM customers GROUP
BY snum HAVING COUNT(snum)>1);
2 rows created.
SELECT * FROM multicast;
SNUM SNAME

CITY

COMM

---------- ---------- ---------- ---------1001 peel


1002 Motika

London
San Jos

.12
.13

45. Write a command that deletes all customers with no current orders.
SELECT * FROM orders;
ONUM

AMT ODATE

CNUM

SNUM

---------- ---------- --------- ---------- ---------3001

18.69

03-OCT-90

2008

1007

767.19 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

3011

9891.88

06-OCT-90

2006

1001

3000

0 04-OCT-90

ONUM

AMT ODATE

2009
CNUM

1006
SNUM

---------- ---------- --------- ---------- ---------3013

04-OCT-90

2009

1006

12 rows selected.
DELETE orders

WHERE onum IS NULL;

2 rows deleted.

46. Write a command that increases by twenty percent the commissions


of all salespeople with total current orders above Rs. 3,000.
SELECT sname,comm+.20*comm"commision" FROM salespeople WHERE
snum=ANY(SELECT snum FROM orders WHERE amt>3000);

SNAME

commision

---------- ---------peel
Motika

.144
.156

47. Write a command that will enable a user to pull orders grouped by
date out of the Orders table quickly.
CREATE INDEX ord_ind ON order1(odate);
Index created.
SELECT odate FROM order1 GROUP BY odate;
ODATE
--------03-OCT-90
04-OCT-90
06-OCT-90

48.If the Orders table has already been created, how can you force the
onum field to be unique (assume all current values are unique)?
CREATE UNIQUE INDEX abc ON order1(onum);
Index created.

49.Create an index that would permit each salesperson to retrieve his or


her orders grouped by date quickly.
CREATE INDEX ord_snum ON orders(snum);
Index created.
CREATE INDEX sale_snum ON salespeople(snum);

Index created.
SELECT rownum,snum,onum,odate FROM orders WHERE odate =
ANY(SELECT odate FROM orders GROUP BY odate);
ROWNUM

SNUM

ONUM ODATE

---------- ---------- ---------- --------1

1007

3001 03-OCT-90

1001

3003 03-OCT-90

1004

3002 03-OCT-90

1002

3005 03-OCT-90

1007

3006 03-OCT-90

1003

3009 04-OCT-90

1006

3013 04-OCT-90

1006

3000 04-OCT-90

1002

3007 04-OCT-90

10

1001

3008 05-OCT-90

11

1002

3010 06-OCT-90

12

1001

3011 06-OCT-90

12 rows selected.

50.Let us suppose that each salesperson is to have only one customer of a


given rating, and that this is currently the case. Enter a command that
enforces it.
CREATE INDEX cust_s_c_r on customers(snum,cnum,rating);
Index created.

Das könnte Ihnen auch gefallen