Lab Assignment 2 DBMS Ganpat University

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

ASSIGNMENT WITH ALL SOLUTIONS Please use accordingly

Create the following Three Tables:

1. Salesman

SNUM SNAME CITY COMMISSION

-------------------------------------------------------- ---------------

1001 PIYUSH LONDON 12%

1002 NIRAJ SURAT 13%

1003 MITI LONDON 11%

1004 RAJESH BARODA 15%

1005 ANAND NEW DELHI 10%

1006 RAM PATAN 10%

1007 LAXMAN BOMBAY 09%

SNUM: A Unique number assign to each salesman.

SNAME: The name of salesman.

CITY: The location of salesman.

COMMISSION: The salesman commission on order.

2. Customer

CNUM CNAME CITY RATING SNUM

-------------------------------------------------------- -----------------------

2001 HARDIK LONDON 100 1001

2002 GITA ROME 200 1003

2003 LAXIT SURAT 200 1002

2004 GOVIND BOMBAY 300 1002

2005 CHANDU LONDON 100 1001

2006 CHAMPAK SURAT 300 1007

2007 PRATIK ROME 100 1004


CNUM: A Unique number assign to each customer.

CNAME: The name of customer.

CITY: The location of customer.

RATING: A level of preference indicator given to this customer.

SNUM: A salesman number assign to this customer.

3. Order

ONUM AMOUNT ODATE CNUM SNUM

-----------------------------------------------------------------------------

3001 18.69 10/03/99 2008 1007

3002 767.19 10/03/99 2001 1001

3003 1900.10 10/03/99 2007 1004

3004 5160.45 10/03/99 2003 1002

3005 1098.25 10/04/99 2008 1007

3006 1713.12 10/04/99 2002 1003

3007 75.75 10/05/99 2004 1002

3008 4723.00 10/05/99 2006 1001

3009 1309.95 10/05/99 2004 1002

3010 9898.87 10/06/99 2006 1001

ONUM: A Unique number assign to each Order.

AMOUNT: Amount of order in Rs.

ODATE: The date of order.

CNUM: The number of customer making the order.

SNUM: The number of salesman credited with the sale.


Solve following request with the help of SQL query:

1. Produce the order no, a mount and date of all orders.

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.

4. List of rating followed by the name of each customer in Surat.

5. List of snum of all salesmen with orders in order table without any duplicates.

6. List of all orders for more than Rs. 1000.

7. List out names and cities of all salesmen in London with commission above 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

10. List all orders taken on October 3rd or 4th or 6th 1997.

11. List all customers whose names begin with a letter 'C'.

12. List all customers whose names begins with letter 'A' to 'G'

13. List all orders with zero or NULL amount.

14. Find out the largest orders of salesman 1002 and 1007.

15. Count all orders of 10-Mar-97.

16. Calculate the total amount ordered.

17. Calculate the average amount ordered.

18. Count the no. of salesmen currently having orders.

19. Find the largest order taken by each salesman on each date.

20. Find the largest order taken by each salesman on 10/03/1997.

21. Count the no. of different non NULL cities in the Customer table.
22. Find out each customer's smallest order.

23. Find out the customer in alphabetical order whose name begins with 'G'

24. Count the no. of salesmen registering orders for each day.

25. List all salesmen with their % of commission.

26. Display the no. of order for each day in the following format: dd-mon-yy.

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.

28. Find the highest rating in each city in the following format:

29. List all customers in descending order of rating.

30. Calculate the total of orders for each day.

31. Show the name of all customers with their salesman's name.

32. List all customers and salesmen who shared a same city.

33. List all orders with the names of their customer and salesman.

34. List all orders by the customers not located in the same city as their salesman.

35. List all customers serviced by salesman with commission above 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 with our duplication.

38. List all customers located in cities where salesman Niraj has customers.

39. Find all pairs of customers served by a single salesman with the salesman's name and no.

40. List all salesmen who are living in the same city with out duplicate rows.

41. Produce the name and city of all the customers with the same rating as Hardik'.

42. Extract all orders of Miti.

43. Extract all orders of Baroda's salesmen.

