SQL ALL Queries
SQL ALL Queries
ga
ga
asdfasd
ga
Axelrod
Fran
London
San Jose
London
Barcelona
New York
London
.12
.13
.11
.15
.1
.26
8 rows selected.
2. List all customers with a rating of 100.
SQL> select cname from customers where rating=100;
CNAME
-------------------Hoffman
Clemens
Pereira
3. Find all records in the Customer table with NULL values in the city column.
SQL> select count(*) from customers where city=NULL;
COUNT(*)
---------0
4. Find the largest order taken by each salesperson on each date.
SQL> select snum,odate,max(amt) from orders group by odate,snum;
SNUM
---------1001
1007
1003
1004
1001
1002
1002
1001
ODATE
MAX(AMT)
--------- ---------06-OCT-96
9891.88
03-OCT-96
1098.16
04-OCT-96
1713.23
03-OCT-96
1900.1
03-OCT-96
767.19
03-OCT-96
5160.45
06-OCT-96
1309.95
05-OCT-96
4723
8 rows selected.
5. Arrange the Orders table by descending customer number.
SQL> select * from orders order by cnum desc;
ONUM
AMT ODATE
CNUM
SNUM
10. List the Customer table if and only if one or more of the customers in the C
ustomer table are located in San Jose.
SQL>
2
3
4
5
CNUM
---------2008
2003
CNAME
-------------------Cisneros
Liu
CITY
RATING
SNUM
-------------------- ---------- ---------San Jose
300
1007
San Jose
200
1002
11. Match salespeople to customers according to what city they lived in.
SQL> select sname,cname,s.city from salespeople s,customers c where s.city=c.cit
y;
SNAME
-------------------Fran
motika
peel
serres
Fran
motika
peel
serres
CNAME
-------------------Hoffman
Hoffman
Hoffman
Liu
Clemens
Clemens
Clemens
Cisneros
CITY
-------------------London
London
London
San Jose
London
London
London
San Jose
8 rows selected.
12. Find the largest order taken by each salesperson.
SQL> select max(amt),snum from orders group by snum;
MAX(AMT)
SNUM
---------- ---------1713.23
1003
9891.88
1001
5160.45
1002
1098.16
1007
1900.1
1004
13. Find customers in San Jose who have a rating above 200.
SQL> select * from customers where rating>200;
CNUM
---------2004
2008
CNAME
-------------------Grass
Cisneros
CITY
RATING
SNUM
-------------------- ---------- ---------Berlin
300
1002
San Jose
300
1007
SNAME
COMM
-------------------- ---------ga
.12
asdfasd
.11
Fran
.26
15. List all the orders of salesperson Motika from the Orders table.
SQL> select onum from orders where snum=(select snum from salespeople where snam
e='Motika');
ONUM
---------3002
16. Find all customers with orders on October 3.
SQL> select * from customers where snum in(select snum from orders where odate='
03-OCT-96');
CNUM
---------2008
2006
2001
2007
2004
2003
CNAME
-------------------Cisneros
Clemens
Hoffman
Pereira
Grass
Liu
CITY
RATING
SNUM
-------------------- ---------- ---------San Jose
300
1007
London
100
1001
London
100
1001
Rome
100
1004
Berlin
300
1002
San Jose
200
1002
6 rows selected.
17. Give the sums of the amounts from the Orders table, grouped by date, elimina
ting all those dates where the SUM was not at least 2000.00 above the MAX amount
.
SQL> select odate,sum(amt) from orders group by odate;
ODATE
SUM(AMT)
--------- ---------05-OCT-96
4723
03-OCT-96
8944.59
04-OCT-96
1788.98
06-OCT-96 11201.83
18. Select all orders that had amounts that were greater than at least one of th
e orders from October 6.
SQL> select * from orders where amt>any(select amt from orders where odate='06-o
ct-96');
ONUM
AMT ODATE
CNUM
SNUM
---------- ---------- --------- ---------- ---------3011
9891.88 06-OCT-96
2006
1001
3005
5160.45 03-OCT-96
2003
1002
3008
4723 05-OCT-96
2006
1001
3002
1900.1 03-OCT-96
2007
1004
3009
1713.23 04-OCT-96
2002
1003
19. Write a query that uses the EXISTS operator to extract all salespeople who h
ave customers with a rating of 300.
SQL> select * from salespeople s where exists(select * from customers c where c.
snum=s.snum and c.rating=300);
SNUM SNAME
CITY
COMM
---------- -------------------- -------------------- ---------1002 serres
San Jose
.13
20. Find all pairs of customers having the same rating.
SQL> select * from customers order by rating;
CNUM
---------2001
2007
2006
2002
2003
2008
2004
CNAME
-------------------Hoffman
Pereira
Clemens
Giovani
Liu
Cisneros
Grass
CITY
RATING
SNUM
-------------------- ---------- ---------London
100
1001
Rome
100
1004
London
100
1001
Rome
200
1003
San Jose
200
1002
San Jose
300
1007
Berlin
300
1002
7 rows selected.
21. Find all customers whose CNUM is 1000 above the SNUM of Serres.
SQL> select cnum,cname from customers where cnum>(select snum from salespeople w
here sname='serres');
CNUM
---------2001
2002
2003
2004
2006
2008
2007
CNAME
-------------------Hoffman
Giovani
Liu
Grass
Clemens
Cisneros
Pereira
7 rows selected
22. Give the salespeople s commissions as percentages instead of decimal numbers.
SQL> select comm*100 from salespeople;
COMM*100
---------12
13
11
15
10
26
8 rows selected.
23. Find the largest order taken by each salesperson on each date, eliminating t
Grass
.13
29. Find salespeople who have multiple customers.
SQL> select snum,count(snum) from customers group by snum having count(snum)>1;
SNUM COUNT(SNUM)
---------- ----------1001
2
1002
2
30. Find salespeople with customers located in their city.
SQL> select s.sname,c.cname,s.city from salespeople s join customers c on s.city
=c.city order by city;
SNAME
-------------------peel
motika
peel
Fran
Fran
motika
serres
serres
CNAME
-------------------Clemens
Hoffman
Hoffman
Hoffman
Clemens
Clemens
Cisneros
Liu
CITY
-------------------London
London
London
London
London
London
San Jose
San Jose
8 rows selected.
31. Find all salespeople whose name starts with
MAX(AMT)
l .
---------- ---------1002
5160.45
1007
1098.16
34.Extract the Salespeople table in the following order : SNUM, SNAME, COMMISSIO
N, CITY
SNAME
COMM CITY
-------------------- ---------- -------------------Peel
12 London
Serres
13 San Jose
Motika
11 London
Rifkin
15 Barcelona
AxelRod
10 New York
Fran
26 London
6 rows selected.
35.Select all customers whose names fall in between
and
alphabetical range.
SQL> select cname from customers where cname between 'A' and 'G' order by cname
asc;
CNAME
-------------------Cisneros
Clemens
36.Select all the possible combinations of customers that you can assign
SQL> select c.cname,s.sname from customers c join salespeople s on c.snum=s.snum
;
CNAME
-------------------Clemens
Hoffman
Grass
Liu
Pereira
Giovani
SNAME
-------------------peel
peel
serres
serres
motika
Axelrod
6 rows selected.
37. Select all orders that are greater than the average for October 4.
38.Write a select command using a corelated subquery that selects the names and
numbers of all customers with ratings equal to the maximum for their city
SQL> select cnum,cname,rating from customers where rating in(select max(rating)
from customers group by city);
CNUM
---------2007
2006
2001
2008
2004
2003
2002
CNAME
RATING
-------------------- ---------Pereira
100
Clemens
100
Hoffman
100
Cisneros
300
Grass
300
Liu
200
Giovani
200
7 rows selected.
39.Write a query that totals the orders for each day and places the results in d
escending order
SQL> select sum(amt) from orders group by odate order by sum(amt) desc;
SUM(AMT)
---------11201.83
8944.59
4723
1788.98
40.Write a select command that produces the rating followed by the name of each
customer in San Jose
SQL> select rating,cname from customers where city in(select city from salespeop
le1 where city='San Jose');
RATING
---------300
200
CNAME
-------------------Cisneros
Liu
41.Find all orders with amounts smaller than any amount for a customer in San Jo
se
42.Find all orders with above average amounts for their customers
SQL> select onum from orders where amt >(select avg(amt) from orders);
ONUM
---------3005
3008
3011
43. Write a query that selects the highest rating in each city.
CITY
MAX(RATING)
-------------------- ----------London
100
Berlin
300
Rome
200
San Jose
300
44. Write a query that calculates the amount of the salesperson s commission on ea
ch order by a customer with a rating above 100.00.
SQL> select sname,comm*amt from salespeople1,orders where snum in(select snum f
rom customers where rating > 100);
SNAME
COMM*AMT
-------------------- ---------AxelRod
10
Serres
13
Rifkin
15
45. Count the customers with ratings above San Jose s average.
SQL> select count(city) from customers where rating >(select avg(rating) from c
ustomers where city='San Jose');
COUNT(CITY)
----------2
46. Write a query that produces all pairs of salespeople with themselves as wel
l as duplicate rows with the order reversed.
SQL> select cnum,cname,rating from customers where rating in(select max(ratin
from customers group by city);
CNUM
---------2007
2006
2001
2008
2004
2003
2002
CNAME
RATING
-------------------- ---------Pereira
100
Clemens
100
Hoffman
100
Cisneros
300
Grass
300
Liu
200
Giovani
200
7 rows selected.
SQL> select s.sname,d.sname from salespeople s,salespeople d order by s.sname
sname desc;
SNAME
SNAME
-------------------- -------------------Axelrod
Axelrod
Axelrod
Axelrod
Axelrod
Axelrod
Axelrod
Axelrod
Fran
Fran
Fran
serres
peel
motika
ga
Fran
Axelrod
SNAME
-------------------Fran
Fran
Fran
Fran
Fran
ga
ga
ga
ga
ga
ga
SNAME
-------------------peel
motika
ga
Fran
Axelrod
SNAME
-------------------ga
ga
motika
motika
motika
motika
motika
motika
motika
motika
peel
SNAME
-------------------Fran
Axelrod
SNAME
-------------------peel
peel
peel
peel
peel
peel
peel
serres
serres
serres
serres
SNAME
--------------------
SNAME
-------------------serres
serres
serres
serres
SNAME
-------------------motika
ga
Fran
Axelrod
serres
serres
peel
motika
ga
serres
peel
motika
ga
Fran
Axelrod
serres
peel
motika
ga
Fran
Axelrod
serres
peel
serres
serres
peel
SNAME
SNAME
-------------------- -------------------peel
motika
motika
ga
ga
Fran
Fran
Axelrod
Axelrod
64 rows selected.
SQL> select snum,sname from salespeople1 where snum in (select snum from custome
rs where cnum in(select cnum from orders having count(cnum)=1 group by cnum));
SNUM
---------1001
1002
1004
SNAME
-------------------Peel
Serres
Motika
49.Write a query that joins the Customer table to itself to find all pairs of cu
stomers served by a single salesperson.
SQL> select c1.cname,c2.cname from customers c,customers c2 where c.snum=c2.snum
and c.cname!=c2.cname;
50.1. Write a query that will give you all orders for more than $1000.00
51.Write a query that lists each order number followed by the name of the custom
er who made that order.
SQL> select orders.onum,customers.cname from customers,orders where customers.cn
um=orders.cnum;
ONUM
---------3001
3003
3002
3005
3006
3009
3007
3008
3010
3011
CNAME
-------------------Cisneros
Hoffman
Pereira
Liu
Cisneros
Giovani
Giovani
Clemens
Grass
Clemens
10 rows selected.
52.Write 2 queries that select all salespeople (by name and number) who have cus
tomers in their cities who they do not service, one using a join and one a corel
ated subquery. Which soluti
SQL> select c.snum,s.snum from salespeople s,customers c where s.city=c.city gro
up by c.snum,s.snum having c.snum!=s.snum;
SNUM
SNUM
---------- ---------1007
1002
1001
1004
1001
1005
53.1. Write a query that selects all customers whose ratings are equal to or gre
ater than ANY (in the SQL sense) of Serres ?
SQL> select cname from customers where rating >= ANY (select rating from custom
ers where snum =(select snum from salespeople1 where sname='Serres'));
CNAME
-------------------Grass
Cisneros
Liu
Giovani
54.Write 2 queries that will produce all orders taken on October 3 or October 4
55.1. Write a query that produces all pairs of orders by a given customer. Name
that customer and eliminate duplicates.
SQL> select c.cname,o.onum from customers c,orders o where c.cnum=o.cnum group b
y c.cname,o.onum;
CNAME
ONUM
-------------------- ---------Hoffman
3003
Clemens
3011
Liu
3005
Cisneros
3001
Giovani
3007
Pereira
3002
Cisneros
3006
Giovani
3009
Grass
3010
Clemens
3008
10 rows selected.
56.Find only those customers whose ratings are higher than every customer in Rom
e.
SQL> select cname from customers where rating >(select max(rating) from custome
rs where city='Rome');
CNAME
-------------------Grass
Cisneros
57. Write a query on the Customers table whose output will exclude all customers
with a rating <= 100.00, unless they are located in Rome.
SQL> select cname from customers where city='Rome' intersect select cname from
customers where rating>=100;
CNAME
-------------------Giovani
Pereira
58.Find all rows from the Customers table for which the salesperson number is 10
0
SQL> select * from customers where snum=1001;
CNUM
---------2001
2006
CNAME
-------------------Hoffman
Clemens
CITY
RATING
SNUM
-------------------- ---------- ---------London
100
1001
London
100
1001
59.Find the total amount in Orders for each salesperson for whom this total is g
reater than the amount of the largest order in the table.
60.Write a query that selects all orders save those with zeroes or NULLs in the
amount field.
SQL> select onum from orders where amt is NULL or amt=0;
no rows selected
61.Produce all combinations of salespeople and customer names such that the form
er precedes the latter alphabetically, and the latter has a rating of less than
200.
SQL> select s.sname,c.cname,c.rating from customers c,salespople s where c.ratin
g<200 group by s.sname,c.cname,c.rating having c.cname>=s.sname;
62.List all Salespeople s names and the Commission they have earned.
63. Write a query that produces the names and cities of all customers with the s
ame rating as Hoffman. Write the query using Hoffman s CNUM rather than his rating
, so that it would still be usable if his rating changed.
SQL> select cname,city from customers where rating in(select rating from custom
ers where snum = (select snum from customers where cname='Hoffman'));
CNAME
-------------------Pereira
Clemens
Hoffman
CITY
-------------------Rome
London
London
64.Find all salespeople for whom there are customers that follow them in alphabe
tical order.
SQL> select s.sname,c.cname from customers c,salespeople1 s where s.snum=c.snum
group by s.sname,c.cname having c.cname >= s.sname;
SNAME
-------------------AxelRod
Motika
CNAME
-------------------Giovani
Pereira
65.Write a query that produces the names and ratings of all customers of all who
have above average orders
SQL> select cname,rating from customers where rating > (select avg(rating) from
customers);
CNAME
RATING
-------------------- ---------Giovani
200
Liu
200
Grass
300
Cisneros
300
66.Find the SUM of all purchases from the Orders table.
SQL> select sum(amt) from orders;
SUM(AMT)
---------26658.4
67.Write a SELECT command that produces the order number, amount and date for al
l rows in the order table.
SQL> select onum,amt,odate from orders;
ONUM
AMT ODATE
---------- ---------- --------3001
18.69 03-OCT-96
3003
767.19 03-OCT-96
3002
1900.1 03-OCT-96
3005
5160.45 03-OCT-96
3006
1098.16 03-OCT-96
3009
1713.23 04-OCT-96
3007
75.75 04-OCT-96
3008
4723 05-OCT-96
3010
1309.95 06-OCT-96
3011
9891.88 06-OCT-96
10 rows selected.
68.Count the number of nonNULL rating fields in the Customers table (including r
epeats).
SQL> select count(rating) from customers;
COUNT(RATING)
------------7
69.Write a query that gives the names of both the salesperson and the customer f
or each order after the order number.
SQL> select cname,sname,odate from salespeople1,customers,orders where salespeop
le1.snum=orders.snum and customers.cnum=orders.cnum;
CNAME
-------------------Hoffman
Giovani
Giovani
Liu
Grass
Clemens
Clemens
Cisneros
Cisneros
Pereira
SNAME
-------------------Peel
AxelRod
AxelRod
Serres
Serres
Peel
Peel
Rifkin
Rifkin
Motika
ODATE
--------03-OCT-96
04-OCT-96
04-OCT-96
03-OCT-96
06-OCT-96
06-OCT-96
05-OCT-96
03-OCT-96
03-OCT-96
03-OCT-96
10 rows selected.
SQL> select snum,sname from salespeople1 where sname not in(select sname from sa
lespeople1 where city = any (select city from customers));
SNUM
---------1007
1003
SNAME
-------------------Rifkin
AxelRod
72.Write a query using the EXISTS operator that selects all salespeople with cus
tomers located in their cities who are not assigned to them.
SQL> select s.sname,city from salespeople s where exists(select * from customers
c where c.snum!=s.snum and c.city=s.city);
SNAME
-------------------Fran
motika
CITY
-------------------London
London
serres
San Jose
73.Write a query that selects all customers serviced by Peel or Motika. (Hint :
The SNUM field relates the two tables to one another.
SQL> select cname from customers where snum in (select snum from salespeople1 w
here sname='Motika' or sname='Peel');
CNAME
-------------------Clemens
Hoffman
Pereira
74. Count the number of salespeople registering orders for each day. (If a sales
person has more than one order on a given day, he or she should be counted only
once.)
SQL> select count(distinct snum),odate from orders group by odate;
COUNT(DISTINCTSNUM)
------------------1
2
4
1
ODATE
--------05-OCT-96
06-OCT-96
03-OCT-96
04-OCT-96
SQL> select onum from orders where snum in (select snum from salespeople1 where
city='London');
ONUM
---------3011
3008
3003
3002
76.Find all orders by customers not located in the same cities as their salespeo
ple.
SQL> select onum from orders where cnum in(select distinct cnum from customers,s
alespeople1 where customers.city not in(select salespeople1.city from salespeopl
e1));
ONUM
---------3002
3007
3009
3010
77.Find all salespeople who have customers with more than one current order
SQL>
2
3
4
SNUM
---------1003
1001
1007
78.Write a query that extracts from the Customers table every customer assigned
to a salesperson who currently has at least one other customer (besides the cust
omer being selected) with orders in the Orders table.
79.Write a query that selects all customers whose names begin with
C .
CITY
MAX(RATING)
-------------------- ----------London
100
Berlin
300
Rome
200
San Jose
300
81.Write a query that will produce the SNUM values of all salespeople with order
s currently in the Orders table (without any repeats).
CNAME
CNUM
-------------------- ---------Grass
2004
Cisneros
2008
Liu
2003
Giovani
2002
Pereira
2007
Clemens
2006
Hoffman
2001
7 rows selected.
84.Find all orders credited to the same salesperson who services Hoffman (CNUM 2
001).
SQL> select onum from orders where snum in(select snum from customers where cna
me='Hoffman');
ONUM
---------3011
3008
3003
85.Find all salespeople whose commission is in between 0.10 and 0.12 (both inclu
sive).
CITY
-------------------London
London
London
8 rows selected.
89.Write a query that selects the first customer in alphabetical order whose nam
e begins with G.
90.Write a query that counts the number of different nonNULL city values in the
Customers table.
---------2665.84
92.What would be the output from the following query?
SELECT * FROM ORDERS WHERE NOT (odate = 10/03/96 OR snum > 1006) AND amt >= 1500
);
>select * from orders where not(odate='03-OCT-96' or snum>1006) and amt>=1500;
93.Find all customers who are not located in San Jose and whose rating is above
200.
SQL> select cname from customers where city!='San Jose' and rating > 200;
CNAME
-------------------Grass
SQL> SELECT snum, sname city, comm FROM salespeople1 WHERE (comm > + 0.12 OR co
mm < 0.14);
SNUM
---------1001
1002
1004
1007
1003
1005
CITY
COMM
-------------------- ---------Peel
12
Serres
13
Motika
11
Rifkin
15
AxelRod
10
Fran
26
6 rows selected.
95.Evaluate the following query :
SELECT * FROM orders
WHERE NOT ((odate = 10/03/96 AND snum > 1002) OR amt > 2
000.00);
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
96.Which salespersons attend to customers not in the city they have been assigne
d to?
SQL> select s.sname,s.city,c.cname,c.city from salespeople1 s,customers c where
c.city!=s.city and s.snum=c.snum;
SNAME
CITY
CNAME
-------------------- -------------------- -------------------CITY
-------------------AxelRod
New York
Giovani
Rome
Serres
Berlin
San Jose
Grass
Rifkin
San Jose
Barcelona
Cisneros
SNAME
CITY
CNAME
-------------------- -------------------- -------------------CITY
-------------------Motika
London
Pereira
Rome
97.Which salespeople get commission greater than 0.11 are serving customers rate
d less than 250?
SQL> select sname from salespeople1 where comm > 11 and snum in(select snum from
customers where rating < 250);
SNAME
-------------------Peel
Serres
98.Which salespeople have been assigned to the same city but get different commi
ssion percentages?
SQL> select sname,comm,city from salespeople1 where city in(
2 select city from salespeople1
3 group by city having count(city) > 1);
SNAME
COMM CITY
-------------------- ---------- -------------------Peel
12 London
Motika
Fran
11 London
26 London
100.Does the customer who has placed the maximum number of orders have the maxim
um rating?
SQL> select cnum,count(onum) from orders having count(onum) in (select max(count
(onum)) from orders group by cnum)group by cnum;
CNUM COUNT(ONUM)
---------- ----------2006
2
2008
2
2002
2
SQL> select max(rating) from customers where cnum in (select cnum from orders ha
ving count(onum) in (select max(count(onum)) from orders group by cnum)group by
cnum);
MAX(RATING)
----------300
102)
SQL> select cname,rating from customers order by rating desc;
CNAME
RATING
-------------------- ---------Grass
300
Cisneros
300
Liu
200
Giovani
200
Pereira
100
Clemens
100
Hoffman
100
7 rows selected.
103.On which days has Hoffman placed orders?
SQL> select onum,odate from orders where cnum in(select cnum from customers wher
e cname='Hoffman');
ONUM ODATE
---------- --------3003 03-OCT-96
104.Do all salespeople have different commissions?
SQL> select count(snum) from salespeople1 where snum in( select snum from orders
group by snum);
COUNT(SNUM)
----------5
SQL> select odate from orders where amt in(select max(amt) from orders group by
snum);
ODATE
--------03-OCT-96
03-OCT-96
03-OCT-96
04-OCT-96
06-OCT-96
SNAME
-------------------Fran
Rifkin
Serres
Peel
Motika
AxelRod
6 rows selected.
SQL> select * from(select sum(comm*amt*0.01) as amt,sname from salespeople1,orde
rs group by sname order by amt desc)where rownum<2;
AMT SNAME
---------- -------------------6931.184 Fran
//////////////////////////////////////////////////////////////
SQL> select a,b from(select sum(comm*amt*0.01) as a,sname as b from salespeople1
,orders group by sname order by a desc)where rownum<2;
//alias a used as order by//
A B
---------- -------------------6931.184 Fran
110.Who is the worst customer with respect to the company?
SQL> select a,c from(select sum(amt) as a,cnum as c from orders group by cnum or
der by a)where rownum=1;
A
C
---------- ---------767.19
2001
111.Are all customers not having placed orders greater than 200 totally been ser
viced by salespersons Peel or Serres?
select sname,snum from salespeople where snum in(select snum from customers wher
e rating>200) and sname!='peel' and sname!='serres';
no rows selected
112.Which customers have the same rating?
CNAME
RATING
-------------------- ---------Pereira
100
Hoffman
100
Pereira
100
Clemens
100
Clemens
100
Hoffman
100
Giovani
200
Liu
200
Cisneros
300
Grass
300
10 rows selected.
//////////////////////////////
SQL> select cname from customers where rating in(select rating from(select ratin
g from customers group by rating)where rownum<4);
CNAME
-------------------Pereira
Clemens
Hoffman
Liu
Giovani
Cisneros
Grass
7 rows selected.
////////////////////////////////
113.Find all orders greater than the average for October 4th.
SQL> select onum from orders where amt > (select avg(amt) from orders where odat
e='04-OCT-1996');
ONUM
----------
3002
3005
3006
3009
3008
3010
3011
7 rows selected.
116.Select the total amount in orders for each salesperson for whom the total is
greater than the amount of the largest order in the table.
SQL> select sum(amt),snum from orders group by snum having sum(amt) >(select max
(amt) from orders);
SUM(AMT)
SNUM
---------- ---------15382.07
1001
117.Give names and numbers of all salespersons who have more than one customer.
SQL> select snum,sname from salespeople1 where snum in(select snum from customer
s group by snum having count(snum)>1);
SNUM
---------1001
1002
SNAME
-------------------Peel
Serres
118.Select all salespersons by name and number who have customers in their city
whom they don t service.
SQL> select s.sname,s.snum from salespeople s,customers c where s.city=c.city an
d s.snum!=c.snum;
SNAME
SNUM
-------------------- ---------Fran
1005
motika
1004
Fran
1005
motika
1004
serres
1002
119.Which customers
SQL> select cnum,cname from customers where cnum in(select cnum from orders grou
p by cnum having sum(amt)=(select min(sum(amt)) from orders group by cnum));
CNUM CNAME
---------- -------------------2001 Hoffman
SQL> select sname from salespeople1 where snum in(select snum from customers whe
re city='Berlin');
SNAME
--------------------
Serres
121.Is there any evidence linking the performance of a salesperson to the commis
sion that he or she is being paid?
SQL> select o.snum,count(o.snum),snum(o.amt*s.comm) from salespeople s,orders o
where o.snum=s.snum group by o.snum;
122.Does the total amount in orders by customer in Rome and London exceed the co
mmission paid to salespersons in London and New York by more than 5 times?
123.Which is the date, order number, amt and city for each salesperson (by name)
for the maximum order he has obtained?
SQL> select o.onum,o.snum,s.city,o.odate,o.amt from orders o,salespeople1 s wher
e o.snum=s.snum and amt in(select max(amt) from orders group by snum);
ONUM
SNUM CITY
---------- ---------- -------------------3011
1001 London
3005
1002 San Jose
3002
1004 London
3006
1007 Barcelona
3009
1003 New York
ODATE
AMT
--------- ---------06-OCT-96
9891.88
03-OCT-96
5160.45
03-OCT-96
1900.1
03-OCT-96
1098.16
04-OCT-96
1713.23