0% found this document useful (0 votes)
18 views48 pages

Lab Manual Muni Babu Practice SQL Queries 1

Uploaded by

r190867
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views48 pages

Lab Manual Muni Babu Practice SQL Queries 1

Uploaded by

r190867
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Course

Course code Course name L-T-P Credits


Category

Database Management Systems


CS2282 PCC 0-0-3 1.5
Laboratory

Course Objectives:

1. Analyze the problem and identify the Entities and Relationships, keys for given database.
2. Design, develop and query a database.
3. Able to construct queries and maintain a simple database using MySQL.
4. Normalization of data present in database tables.
5. Develop triggers programs using PL/SQL.

List of Experiments:

1. Designing the Database through Identifying Entities, Relationship Attributes.

MySQL

1. Queries to facilitate acquaintance of Built-In Functions, String Functions, Numeric Functions,


(Annexure – A, B)
2. Queries to facilitate acquaintance of Date Functions and Conversion Functions. Annexure C, D
E)
3. Queries for Creating, Dropping, and Altering Tables
4. Queries using operators in SQL
5. Queries to Retrieve and Change Data: Select, Insert, Delete, and Update
6. Queries using Group By, Order By, and Having Clauses
7. Queries on Controlling Data: Commit, Rollback, and Save point
8. Queries for creating Views, and Constraints
9. Queries on Joins ( Outer and Inner joins)
10. Queries on Correlated Sub-Queries

PL/SQL

1. Write a PL/SQL Code using Basic Variable, Anchored Declarations, and Usage of Assignment
Operation
2. Write a PL/SQL block using SQL and Control Structures in PL/SQL
3. Write a PL/SQL Code using Cursors, Exceptions and Composite Data Types
4. Write a PL/SQL Code using Procedures, Functions, and Packages FORMS

DBMS LAB MANUAL – prepared by M. Muni Babu Page 1


Course Outcomes:

After completing this course the student must demonstrate the knowledge and ability to:

CO 1 Identify the entities, attributes, relationships, keys for given database.

CO 2 Design a database schema for given problem.

CO 3 Formulate queries using MySQL DML, DDL commands.

CO 4 Formulate SQL queries using constraints and set comparison operators.

CO 5 Apply the normalization techniques for development of application software to


realistic problems.

CO 6 Develop PL/SQL programs using triggers, procedures

CO 7 Ability to design and implement given case study.

Course Nature Practical

Assessment Method

Assessment Tool Experiments Record Viva-Voce/ Total


Quiz/MCQ/Lab project

Weightage (%) 25% 5% 10% 40%

End Semester Examination weightage (%) 60%

******************************************************************************

DBMS LAB MANUAL – prepared by M. Muni Babu Page 2


RAJIV GANDHI UNIVERSITY KNOWLEDGE AND TECHNOLOGIES
IIIT R K VALLEY, IDUPULAPAYA – AP
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

NAME OF THE LABORATORY: DATABASE MANAGEMENT SYSTEMS (DBMS LAB)

LAB MANUAL PREPARED BY: M. MUNI BABU

Sr. Topic Page No


No.
1 Introduction to 2-14
MySQL
2 Creating of Bank 15-15
Database
3 Creating of Bank 15-15
Database Tables
4 Inserting Data 16-17
into Bank Database
5 Practice Questions 18-18
6 Practice Questions 19-19
- Answers
7 Assignment 20-20
Questions
8 Dump Database 21- 27
9 GROUP BY and
HAVING Clause in
SQL

8 Sub-Queries 28-47
(ANY, SOME, ALL,IN, NOT
IN, AND, OR, EXISTS,
NOT EXISTS, UNION,
INTERSECTION, MINUS)

9 Nested Queries
10 Joining 48-56
(Inner,
Left Outer Join or Left
Join,
Right Outer Join or
Right Join,
Full Outer Join,
Cross Join,

DBMS LAB MANUAL – prepared by M. Muni Babu Page 3


Natural Join,
Self Join)
11 Miscellaneous
(DATE, TIME DATETIME,
TIMEZONE, CLOB, BLOB)

12 Cursors
(Statement Level, Row
Level
%Found, %not found,)
13 Triggers
(BEFORE, AFTER –
INSERT, UPDATE, DELETE)

14 Functions
15 Procedures

16 Packages
17 Backup
tools/utilities
18 MySQL Database
Connection
19 Forms using PHP
HTML
20 Reports
21 PHP MySQL CRUD
Application

DBMS LAB MANUAL – prepared by M. Muni Babu Page 4


 CREATE DATABASE WITH YOUR ID (EX: DBSQLR151001)

TABLE REQUIRED:

Salespeople(snum, sname, city, comm.);


Cust(cnum, cname, city, rating, snum);
Orders(onum, amt, odate, cnum, snum);

SQL QUERIES

TABLE SALESPEOPLE

SNUM SNAME CITY COMM


1001 Peel London .12
1002 Serres San Jose .13
1004 Motika London .11
1007 Rafkin Barcelona .15
1003 Axelrod New york .1

TABLE CUST

CNUM CNAME CITY RATING SNUM


2001 Hoffman London 100 1001
2002 Giovanne Rome 200 1003
2003 Liu San Jose 300 1002
2004 Grass Brelin 100 1002
2006 Clemens London 300 1007
2007 Pereira Rome 100 1004

ORDERS

ONUM AMT ODATE CNUM SNUM


