oracle soft copy

28
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

Upload: parth-r-shah

Post on 16-Apr-2015

161 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Oracle Soft Copy

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

Page 2: Oracle Soft Copy

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.

Solution:

SQL> select onum, amt, odate from orders;

Output:

ONUM AMT ODATE ---------- ---------- ------- -------- 3001 18.96 10-MAR-94 3003 767.19 10-MAR-94 3002 1900.10 10-MAR-94 3005 5160.45 10-MAR-94 3006 1098.16 10-MAR-94 3009 1713.23 10-APR-94 3007 75.75 10-APR-94 3008 4723.95 10-MAY-94 3010 1309.95 10-JUN-94 3011 9891.00 10-JUN-94

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

Page 3: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

Output:

CNUM CNAME CITY RATING SNUM ---------- --------------- ------------------- ---------- ---------- 2003 Liu San Jose 300 1002

Q-8) All orders for more than $1000.

Solution:

SQL> select * from orders where amt>1000;

Output: ONUM AMT ODATE CNUM SNUM ---------- ---------- --------- ---------- ---------- -------------- 3002 1900.10 10-MAR-94 2007 1003 3005 5160.45 10-MAR-94 2003 1002 3006 1098.16 10-MAR-94 2008 1002 3009 1713.23 10-APR-94 2002 1003 3008 4723.95 10-MAY-94 2006 1001 3010 1309.95 10-JUN-94 2004 1002

3011 9891.00 10-JUN-94 2006 1001

Q-9) Names and cities of all salespeople in London with a commission above 0.10.

Solution:

SQL> select sname, city from salespeople where city='London' and comm>0.10;

Output: SNAME CITY --------------- --------------- Peel London Motika London

Q-10) All customers excluding those with rating <= 100 if they are located in Rome.

Solution:

SQL> select * from customer where not (rating<=100 and city='Rome');

3

Page 4: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

Output: CNUM CNAME CITY RATING SNUM ----------- --------------- --------------- ------------ ---------- 2001 Hoffman London 100 1001 2002 Giovanne Rome 200 1003 2003 Liu San Jose 300 1002 2004 Grass Berlin 100 1002

2006 Clemens London 300 1007

Q-11) All salespeople either in Barcelona or in London.

Solution:

SQL> select * from salespeople where city='Barcelona' or city='London';

Output:

SNUM SNAME CITY COMM ---------- --------------- --------------- ---------- 1001 Peel London .12 1004 Motika London .11 1007 Rifkin Barcelona .15

Q-12) All salespeople with commission between 0.10 and 0.12 boundary. (Boundary values 0.10 and 0.12 to be included.)

Solution:

SQL> select * from salespeople where comm between 0.10 and 0.12;

Output:

SNUM SNAME CITY COMM ---------- --------------- --------------- ---------- 1001 Peel London .12 1004 Motika London .11 1003 Axelord New York .10

Q-13) All customers without a city.

Solution:

SQL> select * from customer where city is NULL;

Output:

no rows selected.

4

Page 5: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

Q-14) All orders taken on Mar. 10th or 10th Apr. Solution:

SQL> select * from orders where odate='10-Mar-94' or odate='10-Apr-94';

Output:

ONUM AMT ODATE CNUM SNUM ---------- ---------- --------- ---------- ---------- -------------- 3001 18.96 10-MAR-94 2002 1002 3003 767.19 10-MAR-94 2001 1001 3002 1900.10 10-MAR-94 2007 1003 3005 5160.45 10-MAR-94 2003 1002 3006 1098.16 10-MAR-94 2008 1002 3009 1713.23 10-APR-94 2002 1003 3007 75.75 10-APR-94 2004 1002

Q-15) All customers services by Peel or Motika.

Solution:

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');

Output:

CNUM CNAME ---------- ------------- 2001 Hoffman 2006 Pereira

Q-16) All customers whose names begin with a letter A or G.

Solution:

SQL> select cname from customer where cname like 'G%' or cname like 'A%';

Output:

CNAME ---------------- Giovanne Grass

5

Page 6: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

Q-17) Display all orders except those with 0 or null value in amount field.

Solution:

SQL> select onum,amt,odate,cnum,snum from Orders where amt <> 0 or amt is not null;

Output:

ONUM AMT ODATE CNUM SNUM ------- ---------- --------- ---------- ---------- ------------- 3001 18.9610-MAR-94 2002 1003 3003 767.1910-MAR-94 2001 1001 3002 1900.1010-MAR-94 2007 1004 3005 5160.4510-MAR-94 2003 1002 3009 1713.2310-APR-94 2002 1003 3007 75.7510-APR-94 2004 1002 3008 4723.9510-MAY-94 2006 1007 3010 1309.9510-JUN-94 2004 1002 3011 9891.0010-JUN-94 2006 1003

Q-18) Count the number of salespeople currently listing orders in the order table.

Solution:

SQL> select count(distinct snum) from Orders;

Output:

COUNT(DISTINCT SNUM) ------------------------------------

5

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

Page 7: Oracle Soft Copy

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;

Output:

COUNT(DISTINCTCITY) ----------------------------------

4

7

Page 8: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

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;

Output:

ONUM SNUM COMISSION ------------ ----------- --------------- 3011 1001 1186.92 3005 1002 619.254 3008 1001 566.874 3002 1003 228.012 3009 1003 205.5876 3010 1002 157.194 3006 1002 131.7792 3003 1001 92.0628 3007 1002 9.09 3001 1002 2.2752

Q-24) All combination of salespeople and customers who belong to the same city.

Solution:

SQL> select s.sname, c.cname from salespeople s inner join customer c on s.city=c.city;

Output:

SNAME CNAME --------------- --------------- Peel Hoffman Motika Hoffman Peel Clemens Motika Clemens Serres Liu

Q-25) Names of all customers matched with the salespeople serving them.

Solution:

SQL> select cname, sname from customer inner join salespeople on customer.snum=salespeople.snum;Output:

8

Page 9: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

CNAME SNAME --------------- --------------- Hoffman Peel Liu Serres Grass Serres Giovanne Axelord Pereira Motika Clemens Rifkin

Q-26) List each order with the name of the customer who placed the order.

Solution:

SQL> select onum, orders.snum, cname from orders inner join customer on orders.snum=customer.snum;

Output:

ONUM SNUM CNAME ---------- ---------- --------------- 3003 1001 Hoffman 3008 1001 Hoffman 3011 1001 Hoffman 3001 1002 Liu 3005 1002 Liu 3006 1002 Liu 3010 1002 Liu 3007 1002 Liu 3001 1002 Grass 3005 1002 Grass 3006 1002 Grass

ONUM SNUM CNAME ---------- ---------- --------------- 3010 1002 Grass 3007 1002 Grass 3002 1003 Giovanne 3009 1003 Giovanne

Q-27) Produce a listing of all the customers serviced by salespeople having commission more than 12%.

Solution:

SQL> select cname, customer.snum, sname from customer inner join salespeople on salespeople.snum=customer.snum where comm>0.12;Output:

9

Page 10: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

CNAME SNUM SNAME --------------- ---------- ------------- Liu 1002 Serres Grass 1002 Serres Clemens 1007 Rifkin

Q-28) Calculate the amount of salesperson’s commission on each order with a rating above 100.

Solution:

SQL> select sname, onum, amt, comm*amt "COMMISSION" from salespeople inner join orders on salespeople.snum=orders.snum;

Output:

SNAME ONUM AMT COMMISSION --------------- ---------- ---------- -------------------- Peel 3003 767.19 92.0628 Peel 3008 4723.95 566.874 Peel 3011 9891.00 1186.92 Serres 3001 18.96 2.4648 Serres 3005 5160.45 670.8585 Serres 3006 1098.16 142.7608 Serres 3010 1309.95 170.2935 Serres 3007 75.75 9.8475 Axelord 3002 1900.10 190.01 Axelord 3009 1713.23 171.323

Q-29) Find all the pairs of customers having same rating, each pair with only one occurrence.

Solution:

SQL> select c1.cname, c2.cname, c1.rating from customer c1 inner join customer c2 on c1.rating=c2.rating where c1.cname<c2.cname;

Output: CNAME CNAME RATING --------------- --------------- ---------- Grass Hoffman 100 Hoffman Pereira 100 Grass Pereira 100 Clemens Liu 300

10

Page 11: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

Q-30) Policy is to assign 3 salespersons to each customer, one at each of the three ratings. Display all such combinations.