44. Find all orders of the salesman who services 'Hardik'

45. List all orders that are greater than the average of April 10, 1997
46. Find all orders attributed to salesmen in 'London'.

47. List the commission of all salesmen serving customers in 'London'.

48. Find all customers whose cnum is 1000 above than the snum of Niraj.

49. Count the no. of customers with the rating above than the average of 'Surat'.

50. List all orders of the customer 'Chandresh'.

51. Produce the name and rating of all customers who have above average orders.

52. Find all customers with orders on 3rd Oct. 1997 using correlate sub query.

53. List the name and number of all salesmen who has more than Zero customers.

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.

55. Using correlated sub query find the name and number of all customers with rating equal to
maximum for their city.

56. Select the name and number of all salesmen who have customers their cities.

57. Find all salesmen who have customers with rating > 300 58. List all salesmen

with customers located in their cities.

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'.

61. List all order that has amount greater than at least one of the orders from 6th October 1997.

62. Find all orders with amounts smaller than any amount for a customer in 'Rome'.

63. Find all the customers who have greater rating than every customer in 'Rome'.

64. Select all customers whose rating doesn't match with any rating customer of 'Surat'.

65. List all customers whose ratings are equal to or greater than ANY 'Niraj'

66. Find out which salesman produce largest and smallest orders on each date.

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'
68. Insert a row into salesmen table with the value snum is 100-salesman name is Rakesh, city is
unknown and commission is 14%.

69. Insert a row in to customer table with values London, Pratik a 2005 for the ColumnsCity, name
and number.

70. Create another table London staff having same structure as salesman table.

71. Insert all the rows of salesmen table with city London in the London staff table.

72. Create another table Day totals with two-attribute date and total and insert rows into this table
from order table.

73. Create a duplicate of the salesmen table with a name Multicust. Now delete all the rows from
the salesmen table.

74. Get back all the rows of salesmen table from its duplicate table.

75. Remove all orders from customer Chandresh from the orders

table.

76. Set the ratings of all the customers of Piyush to 400. 77.

Increase the rating of all customers in Rome by 100.

78. Salesman Miti has resigned. Reassign her number to a new salesman Gopal whose city is
Bombay and commission is 10%.

79. Double the commission of all salesmen of London.

80. Set ratings for all customers in London to NULL.

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.

82. Assume that we have a table called smcity. Store the information of all salesmen with the
customers in their home cities into smcity.

83. Create a table Bonus that contains date wise maximum amount of order for all salesmen.

84. Create a table Multicust containing the salesmen with more than one customer.

85. New Delhi office has closed. Remove all customers assigned to salesmen in New Delhi.

86. Delete all salesmen who have at least one customer with a rating of 100 from salesmen table.

87. Delete the salesmen who produce the lowest order for each day.
88. Find the smallest order for each day. Reduce the commission of all salesmen by 2% who
produce this order.

89. Delete all customers with no current orders.

90. Write a command to find out the orders by date.

91. Write a command to add the item-name column to the order table.

92. Create a copy of your order table. Drop the original order table.

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.

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.

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.

96. Create a view called Big orders which stores all orders larger than Rs.4000.

97. Create a view Rate count that gives the count of no. of customers at each rating.

98. Create a view that shows all the customers who have the highest ratings.

99. Create a view that shows all the number of salesman in each city.

100. Create a view that shows all the number of salesmen in each city.

101. Create a view that shows the average and total orders for each salesman after his name and
number.

102. Create a view that shows all the salesmen with multiple customers.

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.

104. Create a view Show name that shows for each order the order no, amount, salesman name
and the customer name.

105. List all orders of salesman 'Rajesh' using Show name View along with his commission.

106. Create view Max sales to store the name and number of salesman, along with the date,
which 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.

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.

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%.

110. Assume that the CURDATE is a constant representing current date. Give a command to
create orders table with CURDATE as a default date.

111. List all salesmen in London who had at least one customer located there as well.

112. List all salesmen in London who didn't have any customer there.
DBMS - Assignment 2

---------------------------------------------------------------------------------------------------------------------------------------

[Salesman]