3001 18.69 03-OCT-94 2008 1007
3003 767.19 03-OCT-94 2001 1001
3002 1900.10 03-OCT-94 2007 1004
3005 5160.45 03-OCT-94 2003 1002
3006 1098.16 04-OCT-94 2008 1007
3009 1713.23 04-OCT-94 2002 1003
3007 75.75 05-OCT-94 2004 1002
3008 4723.00 05-OCT-94 2006 1001
3010 1309.95 06-OCT-94 2004 1002
3011 9891.88 06-OCT-94 2006 1001

DBMS LAB MANUAL – prepared by M. Muni Babu Page 5


SQL - ASSIGNMENT QUESTIONS

1. Display snum, sname, city and comm of all salespeople.


2. Display all snum without duplicates from all orders.
3. Display names and commissions of all salespeople in london.
4. All customers with rating of 100.
5. Produce orderno, amount and date form all rows in the order table.
6. All customers in San Jose, who have rating more than 200.
7. All customers who were either located in San Jose or had a rating above 200.
8. All orders for more than $1000.
9. Names and citires of all salespeople in london with commission above 0.10.
10. All customers excluding those with rating <= 100 unless they are located in Rome.
11. All salespeople either in Barcelona or in london.
12. All salespeople with commission between 0.10 and 0.12. (Boundary values should be excluded)
13. All customers with NULL values in city column.
14. All orders taken on Oct 3Rd and Oct 4th 1994.
15. All customers serviced by peel or Motika.
16. All customers whose names begin with a letter from A to B.
17. All orders except those with 0 or NULL value in amt field.
18. Count the number of salespeople currently listing orders in the order table.
19. Largest order taken by each salesperson, datewise.
20. Largest order taken by each salesperson with order value more than $3000.
21. Which day had the hightest total amount ordered.
22. Count all orders for Oct 3rd.
23. Count the number of different non NULL city values in customers table.
24. Select each customer’s smallest order.
25. First customer in alphabetical order whose name begins with G.
26. Get the output like “For dd/mm/yy there are ___ orders.
27. Assume that each salesperson has a 12% commission. Produce order no., salesperson no., and
amount of salesperson’s commission for that order.
28. Find highest rating in each city. Put the output in this form. For the city (city), the highest rating
is : (rating).

DBMS LAB MANUAL – prepared by M. Muni Babu Page 6


29. Display the totals of orders for each day and place the results in descending order.
30. All combinations of salespeople and customers who shared a city. (ie same city).
31. Name of all customers matched with the salespeople serving them.
32. List each order number followed by the name of the customer who made the order.
33. Names of salesperson and customer for each order after the order number.
34. Produce all customer serviced by salespeople with a commission above 12%.
35. Calculate the amount of the salesperson’s commission on each order with a rating above 100.
36. Find all pairs of customers having the same rating.
37. Find all pairs of customers having the same rating, each pair coming once only.
38. Policy is to assign three salesperson to each customers. Display all such combinations.
39. Display all customers located in cities where salesman serres has customer.
40. Find all pairs of customers served by single salesperson.
41. Produce all pairs of salespeople which are living in the same city. Exclude combinations of
salespeople with themselves as well as duplicates with the order reversed.
42. Produce all pairs of orders by given customer, names that customers and eliminates duplicates.
43. Produce names and cities of all customers with the same rating as Hoffman.
44. Extract all the orders of Motika.
45. All orders credited to the same salesperson who services Hoffman.
46. All orders that are greater than the average for Oct 4.
47. Find average commission of salespeople in london.
48. Find all orders attributed to salespeople servicing customers in london.
49. Extract commissions of all salespeople servicing customers in London.
50. Find all customers whose cnum is 1000 above the snum of serres.
51. Count the customers with rating above San Jose’s average.
52. Obtain all orders for the customer named Cisnerous. (Assume you don’t know his customer no.
(cnum)).
53. Produce the names and rating of all customers who have above average orders.
54. Find total amount in orders for each salesperson for whom this total is greater than the amount of
the largest order in the table.
55. Find all customers with order on 3rd Oct.
56. Find names and numbers of all salesperson who have more than one customer.

DBMS LAB MANUAL – prepared by M. Muni Babu Page 7


57. Check if the correct salesperson was credited with each sale.
58. Find all orders with above average amounts for their customers.
59. Find the sums of the amounts from order table grouped by date, eliminating all those dates where
the sum was not at least 2000 above the maximum amount.
60. Find names and numbers of all customers with ratings equal to the maximum for their city.
61. Find all salespeople who have customers in their cities who they don’t service. (Both way using
Join and Correlated subquery).
62. Extract cnum, cname and city from customer table if and only if one or more of the customers in
the table are located in San Jose.
63. Find salespeople no. who have multiple customers.
64. Find salespeople number, name and city who have multiple customers.
65. Find salespeople who serve only one customer.
66. Extract rows of all salespeople with more than one current order.
67. Find all salespeople who have customers with a rating of 300. (use EXISTS)
68. Find all salespeople who have customers with a rating of 300. (use Join).
69. Select all salespeople with customers located in their cities who are not assigned to them. (use
EXISTS).
70. Extract from customers table every customer assigned the a salesperson who currently has at
least one other customer (besides the customer being selected) with orders in order table.
71. Find salespeople with customers located in their cities (using both ANY and IN).
72. Find all salespeople for whom there are customers that follow them in alphabetical order. (Using
ANY and EXISTS)
73. Select customers who have a greater rating than any customer in rome.
74. Select all orders that had amounts that were greater that atleast one of the orders from Oct 6th.
75. Find all orders with amounts smaller than any amount for a customer in San Jose. (Both using
ANY and without ANY)
76. Select those customers whose ratings are higher than every customer in Paris. (Using both ALL
and NOT EXISTS).
77. Select all customers whose ratings are equal to or greater than ANY of the Seeres.
78. Find all salespeople who have no customers located in their city. (Both using ANY and ALL)
79. Find all orders for amounts greater than any for the customers in London.

