Table Salespeople Snum Sname City Comm
Table Salespeople Snum Sname City Comm
Table Salespeople Snum Sname City Comm
1 of 15
TABLE SALESPEOPLE
TABLE CUST
ORDERS
Problems :
5. Produce orderno, amount and date form all rows in the order table.
Suresh Babu
Page no. 2 of 15
Suresh Babu
Page no. 3 of 15
Suresh Babu
Page no. 4 of 15
from orders
group by cnum;
25. First customer in alphabetical order whose name begins with G.
Select min(cname)
from cust
where cname like ‘G%’;
26. Get the output like “ For dd/mm/yy there are ___ orders.
Select 'For ' || to_char(odate,'dd/mm/yy') || ' there are '||
count(*) || ' Orders'
from orders
group by odate;
27. Assume that each salesperson has a 12% commission. Produce order
no., salesperson no., and amount of salesperson’s commission for that
order.
Select onum, snum, amt, amt * 0.12
from orders
order by snum;
28. Find highest rating in each city. Put the output in this form. For the
city (city), the highest rating is : (rating).
Select 'For the city (' || city || '), the highest rating is : (' ||
max(rating) || ')'
from cust
group by city;
29. Display the totals of orders for each day and place the results in
descending order.
Select odate, count(onum)
from orders
group by odate
order by count(onum);
30. All combinations of salespeople and customers who shared a city. (ie
same city).
Select sname, cname
from salespeople, cust
where salespeople.city = cust.city;
31. Name of all customers matched with the salespeople serving them.
Select cname, sname
from cust, salespeople
where cust.snum = salespeople.snum;
32. List each order number followed by the name of the customer who
made the order.
Select onum, cname
from orders, cust
where orders.cnum = cust.cnum;
33. Names of salesperson and customer for each order after the order
number.
Select onum, sname, cname
Suresh Babu
Page no. 5 of 15
Select cname
from cust
Suresh Babu
Page no. 6 of 15
45. All orders credited to the same salesperson who services Hoffman.
Select onum, sname, cname, amt
from orders a, salespeople b, cust c
Suresh Babu
Page no. 7 of 15
Suresh Babu
Page no. 8 of 15
53. Produce the names and rating of all customers who have above
average orders.
Select max(b.cname), max(b.rating), a.cnum
from orders a, cust b
where a.cnum = b.cnum
group by a.cnum
having count(a.cnum) > ( select avg(count(cnum))
from orders
group by cnum);
54. Find total amount in orders for each salesperson for whom this total
is greater than the amount of the largest order in the table.
Select snum,sum(amt)
from orders
group by snum
having sum(amt) > ( select max(amt)
from orders);
55. Find all customers with order on 3rd Oct.
Select cname
from cust a, orders b
where a.cnum = b.cnum and
odate = ‘03-OCT-94’;
56. Find names and numbers of all salesperson who have more than one
customer.
Select sname, snum
from salespeople
where snum in ( select snum
from cust
group by snum
having count(snum) > 1 );
57. Check if the correct salesperson was credited with each sale.
Select onum, a.cnum, a.snum, b.snum
from orders a, cust b
where a.cnum = b.cnum and
a.snum != b.snum;
58. Find all orders with above average amounts for their customers.
select onum, cnum, amt
from orders a
where amt > ( select avg(amt)
from orders b
where a.cnum = b.cnum
group by cnum);
59. Find the sums of the amounts from order table grouped by date,
eliminating all those dates where the sum was not at least 2000 above the
maximum amount.
Suresh Babu
Page no. 9 of 15
from orders b
where a.odate = b.odate
group by odate);
60. Find names and numbers of all customers with ratings equal to the
maximum for their city.
Select a.cnum, a.cname
from cust a
where a.rating = ( select max(rating)
from cust b
where a.city = b.city);
61. Find all salespeople who have customers in their cities who they don’t
service. ( Both way using Join and Correlated subquery.)
Select distinct cname
from cust a, salespeople b
where a.city = b.city and
a.snum != b.snum;
Select cname
from cust
where cname in ( select cname
from cust a, salespeople b
where a.city = b.city and
a.snum != b.snum );
62. Extract cnum,cname and city from customer table if and only if one
or more of the customers in the table are located in San Jose.
Select * from cust
where 2 < (select count(*)
from cust
where city = 'San Jose');
63. Find salespeople no. who have multiple customers.
Select snum
from cust
group by snum
having count(*) > 1;
64. Find salespeople number, name and city who have multiple
customers.
Select snum, sname, city
from salespeople
where snum in ( select snum
from cust
group by snum
having count(*) > 1);
Suresh Babu
Page no. 10 of 15
having count(*) = 1;
66. Extract rows of all salespeople with more than one current order.
Select snum, count(snum)
from orders
group by snum
having count(snum) > 1;
67. Find all salespeople who have customers with a rating of 300. (use
EXISTS)
Select a.snum
from salespeople a
where exists ( select b.snum
from cust b
where b.rating = 300 and
a.snum = b.snum)
68. Find all salespeople who have customers with a rating of 300. (use
Join).
Select a.snum
from salespeople a, cust b
where b.rating = 300 and
a.snum = b.snum;
69. Select all salespeople with customers located in their cities who are
not assigned to them. (use EXISTS).
Select snum, sname
from salespeople
where exists ( select cnum
from cust
where salespeople.city = cust.city and
salespeople.snum != cust.snum);
70. Extract from customers table every customer assigned the a
salesperson who currently has at least one other customer ( besides the
customer being selected) with orders in order table.
Select a.cnum, max(c.cname)
from orders a, cust c
where a.cnum = c.cnum
group by a.cnum,a.snum
having count(*) < ( select count(*)
from orders b
where a.snum = b.snum)
order by a.cnum;
71. Find salespeople with customers located in their cities ( using both
ANY and IN).
Select sname
from salespeople
where snum in ( select snum from cust
Select sname
from salespeople
Suresh Babu
Page no. 11 of 15
Select sname
from salespeople
where exists ( select cname
from cust
where salespeople.snum = cust.snum and
salespeople.sname < cust.cname);
73. Select customers who have a greater rating than any customer in
rome.
Select a.cname
from cust a
where city = 'Rome' and
rating > ( select max(rating)
from cust
where city != 'Rome');
74. Select all orders that had amounts that were greater that atleast one
of the orders from Oct 6th.
Select onum, amt
from orders
where odate != '06-oct-94' and
amt > ( select min(amt)
from orders
where odate = '06-oct-94');
75. Find all orders with amounts smaller than any amount for a customer
in San Jose. (Both using ANY and without ANY)
Select onum, amt
from orders
where amt < any ( select amt
from orders, cust
where city = 'San Jose' and
orders.cnum = cust.cnum);
Suresh Babu
Page no. 12 of 15
Select *
from cust a
where not exists ( select b.rating from cust b
where b.city != 'Paris' and
b.rating > a.rating);
77. Select all customers whose ratings are equal to or greater than ANY
of the Seeres.
Select cname, sname
from cust, salespeople
where rating >= any ( select rating
from cust
where snum = (select snum
from salespeople
where sname = 'Serres'))
and sname != 'Serres'
and salespeople.snum(+) = cust.snum;
78. Find all salespeople who have no customers located in their city.
( Both using ANY and ALL)
Select sname
from salespeople
where snum in ( select snum
from cust
where salespeople.city != cust.city and
salespeople.snum = cust.snum);
Select sname
from salespeople
where snum = any ( select snum
from cust
where salespeople.city != cust.city and
salespeople.snum = cust.snum);
79. Find all orders for amounts greater than any for the customers in
London.
Select onum, amt
from orders
Suresh Babu
Page no. 13 of 15
Suresh Babu
Page no. 14 of 15
Suresh Babu
Page no. 15 of 15
Suresh Babu