create table Salesman(Snum char(4),Sname varchar(15),City varchar(12),Commission char(5),PRIMARY


KEY(Snum));

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

SNUM SNAME CITY COMMI

---- --------------- ------------ -----

1001 Piyush London 12

1002 Niraj Surat 13

1003 Miti London 11

1004 Rajesh Baroda 15

1005 Anand New Delhi 10

1006 Ram Patan 10

1007 Laxman Bombay 9

---------------------------------------------------------------------------------------------------------------------------------------
[Customer]

create table Customer(Cnum char(4),Cname varchar(15),City varchar(12),Rating char(5),Snum


char(4),PRIMARY KEY(Cnum),FOREIGN KEY(Snum) REFERENCES Salesman (Snum));

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

insert into Customer values(2008,'Vatsal','Kutch',100,1007);

CNUM CNAME CITY RATIN SNUM

---- --------------- ------------ ----- ----

2001 Hardik London 100 1001

2002 Gita Rome 200 1003

2003 Laxit Surat 200 1002

2004 Govind Bombay 300 1002

2005 Chandu London 100 1001

2006 Champak Surat 300 1007

2007 Pratik Rome 100 1004

2008 Vatsal Kutch 100 1007

---------------------------------------------------------------------------------------------------------------------------------------

[Orders]
create table Orders (Onum char(4),Amount char(10),Odate DATE,Cnum char(4),Snum char(4), PRIMARY
KEY(Onum), FOREIGN KEY(Cnum) REFERENCES Customer(Cnum), FOREIGN KEY(Snum)
REFERENCES Salesman (Snum));

insert into Orders values(3001,18.69, to_date('10-03-1999','DD-MM-YYYY'), 2008, 1007);

insert into Orders values(3002,767.19, to_date('10-03-1999','DD-MM-YYYY'), 2001, 1001);

insert into Orders values(3003,1900.10, to_date('10-03-1999','DD-MM-YYYY'), 2007, 1001);

insert into Orders values(3004,5160.45, to_date('10-03-1999','DD-MM-YYYY'), 2003, 1002);

insert into Orders values(3005,1098.25, to_date('10-04-1999','DD-MM-YYYY'), 2008, 1007);

insert into Orders values(3006,1713.12, to_date('10-04-1999','DD-MM-YYYY'), 2002, 1003);

insert into Orders values(3007,75.75, to_date('10-05-1999','DD-MM-YYYY'), 2004, 1002);

insert into Orders values(3008,4723.00, to_date('10-05-1999','DD-MM-YYYY'), 2006, 1001);

insert into Orders values(3009,1309.95, to_date('10-05-1999','DD-MM-YYYY'), 2004, 1002);

insert into Orders values(3010,9898.87, to_date('10-06-1999','DD-MM-YYYY'), 2006, 1001);

ONUM AMOUNT ODATE CNUM SNUM

---- ---------- --------- ---- ----

3001 18.69 10-MAR-99 2008 1007

3002 767.19 10-MAR-99 2001 1001

3003 1900.1 10-MAR-99 2007 1001

3004 5160.45 10-MAR-99 2003 1002

3005 1098.25 10-APR-99 2008 1007

3006 1713.12 10-APR-99 2002 1003

3007 75.75 10-MAY-99 2004 1002

3008 4723 10-MAY-99 2006 1001

3009 1309.95 10-MAY-99 2004 1002

3010 9898.87 10-JUN-99 2006 1001

---------------------------------------------------------------------------------------------------------------------------------------
1.Produce the order no, amount and date of all orders.

select Onum,Amount,Odate from Orders;

ONUM AMOUNT ODATE

---- ---------- ---------

3001 18.69 10-MAR-99

3002 767.19 10-MAR-99

3003 1900.1 10-MAR-99

3004 5160.45 10-MAR-99

3005 1098.25 10-APR-99

3006 1713.12 10-APR-99

3007 75.75 10-MAY-99

3008 4723 10-MAY-99

3009 1309.95 10-MAY-99

3010 9898.87 10-JUN-99

---------------------------------------------------------------------------------------------------------------------------------------