DBMS LAB MANUAL – prepared by M. Muni Babu Page 8


80. Find all salespeople and customers located in london.
81. For every salesperson, dates on which highest and lowest orders were brought.
82. List all of the salespeople and indicate those who don’t have customers in their cities as well as
those who do have.
83. Append strings to the selected fields, indicating weather or not a given salesperson was matched
to a customer in his city.
84. Create a union of two queries that shows the names, cities and ratings of all customers. Those
with a rating of 200 or greater will also have the words ‘High Rating’, while the others will have
the words ‘Low Rating’.
85. Write command that produces the name and number of each salesperson and each customer with
more than one current order. Put the result in alphabetical order.
86. Form a union of three queries. Have the first select the snums of all salespeople in San Jose, then
second the cnums of all customers in San Jose and the third the onums of all orders on Oct. 3.
Retain duplicates between the last two queries, but eliminates and redundancies between either
of them and the first.
87. Produce all the salesperson in London who had at least one customer there.
88. Produce all the salesperson in London who did not have customers there.
89. We want to see salespeople matched to their customers without excluding those salesperson who
were not currently assigned to any customers. (User OUTER join and UNION)
90. Insert into table emp1 empno, sal and deptno from emp table.
91. Update Salary of all employees by 10%.
92. Delete all rows from emp for deptno = 10.
93. Select list of all jobs which have an annual average salary greater than that managers.
94. Select list of all employees who have atleast one other employee reporting to them.
95. Select all employees with correponding level numbers.
96. Select average salary for employee at each level.
97. Display organization chart for only those employee who work under ‘JONES’.
98. Display organization chart for only those employee who work under ‘JONES’ and ‘BLAKE’.
99. List information about all the people in the organization above ‘ADAMS’.
100. List all the people who work under ‘BLAKE’ except ‘JAMES’.
101. List all the people who work under ‘KING’ except all employees working under ‘BLAKE’.

DBMS LAB MANUAL – prepared by M. Muni Babu Page 9


102. List all the people who work under ‘KING’ except ‘ADAMS’ and ‘BLAKE’ and all employees
working under ‘BLAKE’.
103. Select max salary of deptno 10,20 and 30 in single row.
104. If supply table has three fields vendor, job, part. Find list of vendor who are supplying all part
for given job.
105. List all pairs of orders having same item and qty.

DBMS LAB MANUAL – prepared by M. Muni Babu Page 10


SQL - ASSIGNMENT QUESTIONS WITH ANSWERS

1. Display snum, sname, city and comm of all salespeople.

SELECT snum, sname, city, comm


FROM salespeople;

OUTPUT:

2. Display all snum without duplicates from all orders.

SELECT DISTINCT snum


FROM orders;

OUTPUT:

3. Display names and commissions of all salespeople in london.

SELECT sname, comm


FROM salespeople
WHERE city = ‘London’;

OUTPUT:

4. All customers with rating of 100.

SELECT cname
FROM cust
WHERE rating = 100;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 11


5. Produce orderno, amount and date form all rows in the order table.

SELECT ordno, amt, odate


FROM orders;

OUTPUT:

6. All customers in San Jose, who have rating more than 200.

SELECT cname
FROM cust
WHERE rating > 200;

OUTPUT:

7. All customers who were either located in San Jose or had a rating above 200.

SELECT cname
FROM cust
WHERE city = ‘San Jose’ or
rating > 200;

OUTPUT:

8. All orders for more than $1000.

SELECT *
FROM orders
WHERE amt > 1000;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 12


9. Names and citires of all salespeople in london with commission above 0.10.

SELECT sname, city


FROM salepeople
WHERE comm > 0.10 AND
city = ‘London’;

OUTPUT:

10. All customers excluding those with rating <= 100 unless they are located in Rome.

SELECT cname
FROM cust
WHERE rating <= 100 OR
city = ‘Rome’;

OUTPUT:

11. All salespeople either in Barcelona or in london.

SELECT sname, city


FROM salespeople
WHERE city IN (‘Barcelona’,’London’);

OUTPUT:

12. All salespeople with commission between 0.10 and 0.12. (Boundary values should be excluded)

SELECT sname, comm


FROM salespeople
WHERE comm > 0.10 AND comm < 0.12;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 13


13. All customers with NULL values in city column.

SELECT cname
FROM cust
WHERE city IS NULL;

OUTPUT:

14. All orders taken on Oct 3Rd and Oct 4th 1994.

SELECT *
FROM orders
WHERE odate in (‘03-OCT-94’,’04-OCT-94’);

OUTPUT:

15. All customers serviced by peel or Motika.

SELECT cname
FROM cust, orders
WHERE orders.cnum = cust.cnum AND
orders.snum IN ( SELECT snum
FROM salespeople
WHERE sname IN 'Peel','Motika'));

OUTPUT:

16. All customers whose names begin with a letter from A to B.

SELECT cname
FROM cust
WHERE cname like ‘A%’ OR
cname like ‘B%’;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 14


17. All orders except those with 0 or NULL value in amt field.

SELECT onum
FROM orders
WHERE amt != 0 OR
amt IS NOT NULL;

OUTPUT:

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

SELECT COUNT(DISTINCT snum)


FROM orders;

OUTPUT:

19. Largest order taken by each salesperson, datewise.

SELECT odate, snum, MAX(amt)