Solution:

SQL> select c1.cnum, c1.cname, c2.cnum, c2.cname, c3.cnum, c3.cname from customer c1, customer c2, customer c3 where c1.rating=100 and c2.rating=200 and c3.rating=300;

Output:

CNUM CNAME CNUM CNAME CNUM CNAME ---------- -------------------- ---------- --------------- ---------- -------------------- 2001 Hoffman 2002 Giovanne 2003 Liu 2001 Hoffman 2002 Giovanne 2006 Clemens 2004 Grass 2002 Giovanne 2003 Liu 2004 Grass 2002 Giovanne 2006 Clemens 2007 Pereira 2002 Giovanne 2003 Liu

2006 Pereira 2002 Giovanne 2006 Clemens

Q-31) Display all customers located in cities where salesman serves has his customers.

Solution:

SQL> select sname, cname from salespeople inner join customer on salespeople.snum=customer.snum where salespeople.city=customer.city;

Output:

SNAME CNAME --------------- --------------- Peel Hoffman Serres Liu

Q-32) Produce all pairs of orders by a given customer, name that customer and eliminate duplicates.

Solution:

SQL> select cname, orders.cnum, onum from customer inner join orders on orders.cnum=customer.cnum;

Output:

11

Page 12: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

CNAME CNUM ONUM --------------- ---------- -------------- Hoffman 2001 3003 Giovanne 2002 3001 Giovanne 2002 3009 Liu 2003 3005 Grass 2004 3007 Grass 2004 3010 Clemens 2006 3008 Clemens 2006 3011 Pereira 2007 3002

Q-33) Produce name and cities of all customers with the same rating as Hoffman.

Solution:

SQL> select cname, city, rating from customer where rating=(select rating from customer where cname='Hoffman');

Output:

CNAME CITY RATING --------------- -------------- ------------- Hoffman London 100 Grass Berlin 100 Pereira Rome 100

Q-34) All orders credited to the same salesperson who services Hoffman.

Solution:

SQL> select onum, orders.snum from orders inner join customer on orders.cnum=customer.cnum where orders.snum=1001;

Output:

ONUM SNUM ---------- ---------- 3003 1001 3008 1001 3011 1001

Q-35) All orders that are greater than the average for Oct. 4.

12

Page 13: Oracle Soft Copy

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

Page 14: Oracle Soft Copy

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)

Output:

SNUM SUM(AMT) ---------- ---------------- 1007 14614.95

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

Page 15: Oracle Soft Copy

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

Page 16: Oracle Soft Copy

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

Page 17: Oracle Soft Copy

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

Page 18: Oracle Soft Copy

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

Page 19: Oracle Soft Copy

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');

Output:

ONUM AMT ODATE CNUM SNUM ---------- ----------- ----------------- ---------- ---------- 3001 18.96 10-MAR-94 2002 1003 3003 767.19 10-MAR-94 2001 1004 3002 1900.10 10-MAR-94 2007 1003 3009 1713.23 10-APR-94 2002 1003 3007 75.75 10-APR-94 2004 1002 3008 4723.95 10-MAY-94 2006 1007 3010 1309.95 10-JUN-94 2004 1002

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

Page 20: Oracle Soft Copy

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

Page 21: Oracle Soft Copy

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);

View created.

SQL> select * from comp_bonus;

Output: SNUM AMT ODATE ---------- ---------- -------------- 1003 1713.23 10-APR-94 1007 4723.95 10-MAY-94 1002 5160.45 10-MAR-94

1007 9891.00 10-JUN-94

Q-59) Same as above, but bonus will go only to salespeople when they have the highest order at least ten times.

Solution:

SQL> create view comp_bonus2 as select snum from orders where amt in (select max(amt) from orders group by odate) group by snum having count(snum)>=2;

View created.

SQL> select * from comp_bonus2;

Output: SNUM ---------- 1007

Q-60) Produce all the salespeople in London who had at least one customer located there as well. (Use Intersect).

Solution:

21

Page 22: Oracle Soft Copy

ROLL NO-100 SQL QUERIES

SQL> select snum from salespeople where city='London' intersect (select snum from customer where city='London' group by snum having count(snum)>=1);

Output:

SNUM ---------- 1001

22