2.Give all information about all the customer with salesman number 1001.

select * from Customer where Snum=1001;

CNUM CNAME CITY RATIN SNUM

---- --------------- ------------ ----- ----

2001 Hardik London 100 1001

2005 Chandu London 100 1001


---------------------------------------------------------------------------------------------------------------------------------------

3.Display the information in the sequwnce of city,snum,snmae and commission.

select city,sname,snum,commission from salesman;

CITY SNAME SNUM COMMI

------------ --------------- ---- -----

London Piyush 1001 12

Surat Niraj 1002 13

London Miti 1003 11

Baroda rajesh 1004 15

New Delhi Anand 1005 10

Patan Ram 1006 10

Bombay Laxman 1007 9

---------------------------------------------------------------------------------------------------------------------------------------

4.List of rating followed by the name of each customer in surat.

select Cname,rating from Customer where city='Surat';

CNAME RATIN

--------------- -----

Laxit 200

Champak 300

---------------------------------------------------------------------------------------------------------------------------------------
5.List snum of all salesman with order in orders table without any duplicates.

select Distinct Salesman.snum from salesman,orders where salesman.snum=orders.snum;

SNUM

----

1001

1007

1002

1003

---------------------------------------------------------------------------------------------------------------------------------------

6.List of all orders for more than Rs. 1000

select * from orders where amount>1000;

ONUM AMOUNT ODATE CNUM SNUM

---- ---------- --------- ---- ----

3003 1900.1 10-MAR-99 2007 1001

3004 5160.45 10-MAR-99 2003 1002

3005 1098.25 10-APR-99 2008 1007

3006 1713.12 10-APR-99 2002 1003

3008 4723 10-MAY-99 2006 1001

3009 1309.95 10-MAY-99 2004 1002

3010 9898.87 10-JUN-99 2006 1001


---------------------------------------------------------------------------------------------------------------------------------------

7. List out names and cities of all salesmen in London with commission above 10%

select Sname,city from salesman where city='London' AND commission>10;

SNAME CITY

--------------- ------------

Piyush London

Miti London

---------------------------------------------------------------------------------------------------------------------------------------

8.List all customers excluding those with rating <= 100 or they are located in Rome.

select * from customer where NOT(rating<100 OR rating=100 OR city='Rome');

CNUM CNAME CITY RATIN SNUM

---- --------------- ------------ ----- ----

2003 Laxit Surat 200 1002

2004 Govind Bombay 300 1002

2006 Champak Surat 300 1007

---------------------------------------------------------------------------------------------------------------------------------------

9.List all order for more than Rs. 1000 except the orders of snum, 1006 of 10/03/97
Select * from Orders Where Amount>100 AND NOT(Snum=1006 OR Odate='10-Mar-1999');

ONUM AMOUNT ODATE CNUM SNUM

---- ---------- --------- ---- ----

3005 1098.25 10-APR-99 2008 1007

3006 1713.12 10-APR-99 2002 1003

3008 4723 10-MAY-99 2006 1001

3009 1309.95 10-MAY-99 2004 1002

3010 9898.87 10-JUN-99 2006 1001

---------------------------------------------------------------------------------------------------------------------------------------

10.List all orders taken on October 3rd or 4th or 6th 1997.

select * from orders Where odate<>'10-mar-1999' AND odate<>'10-apr-1999' AND odate<>'10-jun-1999';

ONUM AMOUNT ODATE CNUM SNUM

---- ---------- --------- ---- ----

3007 75.75 10-MAY-99 2004 1002

3008 4723 10-MAY-99 2006 1001

3009 1309.95 10-MAY-99 2004 1002.

---------------------------------------------------------------------------------------------------------------------------------------

11.List all customers whose names begin with a letter 'C'.

select * from Customer Where cname like 'C%';


CNUM CNAME CITY RATIN SNUM

---- --------------- ------------ ----- ----

2005 Chandu London 100 1001

2006 Champak Surat 300 1007

---------------------------------------------------------------------------------------------------------------------------------------

12.List all customers whose names begins with letter 'A' to 'G'