FROM orders
GROUP by odate, snum
ORDER BY odate,snum;

OUTPUT:

20. Largest order taken by each salesperson with order value more than $3000.
SELECT odate, snum, MAX(amt)
FROM orders
WHERE amt > 3000
GROUP BY odate, snum
ORDER BY odate,snum;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 15


21. Which day had the hightest total amount ordered.

SELECT odate, amt, snum, cnum


FROM orders
WHERE amt = (SELECT MAX(amt)
FROM orders);

OUTPUT:

22. Count all orders for Oct 3rd.

SELECT COUNT(*)
FROM orders
WHERE odate = ‘03-OCT-94’;

OUTPUT:

23. Count the number of different non NULL city values in customers table.

SELECT COUNT(DISTINCT city)


FROM cust;

OUTPUT:

24. Select each customer’s smallest order.

SELECT cnum, MIN(amt)


FROM orders
GROUP BY cnum;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 16


25. First customer in alphabetical order whose name begins with G.

SELECT MIN(cname)
FROM cust
WHERE cname like ‘G%’;

OUTPUT:

26. Get the output like “ For dd/mm/yy there are ___ orders.

SELECT 'For ' || to_char(odate,'dd/mm/yy') || ' there are '||


COUNT(*) || ' Orders'
FROM orders
GROUP BY odate;

OUTPUT:

27. Assume that each salesperson has a 12% commission. Produce order no., salesperson no., and
amount of salesperson’s commission for that order.

SELECT onum, snum, amt, amt * 0.12


FROM orders
ORDER BY snum;

OUTPUT:

28. Find highest rating in each city. Put the output in this form. For the city (city), the highest rating
is : (rating).

SELECT 'For the city (' || city || '), the highest rating is : (' ||
MAX(rating) || ')'
FROM cust
GROUP BY city;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 17


29. Display the totals of orders for each day and place the results in descending order.

SELECT odate, COUNT(onum)


FROM orders
GROUP BY odate
ORDER BY COUNT(onum);

OUTPUT:

30. All combinations of salespeople and customers who shared a city. (ie same city).

SELECT sname, cname


FROM salespeople, cust
WHERE salespeople.city = cust.city;

OUTPUT:

31. Name of all customers matched with the salespeople serving them.

SELECT cname, sname


FROM cust, salespeople
WHERE cust.snum = salespeople.snum;

OUTPUT:

32. List each order number followed by the name of the customer who made the order.

SELECT onum, cname


FROM orders, cust
WHERE orders.cnum = cust.cnum;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 18


33. Names of salesperson and customer for each order after the order number.

SELECT onum, sname, cname


FROM orders, cust, salespeople
WHERE orders.cnum = cust.cnum AND
orders.snum = salespeople.snum;

OUTPUT:

34. Produce all customer serviced by salespeople with a commission above 12%.

SELECT cname, sname, comm


FROM cust, salespeople
WHERE comm > 0.12 AND
cust.snum = salespeople.snum;

OUTPUT:

35. Calculate the amount of the salesperson’s commission on each order with a rating above 100.

SELECT sname, amt * comm


FROM orders, cust, salespeople
WHERE rating > 100 AND
salespeople.snum = cust.snum AND
salespeople.snum = orders.snum AND
cust.cnum = orders.cnum;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 19


36. Find all pairs of customers having the same rating.

SELECT a.cname, b.cname,a.rating


FROM cust a, cust b
WHERE a.rating = b.rating AND
a.cnum != b.cnum;

OUTPUT:

37. Find all pairs of customers having the same rating, each pair coming once only.

SELECT a.cname, b.cname,a.rating


FROM cust a, cust b
WHERE a.rating = b.rating AND
a.cnum != b.cnum AND
a.cnum < b.cnum;

OUTPUT:

38. Policy is to assign three salesperson to each customers. Display all such combinations.

SELECT cname, sname


FROM salespeople, cust
WHERE sname IN (SELECT sname
FROM salespeople
WHERE rownum <= 3)
ORDER BY cname;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 20


39. Display all customers located in cities where salesman serres has customer.

SELECT cname
FROM cust
WHERE city = ( SELECT city
FROM cust, salespeople
WHERE cust.snum = salespeople.snum AND
sname = 'Serres');

SELECT cname
FROM cust
WHERE city IN (SELECT city
FROM cust, orders
WHERE cust.cnum = orders.cnum AND
orders.snum IN ( SELECT snum
FROM salespeople
WHERE sname = 'Serres'));

OUTPUT:

40. Find all pairs of customers served by single salesperson.

SELECT cname
FROM cust
WHERE snum IN (SELECT snum FROM cust
GROUP BY snum
HAVING COUNT(snum) > 1);

SELECT DISTINCT a.cname


FROM cust a ,cust b
WHERE a.snum = b.snum AND
a.rowid != b.rowid;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 21


41. Produce all pairs of salespeople which are living in the same city. Exclude combinations of
salespeople with themselves as well as duplicates with the order reversed.

SELECT a.sname, b.sname


FROM salespeople a, salespeople b
WHERE a.snum > b.snum AND
a.city = b.city;

OUTPUT:

42. Produce all pairs of orders by given customer, names that customers and eliminates duplicates.

SELECT c.cname, a.onum, b.onum


FROM orders a, orders b, cust c
WHERE a.cnum = b.cnum AND
a.onum > b.onum AND
c.cnum = a.cnum;

OUTPUT:

43. Produce names and cities of all customers with the same rating as Hoffman.

SELECT cname, city


FROM cust
WHERE rating = (SELECT rating
FROM cust
WHERE cname = 'Hoffman') AND
cname != 'Hoffman';
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 22


