Adbms SQL Queries Sem-III
Adbms SQL Queries Sem-III
1. Salesman:
2. Customer:
3. Order:-
salesman table:
query: create table salesman(snum number(4), sname varchar2(10), city varchar2(10), commission
number(2));
customer table:
query: create table customer(cnum number(4), cname varchar2(10), city varchar2(10), rating number(3),
snum number(4));
orders table:
query: create table orders(onum number(4), amount number(6,2), odate date, cnum number(4), snum
number(4));
Note: Please Do not use ‘order’ as table name. You can use Orders or order1 etc..
Salesman Table:
Method 1: (If you want to insert data like data entry form then use this method)
now Enter Values. If you want to execute same query or previous query again the just type / and press
enter key.
Method 2: (If you want add data in all fields then use this method)
insert into salesman values (1001,’PIYUSH’,’LONDON’,12);
insert into salesman values (1002,’NIRAJ’,’SURAT’, 13);
insert into salesman values (1003,’MITI’,’LONDON’,11);
insert into salesman values (1004,’RAJESH’,’BARODA’,15);
insert into salesman values (1005,’ANAND’,’NEW DELHI’,10);
insert into salesman values (1006,’RAM’,’PATAN’,10);
insert into salesman values (1007,’LAXMAN’,’BOMBAY’,09);
Method 3: (If you want to add data in selected fields then use this method)
Customer Table:
Method 1:
insert into customer values(&cnum,'&cname','&city',&rating,&snum);
Method 2:
insert into customer values(2001,’HARDIK’,’LONDON’,100,1001);
insert into customer values(2002,’GITA’,’ROME’,200,1003);
insert into customer values(2003,’LAXIT’,’SURAT’,200,1002);
insert into customer values(2004,’GOVIND’,’BOMBAY’,300,1002);
insert into customer values(2005,’CHANDU’,’LONDON’,100,1001);
insert into customer values(2006,’CHAMPAK’,’SURAT‘,300,1007);
insert into customer values(2007,’PRATIK’,’ROME’,100,1004);
Method 3:
insert into customer(cnum,cname,city,rating,snum)values(2001,’HARDIK’,’LONDON’,100,1001);
Orders Table:
Method 1:
insert into orders values(&onum,&amount,'&odate',&cnum,&snum);
Method 2:
insert into orders values(3001,18.69,’10/03/99’,2008,1007);
insert into orders values(3002,767.19,’10/03/99’, 2001,1001);
insert into orders values(3003,1900.10,’10/03/99’,2007,1004);
insert into orders values(3004,5160.45,’10/03/99’,2003,1002);
insert into orders values(3005,1098.25,’10/04/99’,2008,1007);
insert into orders values(3006,1713.12,‘10/04/99’,2002,1003);
insert into orders values(3007,75.75,’10/05/99’,2004,1002);
insert into orders values(3008,4723.00,’10/05/99’,2006,1001);
insert into orders values(3009,1309.95,10/05/99,2004,1002);
insert into orders values(3010,9898.87,10/06/99,2006,1001);
Method 3:
insert into orders(onum,amount,odate,cnum,snum)values(3001,18.69,’10/03/99’,2008,1007);
10 rows selected.
==================================================================
2. Give all the information about all the customers with salesman number 1001.
==================================================================
3. Display the information in the sequence of city, sname, snum, and Commission.
7 rows selected.
==================================================================
CNAME RATING
---------- ----------
laxit 200
champak 300
==================================================================
5. List of snum of all salesmen with orders in order table without an duplicates.
SNUM
----------
1001
1002
1003
1004
1007
==================================================================
7 rows selected.
==================================================================
7. List out names and cities of all salesmen in London with commission above 10%
SQL> select sname, city, commission from salesman where city='london' and commission>10;
8. List all customers excluding those with rating <= 100 or they are located in Rome.
==================================================================
9. List all order for more than Rs. 1000 except the orders of snum,1006 of 10/03/97
SQL> select * from orders where amount>1000 and not(snum=1006 and odate='10-mar-99');
10. List all orders taken on October 3rd or 4th or 6th 1997.
no rows selected
or
no rows selected
==================================================================
11. List all customers whose names begins with a letter 'C'.
12. List all customers whose names begins with letter 'A' to 'G'
SQL> select * from customer where cname like 'a%' or cname like 'b%' or cname like 'c%' or
cname like 'd%' or cname like 'e%' or cname like 'f%' or cname like 'g%';
SQL> select * from customer where substr(cname,1,1) between 'a' and 'g';
no rows selected
==================================================================
14. Find out the largest orders of salesman 1002 and 1007.
LARGEST_ORDER
-------------
5160.45
OR
SQL> select snum, max(amount) from orders where snum in(1002,1007) group by snum
SNUM MAX(AMOUNT)
---------- -----------
1002 5160.45
1007 1098.25
OR
SQL> select snum, max(amount) max from orders group by snum having snum=1002 or snum=1007;
SNUM MAX
---------- ----------
1002 5160.45
1007 1098.25
OR
SQL> select snum,max(amount) max from orders where snum=1002 or snum=1007 grop by snum;
SNUM MAX
---------- ----------
1002 5160.45
1007 1098.25
==================================================================
SQL> select count(odate) as "No. of Orders on 10-MAR-99" from orders where odate='10-mar-99';
SNUM COUNT(ONUM)
---------- -----------
1001 3
1002 3
1003 1
1004 1
1007 2
SQL> select count(distinct snum) as "No. of Salesman having Orders" from orders;
19. Find the largest order taken by each salesman on each date.
ODATE MAX(AMOUNT)
--------- -----------
10-MAR-99 5160.45
10-APR-99 1713.12
10-MAY-99 4723
10-JUN-99 9898.87
SQL> select odate, snum, max(amount) from orders where odate='10-mar-99' group by odate, snum;
SQL> select count(distinct city) as "No. of Non Null Cities" from customer where city is not null;
SQL> select cnum, min(amount) as "Smallest Order" from orders group by cnum;
23. Find out the customer in alphabetical order whose name begins with 'G'
SQL> select * from customer where cname like 'g%' order by cname;
24. Count the no. of salesmen registering orders for each day.
ODATE COUNT(SNUM)
--------- -----------
10-MAR-99 4
10-APR-99 2
10-MAY-99 3
10-JUN-99 1
==================================================================
SQL> select snum, sname, city, commission || '%' as commission from salesman;
SQL> select 'for ' || odate || ' There are ' || count(*) || ' Orders' from orders group by odate;
'FOR'||ODATE||'THEREARE'||COUNT(amount)||'ORDERS'
-----------------------------------------------------------------------
for 10-MAR-99 There are 4 Orders
for 10-APR-99 There are 2 Orders
for 10-MAY-99 There are 3 Orders
for 10-JUN-99 There are 1 Orders
==================================================================
27. Assume each salesperson has a 12% commission. Write a query on the order table that will produce
the order
number, salesman no and amount of commission for that order.
10 rows selected.
==================================================================
28. Find the highest rating in each city in the following format:
SQL> select 'highest rating' || max(rating) || ' is in the city' || city || 'lowest rating' || min(rating)
|| 'is in the city' || city from customer group by city;
'HIGHESTRATING'||MAX(RATING)||'ISINTHECITY'||CITY||'LOWESTRATING'||MIN(RATING)||
--------------------------------------------------------------------------------
==================================================================
ODATE SUM(AMOUNT)
--------- -----------
10-MAR-99 7846.43
10-APR-99 2811.37
10-MAY-99 6108.7
10-JUN-99 9898.87
==================================================================
31. Show the name of all customers with their salesman's name.
CNAME SNAME
---------- -------
hardik piyush
chandu piyush
laxit niraj
govind niraj
gita miti
pratik rajesh
champak laxman
7 rows selected.
==================================================================
32. List all customers and salesmen who shared a same city.
SQL> select cname, sname, c.city from customer c, salesman s where c.city=s.city;
33. List all orders with the names of their customer and salesman.
SQL> select onum, amount, odate, cname, sname from orders o, customer c, salesman s where
o.cnum=c.cnum
and o.snum=s.snum order by o.onum;
8 rows selected.
==================================================================
34. List all orders by the customers not located in the same city as their salesman.
SQL> select distinct o.* from orders o, customer c, salesman s where c.city<>s.city and
c.snum=s.snum and c.cnum=o.cnum order by o.onum;
35. List all customers serviced by salesman with commission above 12%.
SQL> select c.* from customer c, salesman s where c.snum=s.snum and s.commission
>12;
36. Calculate the amount of the salesman commission on each order by customer with rating above 100.
37. Find all pairs of customers having the same rating without duplication.
SQL> select a.cname, b.cname from customer a, customer b where a.rating=b.rating and
b.cnum>a.cnum;
38. List all customers located in cities where salesman Niraj has customers.
SQL> select * from customer where city in(select distinct city from customer where
snum=(select snum from salesman where sname='niraj'));
39. find all pairs of customers served by a single salesman with the salesman's name and no.
SQL> select c.cname, c1.cname, c.snum, s.sname from salesman s, customer c, customer c1
where c.snum=c1.snum and c.cnum>c1.cnum and c.snum=s.snum;
40. List all salesmen who are living in the same city with out duplicate rows.
SQL> select distinct s.* from salesman s, salesman s1 where s.snum<>s1.snum and s.city=s1.city;
41. Produce the name and city of all the customers with the same rating as Hardik'.
SQL> select cname, city from customer where rating=(select rating from customer where
cname='hardik');
CNAME CITY
---------- ----------
hardik london
chandu london
pratik rome
==================================================================
SQL> select * from orders where snum=(select snum from salesman where sname='miti');
SQL> select * from orders where snum=(select snum from salesman where city='baroda');
SQL> select * from orders where snum=(select snum from customer where cname='hardik');
SQL> select o.* from orders o, customer c where c.snum=o.snum and c.cname='hardik';
45. List all orders that are greater than the average of April 10, 1997
SQL>select * from orders where amount > (select avg(amount) from orders where odate='10-apr-97')
no rows selected.
SQL> select * from orders where amount > (select avg(amount) from orders where odate='10-apr-99');
SQL> select * from orders where snum in (select snum from salesman where city='london');
SQL>select cname, sname, commission from customer c, salesman s where c.snum=s.snum and
c.city='london';
48. Find all customers whose cnum is 1000 above than the snum of Niraj.
SQL> select * from customer where cnum > (select snum+1000 from salesman where sname='niraj');
49. Count the no. of customers with the rating above than the average of 'Surat'.
No. of Customers
----------------
2
==================================================================
SQL> select * from orders where cnum=(select cnum from customer where cname='chandresh');
no rows selected
SQL>select * from orders where cnum in (select cnum from customer where cname='chandresh');
no rows selected
SQL>select * from orders where cnum in (select cnum from customer where cname='champak');
51. Produce the name and rating of all customers who have above average orders.
SQL> select cname, rating from customer where cnum in (select cnum from orders where amount>(select
avg(amount) from orders));
CNAME RATING
---------- ----------
laxit 200
champak 300
==================================================================
52. Find all customers with orders on 3rd Oct., 1997 using correlate sub query.
SQL>select * from customer where cnum in (select cnum from orders where odate='3-oct-97');
no rows selected
==================================================================
53. List the name and number of all salesmen who has more than Zero customer.
SQL> select sname, snum from salesman where snum in(select c.snum from customer c, salesman s
where
c.snum=s.snum);
SNAME SNUM
---------- ----------
piyush 1001
niraj 1002
miti 1003
rajesh 1004
laxman 1007
SQL> select sname, snum from salesman where snum in(select distinct snum from customer);
SNAME SNUM
---------- ----------
piyush 1001
niraj 1002
miti 1003
rajesh 1004
laxman 1007
54. Calculate the total amount ordered on each day eliminating the days where the total amount was not
at least Rs. 2000 above the maximum amount of that day.
SQL>select odate, sum(amount)"Sales Per Day" from orders group by odate having sum(amount)>2000;
7 rows selected.
==================================================================
56. Select the name and number of all salesmen who have customers their cities.
SQL>select distinct s.snum, s.sname from salesman s, customer c where s.city=c.city and s.snum=c.snum
SNUM SNAME
---------- ----------
1001 piyush
1002 niraj
==================================================================
57. find all salesmen who have customers with rating > 300
SQL>select * from salesman where snum in(select snum from customer where rating>300);
no rows selected
SQL>select distinct s.* from salesman s, customer c where s.snum=c.snum and c.rating>300;
no rows selected
SQL>select * from salesman where snum in(select snum from customer where rating=300);
SQL>select distinct s.* from salesman s, customer c where s.snum=c.snum and c.rating=300;
59. Find all salesmen for whom there are customers that follow them alphabetical order.
60. Find all customers having rating greater than any customer in 'Rome'.
SQL> select * from customer where rating > any(select rating from customer where city='rome');
61. List all order that has amount grater than at least one of the orders from 6th October, 1997.
SQL> select * from orders where amount > any(select amount from orders where odate='6-oct-99');
no rows selected
==================================================================
62. Find all orders with amounts smaller than any amount for a customer in 'Rome'.
SQL> select * from orders where amount < any(select amount from orders o, customer c where
o.cnum=c.cnum and c.city='rome');
SQL>select * from orders where amount < any(select amount from orders where cnum in(select cnum
from customer where city='rome'));
6 rows selected.
==================================================================
63. Find all the customers who have greater rating than every customer in 'Rome'.
SQL> select * from customer where rating > all(select rating from customer where city='rome');
64. Select all customers whose rating doesn't match with any rating customer of 'Surat'.
SQL> select * from customer where rating <> all (select rating from customer where city='surat');
SQL>select * from customer where rating not in (select rating from customer where city='surat');
65. List all customers whose ratings are equal to or greater than ANY 'Niraj'
SQL> select * from customer where rating>=any(select rating from customer c, salesman s
where sname='niraj' and s.snum=c.snum);
66. Find out which salesman produce largest and smallest orders on each date.
SQL> select oa.odate, oa.snum, oa.amount maximun, ob.snum, ob.amount minimum from orders oa,
orders
ob where (oa.amount, ob.amount) in (select max(amount), min(amount) from orders group by odate);
67. Create a union of two queries that shows the names, cities and ratings of all customers.
Those with rating of >=200 should display 'HIGH RATING' and those with < 200 should display 'LOW
RATING'
SQL>select cname, city, rating || ' High Rating' as rating from customer where rating >=200 union select
cname, city, rating||' Low Rating' as rating from customer where rating<200;
CNAME CITY RATING
---------- ---------- ------------------
champak surat 300 High Rating
chandu london 100 Low Rating
gita rome 200 High Rating
govind bombay 300 High Rating
hardik london 100 Low Rating
laxit surat 200 High Rating
pratik rome 100 Low Rating
7 rows selected.
==================================================================
68. Insert a row into salesmen table with the values snum is 100 salesman name is Rakesh,
city is unknown and commission is 14%.
1 row created.
1 row created.
8 rows selected.
==================================================================
69. Insert a row in to customer table with values London, Pratik a 2005 for the columns city, name and
number.
1 row created.
8 rows selected.
==================================================================
70. Create another table London staff having same structure as salesman table.
Table created.
SQL> create table londonstaff as select * from salesman where snum is null;
Table created.
71. Insert all the rows of salesmen table with city London in the London staff table.
2 rows created.
Table created.
ODATE AMOUNT
--------- ----------
10-MAR-99 18.69
10-MAR-99 767.19
10-MAR-99 1900.1
10-MAR-99 5160.45
10-APR-99 1098.25
10-APR-99 1713.12
10-MAY-99 75.75
10-MAY-99 4723
10-MAY-99 1309.95
10-JUN-99 9898.87
10 rows selected.
==================================================================
73. Create a duplicate of the salesmen table with a name Multicust. Now delete all the rows from the
salesmen table.
Table Created.
Table created.
8 rows selected.
Delete Rows:
8 rows deleted.
8 rows deleted.
no rows selected
==================================================================
74. Get back all the rows of salesmen table from its duplicate table.
8 rows created.
8 rows selected.
==================================================================
75. Remove all orders from customer Chandresh from the orders table.
SQL>delete from orders where cnum in (select cnum from customer where cname='chandresh');
0 rows deleted.
==================================================================
SQL> update customer set rating=400 where snum in(select snum from salesman where
sname='piyush');
2 rows updated.
8 rows selected.
8 rows selected.
==================================================================
2 rows updated.
8 rows selected.
==================================================================
78. Salesman Miti has resigned. Reassign her number to a new salesman Gopal whose city is Bombay and
commission is 10%.
1 row updated.
8 rows selected.
==================================================================
1 row updated.
8 rows selected.
==================================================================
3 rows updated.
8 rows selected.
==================================================================
81. Suppose we have a table called sales Manager with the same definition as Salesmen table.
Company decides to promote salesmen having total order more than 5000 to Sales Manager.
Fill up the Sales Manager table.
SQL> create table salesmanager as select * from salesman where snum is null;
Table created.
SQL> insert into salesmanager select * from salesman where snum in(select snum from orders group by
snum having sum(amount)>5000);
2 rows created.
82. Assume that we have a table called smcity. Store the information of all salesmen
with the customers in their home cities into smcity.
Table created.
Table created.
83. Create a table Bonus that contains date wise maximum amount of order for all salesmen.
SQL> create table bonus (odate,amount) as select odate,max(amount) from orders group by odate;
Table created.
ODATE AMOUNT
--------- ----------
10-MAR-99 5160.45
10-APR-99 1713.12
10-MAY-99 4723
10-JUN-99 9898.87
SQL> create table bonus as select * from orders where amount in(select max(amount) from orders group
by odate);
Table created.
84. Create a table Multicust containing the salesmen with more than one customer.
SQL> create table multicust1 as select * from salesman where snum in (select c.snum from customer c,
customer d where c.snum=d.snum and not c.cnum=d.cnum);
Table created.
SQL> create table multicust2 as select * from salesman where snum in (select snum from customer
group by snum having count(*)>1);
Table created.
85. New Delhi office has closed. Remove all customers assigned to salesmen in New Delhi.
SQL> delete from customer where snum in (select snum from salesman where city='new delhi');
0 rows deleted.
==================================================================
86. Delete all salesmen who have at least one customer with a rating of 100 from salesmen table.
SQL> delete from salesman where snum in(select snum from customer where rating=100);
2 rows deleted.
6 rows selected.
==================================================================
87. Delete the salesmen who produce the lowest order for each day.
SQL> delete from salesman where snum in(select snum from orders where amount in(select min(amount)
from orders group by odate));
2 rows deleted.
88. Find the smallest order for each day. Reduce the commission of all salesmen by 2% who produce this
order.
SQL>select snum, sname, commission-(commission*0.02) "Reduce 2%" from salesman where snum In
(select snum from orders where amount In(select min(amount) from orders group by odate));
SQL> delete from customer where cnum NOT IN(select cnum from orders);
2 rows deleted.
SQL> delete customer c where not exists(select * from orders o where c.cnum=o.cnum);
2 rows deleted.
6 rows selected.
==================================================================
10 rows selected.
==================================================================
Table altered.
92. Create a copy of your order table. Drop the original order table.
Table created.
Table dropped.
==================================================================
93. Write a command to create the order table so that all onum values as well as all combinations
of cnum and snum are different from one another and so that NULL values are excluded from the date
field.
Table Created.
==================================================================
94. Write a command to create the salesmen table so that the default commission is 10% with no NULL
permitted, snum is the primary key and all names contain alphabets only.
Table created.
8 rows created.
==================================================================
95. Give the commands to create our sample tables (salesmen, customer, orders) with
all the necessary constraints like PRIMARY KEY, NOT NULL UNIQUE, FOREING KEY.
Salesman:
Table created.
Customer:
SQL> create table c_customer(cnum number(4) primary key,cname varchar2(10) NOT NULL,city
char(10),ra
ting number(4),snum number(4), FOREIGN KEY(snum) REFERENCES s_salesman);
Table created.
Order:
SQL> create table o_order(onum number(4) primary key,amount number(7,2),odate date NOT
NULL,cnum num
ber(4) REFERENCES c_customer,snum number(4) REFERENCES s_salesman);
Table created.
96. Create a view called Big orders which stores all orders larger than Rs.4000.
View created.
97. Create a view Rate count that gives the count of no. of customers at each rating.
SQL> create view rate_count(rating,cnum) as select rating,count(cnum) from customer group by rating;
View created.
RATING CNUM
---------- ----------
100 2
200 2
300 2
==================================================================
98. Create a view that shows all the customers who have the highest ratings.
SQL> create view high_rate as select * from customer where rating IN(select max(rating)
from customer);
View created.
99. Create a view that shows all the number of salesman in each city.
SQL> create view tot_sales as select city, count(snum) "Total Salesman" from salesman group by city;
View created.
7 rows selected.
==================================================================
100. Create a view that shows all the number of salesmen in each city.
SQL> create view tot_sales as select city, count(snum) "Total Salesman" from salesman group by city;
View created.
7 rows selected.
==================================================================
101. Create a view that shows the average and total orders for each salesmen after his name and number.
View created.
102. Create a view that shows all the salesmen with multiple customers.
SQL> create view multi_cust as select s.* from salesman s,customer c1,customer c2 where s.snum=c1.sn
um AND c1.cnum>c2.cnum AND c1.snum=c2.snum;
View created.
SQL> create view multi_cust1 as select * from salesman where snum in(select c1.snum from customer c1,
customer c2 where c1.snum=c2.snum and c1.cnum <> c2.cnum);
View created.
103. Create a view to keep track of the total no of customers ordering, no of salesmen taking orders,
the no of orders, the average amount ordered, and the total amount ordered for each day.
SQL> create view alltrack as select odate, count(distinct cnum) "No of Customer", count(distinct snu
m) "No of Salesman", count(onum) "No of Orders", avg(amount) "Average",sum(amount) "Total Amount"
fr
om orders group by odate;
View created.
104. Create a view Show name that shows for each order the order no, amount, salesman name and the
customer name.
View created.
8 rows selected.
==================================================================
105. List all orders of salesman 'Rajesh' using Show name View along with his commission.
SQL>create view shnameview as select o.*, sname,commission from salesman s, orders o where
s.snum=o.snum and s.sname='rajesh';
View created.
106. Create a view Max sales to store the name and number of salesman, along with the date,
who have the highest order on any given date.
107. Using above view, find out the name and number of salesman who have the highest order at
least two times. Store the result in another view.
SQL> create view high_orders as select sname, snum from max_sales where
snum in(select snum from orders where amount in(select max(amount) from orders group by odate)
group by snum having count(snum)>=2);
View created.
SNAME SNUM
---------- ----------
piyush 1001
piyush 1001
==================================================================
108. Create a view Same city that shows the no and name and city of the customers along with the
city of the salesman serving them.
SQL> create view same_city as select c.cnum,c.cname,c.city,s.city Sman_City from customer c, Sal
esman s where s.snum=c.snum;
View created.
6 rows selected.
==================================================================
109. Create a view Commission of salesmen table to include only snum and commission field so that
through. this view someone can enter or change the commission but only to values between 10% and
20%.
SQL> create view smancomm as (select snum,commission from salesman where commission between 10
and 20);
View created.
SNUM COMMISSION
---------- ----------
1001 12
1002 13
1003 11
1004 15
1005 10
1006 10
100 14
7 rows selected.
==================================================================
110. Assume that the CURDATE is a constant representing current date. Give a command to create orders
table with CURDATE as a default date.
SQL> create table orders_curdt(onum number(4), amount NUMBER(6,2), odate DATE DEFAULT SYSDATE,
cnum NUMBER(4),
snum NUMBER(4));
Table created.
==================================================================
111. List all salesmen in London who had at least one customer located there as well.
SQL> select * from salesman where city='london' and snum in(select snum from customer where city='lo
ndon');
112. List all salesmen in London who didn't have any customer there.
SQL>select * from salesman where city='london' and snum not in(select snum from customer where
city='london');