0% found this document useful (0 votes)
30 views28 pages

DBMS-3

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)
30 views28 pages

DBMS-3

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/ 28

AKIDO COLLEGE OF ENGINEERING

MDU , ROHTAK
PRACTICAL FILE

NAME : Nitya Sharma

COURSE : Bachelor of Computer Applications (BCA)

SEMESTER : III-C

REGISTRATION NUMBER : 2313071214

SUBJECT : INTRODUCTION TO DATABASE SYSTEM


INDEX
s.no. Topic Remarks

1. Display name and commission of all the salesman.

2. Retrieve salesman id of all salesmen from orders


table without any repeats.
3. Display names and city of salesman, who belongs
to the city of karwar.

4. Display all the information for those customers


with a grade of 200.

5. Display the order number, order date and the


purchase amount for order(s) which will be
delivered by the salesman with ID 44.
6. How the winner of the 1971 prize for Literature.

7. Show all the details of the winners with first name


Louis.
8. Show all the winners in Physics for 1970 together
with the winner of Economics for 1971.
9. Show all the winners of Nobel prize in the year
1970 except the subject Physiology and Economics.
10. Find all the details of the Nobel winners for the
subject not started with the letter 'P' and arranged
the list as the most recent comes first, then by
name in order.
11. Find the name and price of the cheapest item(s).

12. Display all the customers, who are either belongs


to the city Mysore or not had a grade above 1.
13. Find those salesmen with all information who gets
the commission within a range of 0.12 and 0.14.
14. Find all those customers with all information
whose names are ending with the letter 'a'.
15. Find those salesmen with all information whose
name containing the 1st character is 'P' and the 7th
character is 'v' and rests may be any character.
16. find that customer with all information who does
not get any grade except NULL.
17. Find the total purchase amount of all orders.

18. Find the number of salesman currently listing for


all of their customers.
19. Find the highest grade for each of the cities of the
customers.
20. Find the highest purchase amount ordered by the
each customer with their ID and highest purchase
amount.
21. Find the highest purchase amount ordered by the
each customer on a particular date with their ID,
order date and highest purchase amount.
22. Find the highest purchase amount on a date '2023-
10-01' for each salesman with their ID.
23. Find the highest purchase amount with their
customer ID and order date, for only those
customers who have the highest purchase amount
in a day is more than 2000.
24. Write a SQL statement that counts all orders for a
date 2023-12-03.

Multiple tables Joins Nested Queries

1. Find the name and city of those customers and


salesmen who lives in the same city.
2. Find the names of all customers along with the
salesmen who works for them.
3. Display all those orders by the customers not
located in the same cities where their salesmen live.
4. Display all the orders issued by the salesman
'Pranav' from the orders table.
5. Display all the orders which values are greater
than the average order value for 2023-01-04 .
6. Find all orders attributed to salesmen in
Bengalore.
7. Extract the data from the orders table for the
salesman who earned the maximum commission.
8. Find the name and ids of all salesmen who had
more than one customer.
9. Write a query to find all the salesmen who worked
for only one customer.
10. (Equivalent Queries)
Write a query to find all the salesmen who worked
for only one customer.
11. Display all the orders that had amounts that were
greater than at least one of the orders from 2023-
11-02.
12. Display only those customers whose grade are, in
fact,higher than every customer in Hubli.
Tables
Salesman

Customer

Orders
Query 1: Display name and commission of all the salesman.

SELECT name, commission


FROM salesman;

Query 2: Retrieve salesman id of all salesmen from orders


table without any repeats.

SELECT DISTINCT salesman_id


FROM orders;
Query 3: Display names and city of salesman, who belongs to
the city of karwar.

SELECT name,city
FROM salesman
WHERE city='karwar';

Query 4: Display all the information for those customers


with a grade of 200.
customer_id cust_name
SELECT *
FROM customer
WHERE grade = 3;
Query 5: Display the order number, order date and the
purchase amount for order(s) which will be delivered by
the salesman with ID 44.