44. Extract all the orders of Motika.

SELECT Onum
FROM orders
WHERE snum = (SELECT snum
FROM salespeople
WHERE sname = ‘Motika’);
OUTPUT:

45. All orders credited to the same salesperson who services Hoffman.

SELECT onum, sname, cname, amt


FROM orders a, salespeople b, cust c
WHERE a.snum = b.snum AND
a.cnum = c.cnum AND
a.snum = ( SELECT snum
FROM orders
WHERE cnum = ( SELECT cnum
FROM cust
WHERE cname = 'Hoffman'));

OUTPUT:

46. All orders that are greater than the average for Oct 4.

SELECT *
FROM orders
WHERE amt > (SELECT AVG(amt)
FROM orders
WHERE odate = '03-OCT-94');

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 23


47. Find average commission of salespeople in london.

SELECT AVG(comm)
FROM salespeople
WHERE city = ‘London’;

OUTPUT:

48. Find all orders attributed to salespeople servicing customers in london.

SELECT snum, cnum


FROM orders
WHERE cnum IN (SELECT cnum
FROM cust
WHERE city = 'London');

OUTPUT:

49. Extract commissions of all salespeople servicing customers in London.

SELECT comm
FROM salespeople
WHERE snum IN (SELECT snum
FROM cust
WHERE city = ‘London’);

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 24


50. Find all customers whose cnum is 1000 above the snum of serres.

SELECT cnum, cname


FROM cust
WHERE cnum > ( SELECT snum+1000
FROM salespeople
WHERE sname = 'Serres');
OUTPUT:

51. Count the customers with rating above San Jose’s average.

SELECT cnum, rating


FROM cust
WHERE rating > (SELECT AVG(rating)
FROM cust
WHERE city = 'San Jose');
OUTPUT:

52. Obtain all orders for the customer named Cisnerous. (Assume you don’t know his customer no.
(cnum)).

SELECT onum, odate


FROM orders
WHERE cnum = ( SELECT cnum
FROM cust
WHERE cname = ‘Cisnerous’);
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 25


53. Produce the names and rating of all customers who have above average orders.

SELECT MAX(b.cname), MAX(b.rating), a.cnum


FROM orders a, cust b
WHERE a.cnum = b.cnum
GROUP BY a.cnum
HAVING COUNT(a.cnum) > (SELECT AVG(COUNT(cnum))
FROM orders
GROUP BY cnum);
OUTPUT:

54. Find total amount in orders for each salesperson for whom this total is greater than the amount of
the largest order in the table.

SELECT snum,SUM(amt)
FROM orders
GROUP BY snum
HAVING SUM(amt) > (SELECT MAX(amt)
FROM orders);

OUTPUT:

55. Find all customers with order on 3rd Oct.

SELECT cname
FROM cust a, orders b
WHERE a.cnum = b.cnum AND
odate = ‘03-OCT-94’;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 26


56. Find names and numbers of all salesperson who have more than one customer.

SELECT sname, snum


FROM salespeople
WHERE snum IN ( SELECT snum
FROM cust
GROUP BY snum
HAVING COUNT(snum) > 1 );
OUTPUT:

57. Check if the correct salesperson was credited with each sale.

SELECT onum, a.cnum, a.snum, b.snum


FROM orders a, cust b
WHERE a.cnum = b.cnum AND
a.snum != b.snum;

OUTPUT:

58. Find all orders with above average amounts for their customers.

SELECT onum, cnum, amt


FROM orders a
WHERE amt > (SELECT AVG(amt)
FROM orders b
WHERE a.cnum = b.cnum
GROUP BY cnum);

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 27


59. Find the sums of the amounts from order table grouped by date, eliminating all those dates where
the sum was not at least 2000 above the maximum amount.

SELECT odate, SUM(amt)


FROM orders a
GROUP BY odate
HAVING SUM(amt) > (SELECT MAX(amt)
FROM orders b
WHERE a.odate = b.odate
GROUP BY odate);
OUTPUT:

60. Find names and numbers of all customers with ratings equal to the maximum for their city.

SELECT a.cnum, a.cname


FROM cust a
WHERE a.rating = (SELECT MAX(rating)
FROM cust b
WHERE a.city = b.city);
OUTPUT:

61. Find all salespeople who have customers in their cities who they don’t service. ( Both way using
Join and Correlated subquery).

SELECT DISTINCT cname


FROM cust a, salespeople b
WHERE a.city = b.city AND
a.snum != b.snum;

SELECT cname
FROM cust
WHERE cname IN (SELECT cname
FROM cust a, salespeople b
WHERE a.city = b.city AND
a.snum != b.snum );
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 28


62. Extract cnum,cname and city from customer table if and only if one or more of the customers in
the table are located in San Jose.

SELECT * FROM cust


WHERE 2 < (SELECT COUNT(*)
FROM cust
WHERE city = 'San Jose');

OUTPUT:

63. Find salespeople no. who have multiple customers.

SELECT snum
FROM cust
GROUP BY snum
HAVING COUNT(*) > 1;

OUTPUT:

64. Find salespeople number, name and city who have multiple customers.

SELECT snum, sname, city


FROM salespeople
WHERE snum IN ( SELECT snum
FROM cust
GROUP BY snum
HAVING COUNT(*) > 1);
OUTPUT:

65. Find salespeople who serve only one customer.

SELECT snum
FROM cust
GROUP BY snum
HAVING COUNT(*) = 1;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 29


66. Extract rows of all salespeople with more than one current order.

SELECT snum, COUNT(snum)