select Cname from Customer Where Cname BETWEEN 'A' AND 'H';

CNAME

---------------

Gita

Govind

Chandu

Champak

---------------------------------------------------------------------------------------------------------------------------------------

13.List all orders with zero or NULL amount.

select * from Orders WHERE Amount=0 OR Amount is NULL;

no rows selected

---------------------------------------------------------------------------------------------------------------------------------------
14.Find out the largest orders of salesman 1002 and 1007.

select MAX(Amount) From Orders WHERE Snum=1002 OR Snum=1007;

MAX(AMOUNT

----------

75.75

---------------------------------------------------------------------------------------------------------------------------------------

15.Count all orders of 10-Mar-97.

select count(onum) From Orders WHERE odate='10-Mar-1999';

COUNT(ONUM)

-----------

---------------------------------------------------------------------------------------------------------------------------------------

16.Calculate the total amount ordered.

Select SUM(Amount) From Orders;

SUM(AMOUNT)

-----------

25548.43
---------------------------------------------------------------------------------------------------------------------------------------

17.Calculate the average amount ordered.

Select AVG(Amount) From Orders;

AVG(AMOUNT)

-----------

3193.55375

---------------------------------------------------------------------------------------------------------------------------------------

18.Count the no. of salesmen currently having orders.

Select * From Orders WHERE Amount is Not Null;

ONUM AMOUNT ODATE CNUM SNUM

---- ---------- --------- ---- ----

3002 767.19 10-MAR-99 2001 1001

3003 1900.1 10-MAR-99 2007 1001

3004 5160.45 10-MAR-99 2003 1002

3006 1713.12 10-APR-99 2002 1003

3007 75.75 10-MAY-99 2004 1002

3008 4723 10-MAY-99 2006 1001

3009 1309.95 10-MAY-99 2004 1002

3010 9898.87 10-JUN-99 2006 1001

---------------------------------------------------------------------------------------------------------------------------------------
19.Find the largest order taken by each salesman on each date.

select snum,odate,max(Amount) from Orders Group by Odate,Snum;

SNUM ODATE MAX(AMOUNT

---- --------- ----------

1002 10-MAY-99 75.75

1003 10-APR-99 1713.12

1001 10-MAY-99 4723

1001 10-MAR-99 767.19

1002 10-MAR-99 5160.45

1001 10-JUN-99 9898.87

---------------------------------------------------------------------------------------------------------------------------------------

20.Find the largest order taken by each salesman on 10/03/1997.

select snum,odate,max(Amount) from Orders Group by (snum,odate),Snum having Odate='10-mar-1999';

SNUM ODATE MAX(AMOUNT

---- --------- ----------

1002 10-MAR-99 5160.45

1001 10-MAR-99 767.19

---------------------------------------------------------------------------------------------------------------------------------------
21.Count the no. of different non NULL cities in the Customer table.

select Count(city) from Customer;

COUNT(CITY)

-----------

22.Find out each customer's smallest order.

select snum,odate,min(Amount) from Orders Group by Odate,Snum;

---------------------------------------------------------------------------------------------------------------------------------------

23.Find out the customer in alphabetical order whose name begins with 'G'

select * from customer WHERE cname LIKE 'G%';

CNUM CNAME CITY RATIN SNUM

---- --------------- ------------ ----- ----

2002 Gita Rome 200 1003

2004 Govind Bombay 300 1002

---------------------------------------------------------------------------------------------------------------------------------------

24. Count the no. of salesmen registering orders for each day.

select odate,snum,Count(*) From order_111 GROUP BY odate,snum;


ODATE SNUM COUNT(*)

--------- ---------- ----------

10-APR-99 1003 1

10-MAY-99 1001 2

10-JUN-99 1002 1

10-MAR-99 1001 1

10-MAR-99 1004 1

10-MAR-99 1007 1

10-MAR-99 1002 1

10-APR-99 1007 1

8 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

25. List all salesmen with their % of commission.

select sname,Commission from salesman_111;

SNAME COMMI

-------------------- -----

Daxesh 12%

karan 13%

Mehul 11%

Bhavesh 15%

Aashish 10%

Aanand 10%

Rajesh 9%
7 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

26. Display the no. of order for each day in the following format: dd-mon-yy.

select odate,snum From order_111 GROUP BY odate,snum;

ODATE SNUM

--------- ----------

10-APR-99 1003

10-MAY-99 1001

10-JUN-99 1002

10-MAR-99 1001

10-MAR-99 1004

10-MAR-99 1007

10-MAR-99 1002

10-APR-99 1007

8 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

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.

select onum,snum,amount,amount*0.12 from orders_074 order by snum;


ONUM SNUM AMOUNT AMOUNT*0.12

---------- ---------- ---------- -----------

3002 1001 767 92.04

3008 1001 4723 566.76

3010 1001 9899 1187.88

3009 1002 1310 157.2

3004 1002 5160 619.2

3007 1002 76 9.12

3005 1003 1098 131.76

3003 1004 1900 228

3001 1007 19 2.28

3006 1007 1713 205.56

10 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

29. List all customers in descending order of rating.

select * from customer order by rating desc;

CNUM CNAME CITY RATIN SNUM

---- --------------- ------------ ----- ----

2004 Govind Bombay 300 1002

2006 Champak Surat 300 1007

2003 Laxit Surat 200 1002

2002 Gita Rome 200 1003

2008 Vatsal Kutch 100 1007


2005 Chandu London 100 1001

2007 Pratik Rome 100 1004

2001 Hardik London 100 1001

8 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

30. Calculate the total of orders for each day.

Select 'for',odate,'there are',Count(onum),'orders.'From orders Group by odate;

'FO ODATE 'THEREARE COUNT(ONUM) 'ORDERS

--- ------------------ --------- ----------- -------

for 10-MAY-99 there are 3 orders.

for 10-JUN-99 there are 1 orders.

for 10-APR-99 there are 2 orders.

for 10-MAR-99 there are 4 orders.

---------------------------------------------------------------------------------------------------------------------------------------

31. Show the name of all customers with their salesman's name.

---------------------------------------------------------------------------------------------------------------------------------------

32. List all customers and salesmen who shared a same city.

Select sname,cname from Customer, Salesman where salesman.city=customer.city;

SNAME CNAME

--------------- ---------------
Miti Hardik

Piyush Hardik

Niraj Laxit

Laxman Govind

Miti Chandu

Piyush Chandu

Niraj Champak

---------------------------------------------------------------------------------------------------------------------------------------

33. List all orders with the names of their customer and salesman.

---------------------------------------------------------------------------------------------------------------------------------------

34. List all orders by the customers not located in the same city as their salesman.

Select sname,cname from Customer, Salesman where salesman.city != customer.city;

SNAME CNAME

--------------- ---------------

Piyush Gita

Piyush Laxit

Piyush Govind

Piyush Champak

Piyush Pratik

Piyush Vatsal

Niraj Hardik

Niraj Gita

Niraj Govind

Niraj Chandu
Niraj Pratik

SNAME CNAME

--------------- ---------------

Niraj Vatsal

Miti Gita

Miti Laxit

Miti Govind

Miti Champak

Miti Pratik

Miti Vatsal

Rajesh Hardik

Rajesh Gita

Rajesh Laxit

Rajesh Govind

SNAME CNAME

--------------- ---------------

Rajesh Chandu

Rajesh Champak

Rajesh Pratik

Rajesh Vatsal

Anand Hardik

Anand Gita

Anand Laxit

Anand Govind

Anand Chandu

Anand Champak
Anand Pratik

SNAME CNAME

--------------- ---------------

Anand Vatsal

Ram Hardik

Ram Gita

Ram Laxit

Ram Govind

Ram Chandu

Ram Champak

Ram Pratik

Ram Vatsal

Laxman Hardik

Laxman Gita

SNAME CNAME

--------------- ---------------

Laxman Laxit

Laxman Chandu

Laxman Champak

Laxman Pratik

Laxman Vatsal

49 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

35. List all customers serviced by salesman with commission above 12%.
Select cname,sname,commission from customer ,salesman where commission>12 and
customer.snum=salesman.snum;

CNAME SNAME COMMI

--------------- --------------- -----

Laxit Niraj 13

Govind Niraj 13

Pratik Rajesh 15

3 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

36. Calculate the amount of the salesman commission on each order by customer with rating

above 100.

Select sname,amount*commission from orders,customer,salesman where rating>100 and


salesman.snum=customer.snum and salesman.snum=orders.snum and customer.cnum=orders.cnum;

SNAME AMOUNT*COMMISSION

--------------- -----------------

Niraj 67085.85

Miti 18844.32

Niraj 984.75

Niraj 17029.35

---------------------------------------------------------------------------------------------------------------------------------------

37. Find all pairs of customers having the same rating with our duplication.
select a.cname,b.cname,a.rating from customer a, customer b where a.rating=b.rating and a.cnum!
=b.cnum and a.cnum<b.cnum;

CNAME CNAME RATIN

--------------- --------------- -----

Gita Laxit 200

Hardik Chandu 100

Govind Champak 300

Chandu Pratik 100

Hardik Pratik 100

Pratik Vatsal 100

Chandu Vatsal 100

Hardik Vatsal 100

8 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

38. List all customers located in cities where salesman Niraj has customers.

select cname from customer where city in (select city from customer,orders where
customer.cnum=orders.cnum and orders.snum in (select snum from salesman where sname='Niraj'));

CNAME

---------------

Laxit

Govind

Champak

---------------------------------------------------------------------------------------------------------------------------------------

39. Find all pairs of customers served by a single salesman with the salesman's name and no.
Select customer.cname,salesman.sname,salesman.snum from customer,salesman where snum in (select
snum from customer group by snum having count(snum) > 1);

CNAME

---------------

Hardik

Laxit

Govind

Chandu

Champak

Vatsal

6 rows selected.

---------------------------------------------------------------------------------------------------------------------------------------

40. List all salesmen who are living in the same city with out duplicate rows.

---------------------------------------------------------------------------------------------------------------------------------------

41. Produce the name and city of all the customers with the same rating as Hardik'.

Select cname, city from customer where rating = (select rating from customer where cname = 'Hardik') and
cname != 'Hardik';

CNAME CITY

--------------- ------------

Chandu London

Pratik Rome
Vatsal Kutch

---------------------------------------------------------------------------------------------------------------------------------------

42. Extract all orders of Miti.

Select onum from orders where snum=(select snum from salesman where sname = 'Miti');

ONUM

----

3006

---------------------------------------------------------------------------------------------------------------------------------------

43. Extract all orders of Baroda's salesmen.

Select onum from orders where snum=(select city from salesman where city = 'Baroda');

no rows selected

---------------------------------------------------------------------------------------------------------------------------------------

44. Find all orders of the salesman who services 'Hardik'

Select onum, sname, cname, amount from orders a, salesman b, customer c where a.snum = b.snum and
a.cnum = c.cnum and a.snum = ( select snum from orders where cnum = ( select cnum from customer
where cname = 'Hardik'));

ONUM SNAME CNAME AMOUNT

---- --------------- --------------- ----------

3002 Piyush Hardik 767.19

3008 Piyush Champak 4723


3010 Piyush Champak 9898.87

3003 Piyush Pratik 1900.1

---------------------------------------------------------------------------------------------------------------------------------------

45. List all orders that are greater than the average of April 10, 1999

Select * from orders where amount > ( select avg(amount) from orders where odate = '10-Apr-1999');

ONUM AMOUNT ODATE CNUM SNUM

---- ---------- ------------------ ---- ----

3003 1900.1 10-MAR-99 2007 1001

3004 5160.45 10-MAR-99 2003 1002

3006 1713.12 10-APR-99 2002 1003

3008 4723 10-MAY-99 2006 1001

3010 9898.87 10-JUN-99 2006 1001

---------------------------------------------------------------------------------------------------------------------------------------

46. Find all orders attributed to salesmen in 'London'.

Select snum, cnum from orders where cnum in (select cnum from customer where city = 'London');

SNUM CNUM

---- ----

1001 2001

---------------------------------------------------------------------------------------------------------------------------------------

47. List the commission of all salesmen serving customers in 'London'.

Select commission from salesman where snum in (select snum from customer where city = 'London');
COMMI

-----

12

---------------------------------------------------------------------------------------------------------------------------------------

48. Find all customers whose cnum is 1000 above than the snum of Niraj.

Select cnum, cname from customer where cnum > ( select snum+1000 from salesman where sname =
'Niraj');

CNUM CNAME

---- ---------------

2003 Laxit

2004 Govind

2005 Chandu

2006 Champak

2007 Pratik

2008 Vatsal

---------------------------------------------------------------------------------------------------------------------------------------

49. Count the no. of customers with the rating above than the average of 'Surat'.

Select cnum, rating from customer where rating > ( select avg(rating) from customer where city = 'Surat');

CNUM RATIN

---- -----

2004 300
2006 300

---------------------------------------------------------------------------------------------------------------------------------------

50. List all orders of the customer 'Chandresh'.

Select onum, odate from orders where cnum = ( select cnum from customer where cname = ‘Chandresh’);

No Rows Selected.

---------------------------------------------------------------------------------------------------------------------------------------

51. Produce the name and rating of all customers who have above average orders.

Select max(b.cname), max(b.rating), a.cnum from orders a, customer b where a.cnum = b.cnum group by
a.cnum having count(a.cnum) > ( select avg(count(cnum)) from orders group by cnum);

MAX(B.CNAME) MAX(B CNUM

--------------- ----- ----

Vatsal 100 2008

Govind 300 2004

Champak 300 2006

---------------------------------------------------------------------------------------------------------------------------------------

52. Find all customers with orders on 3rd Oct. 1997 using correlate sub query.

Select cname from customer a, orders b where a.cnum = b.cnum and odate = '03-OCT-97';

No rows selected

---------------------------------------------------------------------------------------------------------------------------------------
53. List the name and number of all salesmen who has more than Zero customers.

Select sname, snum from salesman where snum in ( select snum from customer group by snum having
count(snum) > 0 );

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.

Select odate, sum(amount) from orders a group by odate having sum(amount) > ( select max(amount)
from orders b where a.odate = b.odate group by odate);

ODATE SUM(AMOUNT)

------------------ -----------

10-MAY-99 6108.7

10-APR-99 2811.37

10-MAR-99 7846.43

---------------------------------------------------------------------------------------------------------------------------------------

55. Using correlated sub query find the name and number of all customers with rating equal to

maximum for their city.


select a.cnum, a.cname from customer a where a.rating = ( select max(rating) from customer b where
a.city = b.city);

CNUM CNAME

---- ---------------

2001 Hardik

2002 Gita

2004 Govind

2005 Chandu

2006 Champak

2008 Vatsal

---------------------------------------------------------------------------------------------------------------------------------------

56. Select the name and number of all salesmen who have customers their cities.

Select cname from customer where cname in ( select cname from customer a, salesman b where a.city =
b.city and a.snum != b.snum );

CNAME

---------------

Hardik

Govind

Chandu

Champak

---------------------------------------------------------------------------------------------------------------------------------------

57. Find all salesmen who have customers with rating > 300
Select a.snum from salesman a, customer b where b.rating = 300 and a.snum = b.snum;

SNUM

----

1002

1007

---------------------------------------------------------------------------------------------------------------------------------------

58. List all salesmen with customers located in their cities.

Select sname

from salespeople

where snum in ( select snum from cust

where salespeople.city = cust.city and

salespeople.snum = cust.snum);

---------------------------------------------------------------------------------------------------------------------------------------

59. Find all salesmen for whom there are customers that follow them alphabetical order.

Select sname

from salespeople

where sname < any ( select cname

from cust

where salespeople.snum = cust.snum);

---------------------------------------------------------------------------------------------------------------------------------------

60. Find all customers having rating greater than any customer in 'Rome'.

Select a.cname
from cust a

where city = 'Rome' and

rating > ( select max(rating)

from cust

where city != 'Rome');

---------------------------------------------------------------------------------------------------------------------------------------

You might also like