SELECT order_number, order_date, purch_amt


FROM orders
WHERE salesman_id = 44;

Table: nobel_win
Query 6: How the winner of the 1971 prize for Literature.

SELECT winner
FROM nobel_win
WHERE year = 1971
AND subject = 'Literature';

Query 7: Show all the details of the winners with first name
Louis.
year subject winner country category
1970 Physics Louis Neel France Scientist
SELECT *
FROM nobel_win
WHERE winner LIKE 'Louis%';
Query 8: Show all the winners in Physics for 1970 together
with the winner of Economics for 1971.

SELECT *
FROM nobel_win
WHERE (subject = 'Physics' AND year = 1970)
UNION
(SELECT *
FROM nobel_win
WHERE (subject = 'Economics' AND year = 1971)
);

Query 9: Show all the winners of Nobel prize in the year


1970 except the subject Physiology and Economics.

SELECT *
FROM nobel_win
WHERE year = 1970
AND subject NOT IN ('Physiology','Economics');
Query 10: Find all the details of the Nobel winners for the
subject not started with the letter 'P' and arranged
the list as the most recent comes first, then by name in order.

SELECT *
FROM nobel_win
WHERE subject NOT LIKE 'P%'
ORDER BY year DESC, winner;

Table: item_mast
Query 11: Find the name and price of the cheapest item(s).

SELECT pro_name, pro_price


FROM item_mast
WHERE pro_price = (SELECT MIN(pro_price)
FROM item_mast);

Query 12: (table customer)

Display all the customers, who are either belongs to the city Mysore
or not had a grade above 1.

SELECT *
FROM customer
WHERE city = 'Mysore' OR NOT grade > 1;
Query 13: (table salesman)

Find those salesmen with all information who gets the


commission within a range of 0.12 and 0.14.

SELECT salesman_id, name, city, commission


FROM salesman
WHERE commission between 0.12 AND 0.14;

Query 14: (table customer)

Find all those customers with all information whose names


are ending with the letter 'a'.

SELECT *
FROM customer
WHERE cust_name LIKE '%a';
Query 15: (table salesman)

Find those salesmen with all information whose name


containing the 1st character is 'P' and the 7th character is 'v'
and rests may be any character.

SELECT *
FROM salesman
WHERE name LIKE ‘P______V%';

Query 16 (table customer)

find that customer with all information who does not get any
grade except NULL.

SELECT *
FROM customer
WHERE grade IS NULL;
Query 17: (table orders)

Find the total purchase amount of all orders.

SELECT SUM(pur_amt)
FROM orders;

Query 18: (table orders)

Find the number of salesman currently listing for all of their


customers.

SELECT COUNT (DISTINCT salesman_id)


FROM orders;
Query 19: (table customer)

Find the highest grade for each of the cities of the customers.

SELECT city, MAX(grade)


FROM customer
GROUP BY city;

Query 20 : (table orders)

Find the highest purchase amount ordered by the each


customer with their ID and highest purchase amount.
SELECT customer_id, MAX(purch_amt)
FROM orders
GROUP BY customer_id;

Query 21: (table orders)

Find the highest purchase amount ordered by the each


customer on a particular date with their ID, order date
and highest purchase amount.

SELECT customer_id, order_date, MAX(pur_amt)


FROM orders
GROUP BY customer_id, order_date;
Query 22: (table orders)

Find the highest purchase amount on a date '2023-10-01'


for each salesman with their ID.

SELECT salesman_id, MAX(pur_amt)


FROM orders
WHERE order_date = '2023-10-01'
GROUP BY salesman_id;

Query 23: (table orders)

Find the highest purchase amount with their customer ID


and order date, for only those customers who have the
highest purchase amount in a day is more than 2000.
SELECT customer_id, order_date, MAX(pur_amt)
FROM orders
GROUP BY customer_id, order_date
HAVING MAX(pur_amt) > 2000.00;

Query 24: (table orders)

Write a SQL statement that counts all orders for a


date 2023-12-03.