FROM orders
GROUP BY snum
HAVING COUNT(snum) > 1;

OUTPUT:

67. Find all salespeople who have customers with a rating of 300. (use EXISTS)

SELECT a.snum
FROM salespeople a
WHERE EXISTS (SELECT b.snum
FROM cust b
WHERE b.rating = 300 AND
a.snum = b.snum);
OUTPUT:

68. Find all salespeople who have customers with a rating of 300. (use Join).

SELECT a.snum
FROM salespeople a, cust b
WHERE b.rating = 300 AND
a.snum = b.snum;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 30


69. Select all salespeople with customers located in their cities who are not assigned to them. (use
EXISTS).

SELECT snum, sname


FROM salespeople
WHERE EXISTS (SELECT cnum
FROM cust
WHERE salespeople.city = cust.city AND
salespeople.snum != cust.snum);

OUTPUT:

70. Extract from customers table every customer assigned the a salesperson who currently has at
least one other customer ( besides the customer being selected) with orders in order table.

SELECT a.cnum, MAX(c.cname)


FROM orders a, cust c
WHERE a.cnum = c.cnum
GROUP BY a.cnum,a.snum
HAVING COUNT(*) < (SELECT COUNT(*)
FROM orders b
WHERE a.snum = b.snum)
ORDER BY a.cnum;
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 31


71. Find salespeople with customers located in their cities ( using both ANY and IN).

SELECT sname
FROM salespeople
WHERE snum IN (SELECT snum
FROM cust
WHERE salespeople.city = cust.city AND
salespeople.snum = cust.snum);

SELECT sname
FROM salespeople
WHERE snum = ANY (SELECT snum
FROM cust
WHERE salespeople.city = cust.city AND
salespeople.snum = cust.snum);
OUTPUT:

72. Find all salespeople for whom there are customers that follow them in alphabetical order. (Using
ANY and EXISTS)

SELECT sname
FROM salespeople
WHERE sname < ANY (SELECT cname
FROM cust
WHERE salespeople.snum = cust.snum);
SELECT sname
FROM salespeople
WHERE EXISTS ( SELECT cname
FROM cust
WHERE salespeople.snum = cust.snum AND
salespeople.sname < cust.cname);
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 32


73. Select customers who have a greater rating than any customer in rome.

SELECT a.cname
FROM cust a
WHERE city = 'Rome' AND
rating > (SELECT MAX(rating)
FROM cust
WHERE city != 'Rome');
OUTPUT:

74. Select all orders that had amounts that were greater that atleast one of the orders from Oct 6th.

SELECT onum, amt


FROM orders
WHERE odate != '06-oct-94' AND
amt > (SELECT MIN(amt)
FROM orders
WHERE odate = '06-oct-94');
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 33


75. Find all orders with amounts smaller than any amount for a customer in San Jose. (Both using
ANY and without ANY)

SELECT onum, amt


FROM orders
WHERE amt < ANY ( SELECT amt
FROM orders, cust
WHERE city = 'San Jose' AND
orders.cnum = cust.cnum);

SELECT onum, amt


FROM orders
WHERE amt < ( SELECT MAX(amt)
FROM orders, cust
WHERE city = 'San Jose' AND
orders.cnum = cust.cnum);
OUTPUT:

76. Select those customers whose ratings are higher than every customer in Paris. ( Using both ALL
and NOT EXISTS).

SELECT * FROM cust


WHERE rating > ANY (SELECT rating FROM cust
WHERE city = 'Paris');

SELECT *
FROM cust a
WHERE NOT EXISTS ( SELECT b.rating FROM cust b
WHERE b.city != 'Paris' AND
b.rating > a.rating);

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 34


77. Select all customers whose ratings are equal to or greater than ANY of the Seeres.

SELECT cname, sname


FROM cust, salespeople
WHERE rating >= ANY (SELECT rating
FROM cust
WHERE snum = (SELECT snum
FROM salespeople
WHERE sname = 'Serres')) AND
sname != 'Serres' AND
salespeople.snum(+) = cust.snum;

OUTPUT:

78. Find all salespeople who have no customers located in their city. ( Both using ANY and ALL)

SELECT sname
FROM salespeople
WHERE snum IN ( SELECT snum
FROM cust
WHERE salespeople.city != cust.city AND
salespeople.snum = cust.snum);

SELECT sname
FROM salespeople
WHERE snum = ANY ( SELECT snum
FROM cust
WHERE salespeople.city != cust.city AND
salespeople.snum = cust.snum);
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 35


79. Find all orders for amounts greater than any for the customers in London.

SELECT onum, amt


FROM orders
WHERE amt > ANY ( SELECT amt
FROM orders, cust
WHERE city = ‘London’ AND
orders.cnum = cust.cnum);
OUTPUT:

80. Find all salespeople and customers located in london.

SELECT sname, cname


FROM cust, salespeople
WHERE cust.city = 'London' AND
salespeople.city = 'London' AND
cust.snum = salespeople.snum;

OUTPUT:

81. For every salesperson, dates on which highest and lowest orders were brought.

SELECT a.amt, a.odate, b.amt, b.odate


FROM orders a, orders b
WHERE (a.amt, b.amt) IN (SELECT MAX(amt), MIN(amt)
FROM orders
GROUP BY snum);
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 36


82. List all of the salespeople and indicate those who don’t have customers in their cities as well as
those who do have.

SELECT snum, city, 'Customer Present'


FROM salespeople a
WHERE EXISTS (SELECT snum
FROM cust
WHERE a.snum = cust.snum AND
a.city = cust.city)
UNION

