ROLL NO-100 SQL QUERIES Q-1) Display snum, sname, city and comm. of all salespeople. Solution: SQL> select snum, sname, city, comm from salespeople; Output: SNUM SNAME CITY COMM ---------- --------------- ----------------------------------- 1001 Peel London .12 1002 Serres San Jose .13 1004 Motika London .11 1007 Rifkin Barcelona .15 1003 Axelord New York .1 Q-2) Display all snum without duplicates from all orders. Solution: SQL> select distinct snum from orders; Output: SNUM ---------- 1001 1002 1003 Q-3) Display names and commissions of all salespeople from London. Solution: SQL> select sname, comm from salespeople where city='London'; Output: SNAME COMM --------------- ---------- Peel .12 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
ROLL NO-100 SQL QUERIES
Q-1) Display snum, sname, city and comm. of all salespeople.
Solution:
SQL> select snum, sname, city, comm from salespeople;
Output: SNUM SNAME CITY COMM ---------- --------------- ----------------------------------- 1001 Peel London .12 1002 Serres San Jose .13 1004 Motika London .11 1007 Rifkin Barcelona .15 1003 Axelord New York .1
Q-2) Display all snum without duplicates from all orders.
Solution:
SQL> select distinct snum from orders;
Output: SNUM ---------- 1001 1002 1003
Q-3) Display names and commissions of all salespeople from London.
Solution:
SQL> select sname, comm from salespeople where city='London';
Output:
SNAME COMM --------------- ---------- Peel .12 Motika .11
Q-4) All customers with a rating of 100.
Solution:
SQL> select * from customer where rating=100;
1
ROLL NO-100 SQL QUERIES
Output: CNUM CNAME CITY RATING SNUM ---------- --------------- --------------- ---------- ---------- ---------------- 2001 Hoffman London 100 1001 2004 Grass Berlin 100 1002 2007 Pereira Rome 100 1004
Q-5) Produce order no, amount and date for all rows in the order table.
Q-6) All customers who were either located in San Jose or had a rating above $200.
Solution:
SQL> select * from customer where city='San Jose' or rating>200;
Output: CNUM CNAME CITY RATING SNUM ---------- --------------- --------------- ---------- -------------------- 2003 Liu San Jose 300 1002 2006 Clemens London 300 1007
Q-7) All customers in San Jose, who have a rating > 200.
Solution:
SQL> select * from customer where city='San Jose' and rating>200;
2
ROLL NO-100 SQL QUERIES
Output:
CNUM CNAME CITY RATING SNUM ---------- --------------- ------------------- ---------- ---------- 2003 Liu San Jose 300 1002
SQL> select cnum, cname from customer inner join salespeople on customer.snum=salespeople.snum where customer.snum=(select snum from salespeople where sname='Peel') or customer.snum=(select snum from salespeople where sname='Motika');
Q-19) Largest order taken by each sales order value more than $3000.
Solution:
SQL> select max (amt) from orders where amt>3000;
Output:
MAX(AMT) --------------- 9891
6
ROLL NO-100 SQL QUERIES
Q-20) Which day had the highest total amount ordered?
Solution:
SQL> select odate from orders where AMT = (select max(AMT) from orders);
Output:
ODATE ----------- 10-JUN-94
Q-21) Count all orders for 10 June.Get the output like:For dd/mm/yy, there are ____________ orders.
Solution:
SQL> select 'for' || to_char(odate,'dd/mm/yy') || 'there are' || count(onum) || 'orders' from orders where odate='10-JUN-94' group by odate;
Output:
'FOR'||TO_CHAR(ODATE,'DD/MM/YY')||'THEREARE'||COUNT(ONUM)||'ORDERS ------------------------------------------------------------------ for 10/06/94 there are 2orders
Q-22) Count the number of different non-null city values in customers table.
Solution:
SQL> select count (distinct city) from customer where city is not NULL;
Q-23) Assume that each salesperson has a commission of 12%. Produce order no, salesman No and amount salesman commission for each day and place the result in descending order.
Solution:
SQL>select onum,snum,amt*0.12"COMISSION" from orders order by COMISSION desc;
Q-35) All orders that are greater than the average for Oct. 4.
12
ROLL NO-100 SQL QUERIES
Solution:
SQL> select amt from orders where amt>(select avg(amt) from orders where odate='10-Mar-94');
Output:
AMT ---------- 5160.45 4723.95 9891.00
Q-36) Find all customers whose cnum is 1000 above the snum of serres.
Solution:
SQL> select cname, cnum from customer inner join salespeople on salespeople.snum=customer.snum where customer.cnum>1000+(select snum from salespeople where sname='Serres');
Output:
CNAME CNUM -------------- ---------- Liu 2003 Grass 2004 Clemens 2006 Pereira 2007
Q-37) Count the customers with rating above San Jose’s average.
Solution:
SQL> select count(cnum) from customer where rating>(select avg(rating) from customer where city='San Jose');
Output:
COUNT(CNUM) --------------------- 0
Q-38) Produce the names and rating of all customers who have above average orders.
13
ROLL NO-100 SQL QUERIES
Solution:
SQL> select c.cname, c.rating from customer c inner join orders o on c.cnum=o.cnum where o.amt>(select avg(amt) from orders);
Output: CNAME RATING -------------------------------- Liu 300 Grass 100 Clemens 300
Q-39) Find total amount in orders for each salesperson for whom this total is greater than the amount of the largest order in the order table.
Solution:
SQL> select snum,sum(amt) from Orders group by snum having sum(amt) > (select max(amt) from Orders)
Q-40) Find all customers with orders on Mar 10th .
Solution:
SQL> select cname, onum, odate from customer, orders where odate='10-Mar-94' and orders.cnum=customer.cnum;
Output: CNAME ONUM ODATE --------------------------------------------------- Hoffman 3003 10-MAR-94 Giovanne 3001 10-MAR-94 Liu 3005 10-MAR-94 Pereira 3002 10-MAR-94 Q-41) Find name and number of salespeople who have more than 1 or a customer.
Solution:
14
ROLL NO-100 SQL QUERIES
SQL> select snum,sname from salespeople where snum in (select snum from customer group by snum having count(cnum)>1);
Output:
SNUM SNAME ---------- --------------- 1002 Serres
Q-42) Find the sums of the amounts from order table grouped by date, eliminating all those dates where the sum was not at least 200 above the maximum amount.
Solution:
SQL> select sum(amt) from orders group by odate having sum(amt)> (select max(amt)+200 from orders);
Output: SUM(AMT) ------------- 11200.95
Q-43) Find names and numbers of all customers with rating equal to the maximum for their city.
Solution:
SQL> select cname,cnum from customer c1 where rating=(select max(rating) from customer c2 where c1.city=c2.city);
Output:
CNAME CNUM --------------- ---------- Giovanne 2002 Liu 2003 Grass 2004 Clemens 2006
Q-44) Find all salespeople who have customers in their cities who they don’t service.
Solution:
15
ROLL NO-100 SQL QUERIES
SQL> select s.sname,s.snum,c.cname from salespeople s inner join customer c on s.city=c.city where s.snum <> c.snum;
Output:
SNAME SNUM CNAME ------------ ---------- ------------ Motika 1004 Hoffman Motika 1004 Clemens Peel 1001 Clemens
Q-45) Find salespeople no, who have multiple customers.
Solution:
SQL> select s.snum from salespeople s where 1<(select count(c1.cnum) from customer c1 group by c1.snum having c1.snum=s.snum);
Output: SNUM ---------- 1002
Q-46) Find salespeople number, name, and city that have multiple customers.
Solution:
SQL> select s.snum,s.sname,s.city from salespeople s where 1<(select count(c1.cnum) from customer c1 group by c1.snum having c1.snum=s.snum);
Output: SNUM SNAME CITY ---------- ------------ -------------- 1002 Serres San Jose
Q-47) Extract rows of all salespeople with more than one current order.
Solution:
16
ROLL NO-100 SQL QUERIES
SQL> select * from salespeople where snum in (select snum from Orders group by snum having count(onum)>1);
Output: SNUM SNAME CITY COMM ---------- --------------- --------------- ------------- 1002 Serres San Jose .13 1003 Axelord New York .10
Q-48) Find all salespeople who have customers with a rating of 300. Use EXISTS.
Solution:
SQL> select * from salespeople s1 where exists (select cnum from customer c1 where s1.snum=c1.snum and rating=300);
Output:
SNUM SNAME CITY COMM --------- --------------- --------------- ---------- 1002 Serres San Jose .13 1007 Rifkin Barcelona .15
Q-49) Extract from customer’s table, every customer assigned to a salesperson who currently has at least one other customer (besides the customer being) with orders in the order table.
Solution:
SQL> select cname from customer where snum in (select snum from orders group by snum having count(onum)>1);
Output:
CNAME --------------- Liu Grass Giovanne
Q-50) Find salespeople with customers located in their cities.
Solution:
17
ROLL NO-100 SQL QUERIES
SQL> select sname from salespeople s1 where city = any (Select city from customer c1 where c1.snum = s1.snum);
Output:
SNAME --------------- Peel Serres
Q-51) Find all salespeople for whom there are customers that follow them in alphabetical order. (Two ways using ANY and EXISTS)
I-Solution:
SQL> select * from salespeople where snum=any(select snum from customer where sname<cname and salespeople.snum=customer.snum);
Output:
SNUM SNAME CITY COMM ----------- ------------- ------------- ---------- 1004 Motika London .11 1003 Axelord New York .10 II-Solution:
SQL> select * from salespeople where exists (select snum from customer where sname<cname and salespeople.snum=customer.snum);
Output: SNUM SNAME CITY COMM ----------- ----------- --------------- ------------ 1004 Motika London .11 1003 Axelord New York .10
Q-52) Select customers who have a greater rating than any customer in Rome.
Solution:
SQL> select * from customer where rating >any (select rating from customer where city='Rome');
Output:
CNUM CNAME CITY RATING SNUM --------- ----------------- ------------- ------------- ----------
18
ROLL NO-100 SQL QUERIES
2002 Giovanne Rome 200 1003 2003 Liu San Jose 300 1002 2006 Clemens London 300 1007
Q-53) Find all orders with amounts smaller than any amount for a customer in San Jose.
Solution:
SQL> select * from orders where amt <any(select amt from customer,orders where customer.cnum=orders.cnum and customer.city='San Jose');
Q-54) Append strings to the selected fields, indicating whether or not a given salesperson was matched to a customer in his city.
Solution:SQL> select s.snum ||' matched with ' || cnum from salespeople s,
customer c where s.snum=c.snum and s.city=c.city union select s.snum ||' not matched with '||cnum from salespeople s, customer c where s.snum=c.snum and s.city<>c.city;
Output: S.SNUM||'MATCHEDWITH'||CNUM ------------------------------------------------ 1001 matched with 2001 1002 matched with 2003 1002 not matched with 2004 1003 not matched with 2002 1004 not matched with 2007 1007 not matched with 2006 Q-55) Create a union of two queries that shows the names, cities and rating of all customers. Those with a rating of 200 or greater will have the words ‘High Rating’ while others will have ‘Low Rating’.
19
ROLL NO-100 SQL QUERIES
Solution:
SQL> select cname,city,rating, ' high ' "Rating" from customer where rating >=200 union (select cname,city,rating, ' low ' from customer where rating < 200);
Output:
CNAME CITY RATING Rating -------------- -------------------- ---------- ---------------- Clemens London 300 high Giovanne Rome 200 high Grass Berlin 100 low Hoffman London 100 low Liu San Jose 300 high Pereira Rome 100 low
Q-56) Create a view London staff that consists of all salespeople from London.
Solution:
SQL> select * from London_staff;
Output:
SNUM SNAME CITY COMM ---------- ------------ ------------ ---------- 1001 Peel London .12 1004 Motika London .11
Q-57) Create a view that consists of various rating and the counts.
Solution:
SQL> create view Varrating as select rating, count(*) "Count" from customer group by rating;
View created.
SQL> select * from Varrating;
Output:
RATING Count ------------ ----------
20
ROLL NO-100 SQL QUERIES
100 3 200 1 300 2
Q-58) The company provides a bonus for the salesperson that has the customer with the highest order on any given date. Create a view.
Solution:
SQL> create view comp_bonus as select snum,amt,odate from orders where amt in (select max(amt) from orders group by odate);