SELECT COUNT(*)
FROM orders
WHERE order_date = '2023-12-03';
Multiple tables Joins Nested Queries

Query 1: Find the name and city of those customers


and salesmen who lives in the same city.

SELECT C.customer_name S.name S.city


FROM salesman AS S customer AS C
WHERE S.city = C.city;
Query 2: Find the names of all customers along with the
salesmen who works for them.

SELECT customer.cust_name, salesman.name


FROM customer ,salesman
WHERE salesman.salesman_id = customer.salesman_id;

Query 3: Display all those orders by the customers not


located in the same cities where their salesmen live.

SELECT order_number customer_name orders.customer_id


orders.salesman_id
FROM salesman customer orders
WHERE customer.city <> salesman.city
AND orders.customer_id = customer.customer_id
AND orders.salesman_id = salesman.salesman_id;
Query 4: (using subquery)

Display all the orders issued by the salesman 'Pranav' from


the orders table.

SELECT *
FROM orders
WHERE salesman_id =
(SELECT salesman_id
FROM salesman
WHERE name = 'Pranav’);

• Can we make this query unnested? If yes how?

Yes, this query can be re-written using an INNER JOIN intead


of a subquery.
Query 5: (using subquery)

Display all the orders which values are greater than


the average order value for 2023-01-04 .

SELECT *
FROM orders
WHERE pur_amt >
(SELECT AVG(pur_amt)
FROM orders
WHERE order_date = '2023-01-04 ');

• Can we make this query unnested? If yes how?

Yes, this query can be re-written using a JOIN function.

Query 6: (using subquery)

Find all orders attributed to salesmen in Bengalore.

SELECT *
FROM orders
WHERE salesman_id IN
(SELECT salesman_id
FROM salesman
WHERE city ='Bengalore');

• Can we make this query unnested? If yes how?

Yes, the query can be rewritten using an INNER JOIN or EXISTS


clause.

Query 7: (using subquery)

Extract the data from the orders table for the salesman who
earned the maximum commission.

SELECT order_number, pur_amt, order_date, salesman_id


FROM orders
WHERE salesman_id IN (
SELECT salesman_id
FROM salesman
WHERE commission = (
SELECT MAX(commission)
FROM salesman)
);
Query 8: (using subquery)

Find the name and ids of all salesmen who had more than
one customer.

SELECT salesman_id, name


FROM salesman AS a
WHERE 1 < (SELECT COUNT(*)
FROM customer AS c
WHERE c.salesman_id = a.salesman_id);

Query 9: (using subquery)

Write a query to find all the salesmen who worked for only
one customer.

SELECT *
FROM salesman
WHERE salesman_id IN (
SELECT DISTINCT salesman_id
FROM customer a
WHERE NOT EXISTS (
SELECT * FROM customer b
WHERE a.salesman_id = b.salesman_id
AND a.cust_name <> b.cust_name)
);

Query 10: (Equivalent Queries)

Write a query to find all the salesmen who worked for only
one customer.

SELECT c.salesman_id, s.name, s.city, s.commission


FROM salesman s, customer c
WHERE s.salesman_id = c.salesman_id
GROUP BY c.salesman_id, s.name
HAVING COUNT(c.salesman_id) = 1;

SELECT *
FROM salesman
WHERE salesman_id NOT IN (
SELECT a.salesman_id
FROM customer a, customer b
WHERE a.salesman_id = b.salesman_id
AND a.cust_name <> b.cust_name);

Query 11: (using subquery)

Display all the orders that had amounts that were greater
than at least one of the orders from 2023-11-02.

SELECT *
FROM Orders
WHERE pur_amt > ANY
(SELECT pur_amt
FROM orders
WHERE order_date = '2023-11-02');

Query 12: (using subquery)


Display only those customers whose grade are, in fact,higher
than every customer in Hubli.

SELECT *
FROM customer
WHERE grade > ALL
(SELECT grade
FROM customer
WHERE city = 'Hubli');

You might also like