SELECT snum, city, 'Customer Not Present'


FROM salespeople a
WHERE EXISTS (SELECT snum
FROM cust c
WHERE a.snum = c.snum AND
a.city != c.city AND
c.snum NOT IN ( SELECT snum
FROM cust
WHERE a.snum = cust.snum AND
a.city = cust.city));
OUTPUT:

83. Append strings to the selected fields, indicating weather or not a given salesperson was matched
to a customer in his city.

SELECT a.cname, DECODE(a.city,b.city,'Matched','Not Matched')


FROM cust a, salespeople b
WHERE a.snum = b.snum;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 37


84. Create a union of two queries that shows the names, cities and ratings of all customers. Those
with a rating of 200 or greater will also have the words ‘High Rating’, while the others will have
the words ‘Low Rating’.

SELECT cname, cities, rating, ‘Higher Rating’


FROM cust
WHERE rating >= 200

UNION

SELECT cname, cities, rating, ‘Lower Rating’


FROM cust
WHERE rating < 200;

OUTPUT:

85. Write command that produces the name and number of each salesperson and each customer with
more than one current order. Put the result in alphabetical order.

SELECT 'Customer Number ' || cnum "Code ",count(*)


FROM orders
GROUP BY cnum
HAVING COUNT(*) > 1

UNION

SELECT 'Salesperson Number '||snum,COUNT(*)


FROM orders
GROUP BY snum
HAVING COUNT(*) > 1;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 38


86. Form a union of three queries. Have the first select the snums of all salespeople in San Jose, then
second the cnums of all customers in San Jose and the third the onums of all orders on Oct. 3.
Retain duplicates between the last two queries, but eliminates and redundancies between either
of them and the first.

SELECT 'Customer Number ' || cnum "Code "


FROM cust
WHERE city = 'San Jose'

UNION

SELECT 'Salesperson Number '||snum


FROM salespeople
WHERE city = 'San Jose'

UNION ALL

SELECT 'Order Number '|| onum


FROM Orders
WHERE odate = '03-OCT-94';

OUTPUT:

87. Produce all the salesperson in London who had at least one customer there.

SELECT snum, sname


FROM salespeople
WHERE snum IN (SELECT snum
FROM cust
WHERE cust.snum = salespeople.snum AND
cust.city = 'London') AND
city = ‘London’;
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 39


88. Produce all the salesperson in London who did not have customers there.

SELECT snum, sname


FROM salespeople
WHERE snum IN (SELECT snum
FROM cust
WHERE cust.snum = salespeople.snum AND
cust.city = 'London') AND
city = 'London';
OUTPUT:

89. We want to see salespeople matched to their customers without excluding those salesperson who
were not currently assigned to any customers. (User OUTER join and UNION)

SELECT sname, cname


FROM cust, salespeople
WHERE cust.snum(+) = salespeople.snum;

SELECT sname, cname


FROM cust, salespeople
WHERE cust.snum = salespeople.snum

UNION

SELECT DISTINCT sname, 'No Customer'


FROM cust, salespeople
WHERE 0 = (SELECT COUNT(*)
FROM cust
WHERE cust.snum = salespeople.snum);

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 40


90. Insert into table emp1 empno, sal and deptno from emp table.

IF TABLE emp1 IS CREATED THEN


INSERT INTO emp1 (SELECT empno,sal,deptno
FROM emp);

IF TABLE IS NOT CREATED THEN


CREATE TABLE emp1 AS ( SELECT empno,sal,deptno
FROM emp);

OUTPUT:

91. Update Salary of all employees by 10%.

UPDATE emp
SET sal = sal + 0.10 * sal;

OUTPUT:

92. Delete all rows from emp for deptno = 10.


DELETE FROM emp
WHERE deptno = 10;

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 41


93. Select list of all jobs which have an annual average salary greater than that managers.

SELECT job,avg(sal)
FROM emp
GROUP BY job
HAVING AVG(sal) > (SELECT AVG(sal)
FROM emp
WHERE job = 'MANAGER');
OUTPUT:

94. Select list of all employees who have atleast one other employee reporting to them.

SELECT a.job, a.ename, a.empno, a.deptno


FROM emp a
WHERE EXISTS ( SELECT *
FROM emp b
WHERE a.empno = b.mgr);
OUTPUT:

95. Select all employees with correponding level numbers.

COLUMN orgn_chart format a21


SELECT LPAD(' ',3*level)|| ename orgn_charts,level,empno,job,mgr
FROM emp
CONNECT BY PRIOR empno = mgr
START WITH name = 'KING';

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 42


96. Select average salary for employee at each level.

SELECT level,AVG(sal)
FROM emp
CONNECT By PRIOR empno = mgr
START WITH name = 'KING'
GROUP BY level
ORDER BY level;

OUTPUT:

97. Display organization chart for only those employee who work under ‘JONES’.

COLUMN orgn_chart format a21


SELECT LPAD(' ',3*level)|| ename orgn_charts,level,empno,job,mgr
FROM emp
CONNECT BY prior empno = mgr
START WITH name = 'JONES';

OUTPUT:

98. Display organization chart for only those employee who work under ‘JONES’ and ‘BLAKE’.

COLUMN orgn_chart format a21


SELECT LPAD(' ',3*level)|| ename orgn_charts,level,empno,job,mgr
FROM emp
CONNECT BY PRIOR empno = mgr
START WITH name IN ('JONES',’BLAKE’);

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 43


99. List information about all the people in the organization above ‘ADAMS’.

COLUMN orgn_chart FORMAT a21


