Lab Manual Muni Babu Practice SQL Queries 1
Lab Manual Muni Babu Practice SQL Queries 1
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:
MySQL
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
After completing this course the student must demonstrate the knowledge and ability to:
Assessment Method
******************************************************************************
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,
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
TABLE REQUIRED:
SQL QUERIES
TABLE SALESPEOPLE
TABLE CUST
ORDERS
OUTPUT:
OUTPUT:
OUTPUT:
SELECT cname
FROM cust
WHERE rating = 100;
OUTPUT:
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:
SELECT *
FROM orders
WHERE amt > 1000;
OUTPUT:
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:
OUTPUT:
12. All salespeople with commission between 0.10 and 0.12. (Boundary values should be excluded)
OUTPUT:
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:
SELECT cname
FROM cust, orders
WHERE orders.cnum = cust.cnum AND
orders.snum IN ( SELECT snum
FROM salespeople
WHERE sname IN 'Peel','Motika'));
OUTPUT:
SELECT cname
FROM cust
WHERE cname like ‘A%’ OR
cname like ‘B%’;
OUTPUT:
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.
OUTPUT:
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:
OUTPUT:
SELECT COUNT(*)
FROM orders
WHERE odate = ‘03-OCT-94’;
OUTPUT:
23. Count the number of different non NULL city values in customers table.
OUTPUT:
OUTPUT:
SELECT MIN(cname)
FROM cust
WHERE cname like ‘G%’;
OUTPUT:
26. Get the output like “ For dd/mm/yy there are ___ orders.
OUTPUT:
27. Assume that each salesperson has a 12% commission. Produce order no., salesperson no., and
amount of salesperson’s commission for that order.
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:
OUTPUT:
30. All combinations of salespeople and customers who shared a city. (ie same city).
OUTPUT:
31. Name of all customers matched with the salespeople serving them.
OUTPUT:
32. List each order number followed by the name of the customer who made the order.
OUTPUT:
OUTPUT:
34. Produce all customer serviced by salespeople with a commission above 12%.
OUTPUT:
35. Calculate the amount of the salesperson’s commission on each order with a rating above 100.
OUTPUT:
OUTPUT:
37. Find all pairs of customers having the same rating, each pair coming once only.
OUTPUT:
38. Policy is to assign three salesperson to each customers. Display all such combinations.
OUTPUT:
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:
SELECT cname
FROM cust
WHERE snum IN (SELECT snum FROM cust
GROUP BY snum
HAVING COUNT(snum) > 1);
OUTPUT:
OUTPUT:
42. Produce all pairs of orders by given customer, names that customers and eliminates duplicates.
OUTPUT:
43. Produce names and cities of all customers with the same rating as Hoffman.
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.
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:
SELECT AVG(comm)
FROM salespeople
WHERE city = ‘London’;
OUTPUT:
OUTPUT:
SELECT comm
FROM salespeople
WHERE snum IN (SELECT snum
FROM cust
WHERE city = ‘London’);
OUTPUT:
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)).
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:
SELECT cname
FROM cust a, orders b
WHERE a.cnum = b.cnum AND
odate = ‘03-OCT-94’;
OUTPUT:
57. Check if the correct salesperson was credited with each sale.
OUTPUT:
58. Find all orders with above average amounts for their customers.
OUTPUT:
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).
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:
OUTPUT:
SELECT snum
FROM cust
GROUP BY snum
HAVING COUNT(*) > 1;
OUTPUT:
64. Find salespeople number, name and city who have multiple customers.
SELECT snum
FROM cust
GROUP BY snum
HAVING COUNT(*) = 1;
OUTPUT:
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:
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 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:
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.
76. Select those customers whose ratings are higher than every customer in Paris. ( Using both ALL
and NOT EXISTS).
SELECT *
FROM cust a
WHERE NOT EXISTS ( SELECT b.rating FROM cust b
WHERE b.city != 'Paris' AND
b.rating > a.rating);
OUTPUT:
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:
OUTPUT:
81. For every salesperson, dates on which highest and lowest orders were brought.
83. Append strings to the selected fields, indicating weather or not a given salesperson was matched
to a customer in his city.
OUTPUT:
UNION
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.
UNION
OUTPUT:
UNION
UNION ALL
OUTPUT:
87. Produce all the salesperson in London who had at least one customer 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)
UNION
OUTPUT:
OUTPUT:
UPDATE emp
SET sal = sal + 0.10 * sal;
OUTPUT:
OUTPUT:
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.
OUTPUT:
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’.
OUTPUT:
98. Display organization chart for only those employee who work under ‘JONES’ and ‘BLAKE’.
OUTPUT:
OUTPUT:
100. List all the people who work under ‘BLAKE’ except ‘JAMES’.
OUTPUT:
101. List all the people who work under ‘KING’ except all employees
working under ‘BLAKE’.
OUTPUT:
OUTPUT:
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:
SELECT lot_no,COUNT(lot_no)
FROM lot_mast
GROUP BY lot_no
There are twelve records for each employee in a year. The table structure
is as follow.
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
start_site_name end_site_name
F1 F2
F2 F3
F3 F4
F5 F6
F6 F7
Fa Fb
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;