SELECT LPAD(' ',3*level)|| ename orgn_charts,empno,job,mgr
FROM emp
CONNECT BY empno = prior mgr
START WITH name = ‘ADAMS’;

OUTPUT:

100. List all the people who work under ‘BLAKE’ except ‘JAMES’.

COLUMN orgn_chart FORMAT a21


SELECT LPAD(' ',3*level)|| ename orgn_chart,level,empno,job,mgr
FROM emp
WHERE ename != 'JAMES'
CONNECT BY PRIOR empno = mgr
START WITH ename = 'BLAKE';

OUTPUT:

101. List all the people who work under ‘KING’ except all employees
working under ‘BLAKE’.

SELECT LPAD(' ',3*level)|| ename orgn_chart,level,empno,job,mgr


FROM emp
CONNECT BY PRIOR empno = mgr AND
ename != 'BLAKE'
START WITH ename = 'KING';

OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 44


102. List all the people who work under ‘KING’ except ‘ADAMS’ and ‘BLAKE’ and all employees
working under ‘BLAKE’.

SELECT LPAD(' ',3*level)|| ename orgn_chart,level,empno,job,mgr


FROM emp
WHERE ename != ‘ADAMS’
CONNECT BY PRIOR empno = mgr AND
ename != 'BLAKE'
START WITH ename = 'KING';

OUTPUT:

103. Select max salarys of deptno 10,20 and 30 in single row.

SELECT MIN(DECODE(deptno,10,MAX(sal))) "Dept No 10",


MIN(DECODE(deptno,20,MAX(sal))) "Dept No 20",
MIN(DECODE(deptno,30,MAX(sal))) "Dept No 30"
FROM emp
GROUP BY deptno;

OUTPUT:

104. If supply table has three fields vendor, job, part. Find list of vendor who
are supplying all part for given job.

SELECT a.vendor,a.job,count(*)
FROM supply a
GROUP BY a.vendor,a.job
HAVING COUNT(*) = (SELECT COUNT(*)
FROM supply b
WHERE a.job = b.job);
OUTPUT:

DBMS LAB MANUAL – prepared by M. Muni Babu Page 45


105. List all pairs of orders having same item and qty.
SELECT a.ordid, b.ordid
FROM item a, item b
WHERE NOT EXISTS (SELECT itemid,qty
FROM item c
WHERE c.ordid = a.ordid
MINUS
SELECT itemid,qty
FROM item d
WHERE d.ordid = b.ordid ) AND
NOT EXISTS (SELECT itemid, qty
FROM item c
WHERE c.ordid = b.ordid
MINUS
SELECT itemid,qty
FROM item d
WHERE d.ordid = a.ordid ) AND
a.ordid < b.ordid
GROUP BY a.ordid,b.ordid;

Table name: lot_mas t


Structure:
lot_no lot_desc storage
a Pentium fabrication
a 486 fabrication
b Pentium fabrication
d Pentium fabrication
d Pentium fabrication
. . .
. . .
. . .

I want to display the out put like


lot_no times
a 2
d 3
. .
. .

SELECT lot_no,COUNT(lot_no)
FROM lot_mast
GROUP BY lot_no

DBMS LAB MANUAL – prepared by M. Muni Babu Page 46


HAVING COUNT(lot_no) > 1;

There are twelve records for each employee in a year. The table structure
is as follow.

Empno month_no salary


1000 01 2000
1000 02 3500
1000 03 2500
.... .. ....
.... .. ....
1000 12 3450
2000 01 1900
2000 02 5000
.... .. ....
.... .. ....
2000 12 5450
3000 01 1900
3000 02 5000
.... .. ....
.... .. ....

The output should be as follows

empno Jan Feb Mar Apr May ...... Dec


1000 2000 3500 2500 .... .... ...... 3450
2000 1900 5000 .... .... .... ...... 5450
3000 1900 5000 .... .... .... ...... .......
.... .... .... .... .... .... .... ....
.... .... .... .... .... .... .... ....
.... .... .... .... .... .... .... ....

SELECT a.empno,a.salary,b.salary,c.salary,d.salary,e.salary,
f.salary,g.salary,h.salary,i.salary,j.salary,k.salary,l.salary
FROM emp a,emp b,emp c,emp d,emp e,emp
f,emp g,emp h,emp i,emp j,emp,k.emp,emp l
WHERE a.month = 1 AND
a.empno = b.empno and b.month = 2 AND
b.empno = c.empno and c.month = 3 AND
c.empno = d.empno and d.month = 4 AND
d.empno = e.empno and e.month = 5 AND
d.empno = f.empno and f.month = 6 AND

DBMS LAB MANUAL – prepared by M. Muni Babu Page 47


d.empno = g.empno and g.month = 7 AND
d.empno = h.empno and h.month = 8 AND
d.empno = i.empno and i.month = 9 AND
d.empno = j.empno and j.month = 10 AND
d.empno = k.empno and k.month = 11 AND
d.empno = l.empno and l.month = 12;

The table structure is as follows

start_site_name end_site_name
F1 F2
F2 F3
F3 F4
F5 F6
F6 F7
Fa Fb
Fb Fc
.. ..
.. ..

The out will be

level start_site_name end_site_name


1 F1 F2
2 F2 F3
3 F3 F4
1 F5 F6
2 F6 F7
1 Fa Fb
2 Fb Fc
.. .. ..
.. .. ..
.. .. ..

SELECT level,start_site_name,end_site_name
FROM table_name
CONNECT BY PRIOR end_site_name = start_site_name
START WITH start_site_name = F1;

DBMS LAB MANUAL – prepared by M. Muni Babu Page 48

You might also like