DBMS Lab
DBMS Lab
CREATION OF TABLE:
INSERTION OF VALUES:
QUERY:
O/P:
2. Display the details of accident in Banglore and damage amount more than 50000.
Ans:
SELECT *
FROM accident a, participated p
WHERE a.report_no=p.rep_no and
a.loc='Banglore' and
p.dm_amt >=50000;
O/P:
O/P:
Enter value for model: Hyundai
MAX(DM_AMT)
-----------
125000
5. Display the details of accident WHERE damage amount is more than the average damage amount of
Hyundai model.
Ans:
SELECT *
FROM accident a, participated p
WHERE a.report_no=p.rep_no and
p.dm_amt < (SELECT avg(dm_amt)
FROM car c, participated p
WHERE c.reg_no=p.reg_no and
c.model='Hyundai');
O/P:
6. Display the car details whose model is Kia and manufactured during 2022.
Ans:
SELECT *
FROM car
WHERE model='Kia' and year=2022;
O/P:
7. Display the alphabetical order of person name, who are involved in accidents and the damage
amount is more than 100000.
Ans:
SELECT name
FROM person p, participated pa
WHERE p.dr_id=pa.dr_id and
pa.dm_amt>100000
ORDER BY p.name;
O/P:
NAME
----------
Raju
Ramu
O/P:
9. Update the damage amount to 40000 for the car with a specific register number in the accident with
report number 15 to 30.
Ans:
UPDATE participated
SET dm_amt=40000
WHERE rep_no >15 and rep_no <30 and
reg_no='®_no';
O/P:
10. Find the total number of people who owned cars that involved in accidents in 2022.
Ans:
SELECT count(*) as no_of_people
FROM owns o, accident a, participated p
WHERE o.dr_id = p.dr_id and
a.report_no = p.rep_no and
a.acc_date like '%22';
O/P:
NO_OF_PEOPLE
------------
1
2. Consider the following database of student enrollment in courses & books adopted for each course.
CREATION OF TABLE:
INSERTION OF VALUES:
QUERIES:
1. Display the details of all the students who born during 2004.
Ans:
SELECT *
FROM student
WHERE bdate like '%04';
O/P:
O/P:
3. Display course number and course name which has adopted more than 2 books.
Ans:
SELECT c.courseno, c.cname
FROM course c, book_adoption b
WHERE c.courseno=b.courseno
GROUP BY c.courseno, c.cname having
count(*) >2;
O/P:
COURSENO CNAME
---------- ----------
EE5TH1 WT
4. Display the students who have scored more than 80 and enrolled for DS course.
Ans:
SELECT *
FROM enroll e, student s
WHERE e.usn=s.usn and e.courseno='CS5TH1' and
e.marks >80;
O/P:
O/P:
DEPT TOTAL_STUDENT
---------- -------------
CSE 2
ECE 2
EEE 2
ISE 2
O/P:
BOOK_TITLE
-------------------------
OOMD with UML
7. Demonstrate how you add new textbook to the database and make this book adopted by some
department.
Ans:
Insert into text values (77773,'Internet of Thinking','Pearson','David');
1 row created.
Insert into book_adoption values('CS5TH1',5,77773);
1 row created.
O/P:
8.List any department that has all it’s adopted book published by specific publisher.
Ans:
SELECT c.dept
FROM text t, book_adoption b, course c
WHERE c.courseno=b.courseno and
t.book_ISBN=b.book_ISBN and
t.publisher='Pearson' MINUS
(SELECT c.dept
FROM text t, book_adoption b, course c
WHERE c.courseno=b.courseno and
t.book_ISBN=b.book_ISBN and
t.publisher!='Pearson');
O/P:
DEPT
----------
ECE
9.Produce a list of textbook like course number, book_ISBN, title in alphabetical order of courses
offered by CSE department that use more than 2 books.
Ans:
SELECT c.courseno, t.book_ISBN, t.book_title, count(*)
FROM course c, text t, book_adoption b
WHERE c.courseno=b.courseno and
b.book_ISBN=t.book_ISBN and
dept='CSE' and
c.courseno IN (SELECT ba.courseno
FROM book_adoption ba
GROUP BY ba.courseno
HAVING count(courseno)>=2)
GROUP BY c.courseno,t.book_ISBN, t.book_title;
O/P:
10. Display the maximum and minimum number of students who are registered for any course.
Ans:
SELECT max(count(*)) as max, min(count(*)) as min
FROM student s, course c,enroll e
WHERE s.usn=e.usn and c.courseno=e.courseno
GROUP BY c.cname;
O/P:
MAX MIN
--------- ----------
2 1
CREATION OF TABLE:
INSERTION OF VALUES:
QUERIES:
O/P:
BOOK_ID TITLE
---------- --------
1 DBMS
2 ADBMS
O/P:
O/P:
O/P:
5. Retrieve the name of author who has written more than two books.
Ans:
SELECT author_name
FROM book_authors
GROUP BY author_name
HAVING count (*)>1;
O/P:
AUTHOR_NAME
--------------------
Navathe
6. Get the particular of a borrower or who have borrowed more than three books FROM Jan 2023 to
Jun 2023.
Ans:
SELECT card_no
FROM book_lending
WHERE date_out between '01-Jan-23' and '01-Jun-23'
GROUP BY card_no
HAVING count(*) >3;
O/P:
CARD_NO
----------
101
7. Retrieve the publisher name who has published maximum number of books.
Ans:
SELECT publisher_name,count(*)
FROM book
GROUP BY publisher_name
HAVING count (*) >= (SELECT max(count(*))
FROM book
GROUP BY publisher_name);
O/P:
O/P:
1 row updated.
9. Delete a book in book table update the content of other table to reflect the data manipulation
operation.
Ans:
DELETE FROM book
WHERE book_id=3;
O/P:
SELECT * FROM book;
BOOK_ID AUTHOR_NAME
---------- --------------------
1 Navathe
2 Navathe
4 Edward Angel
5 Galvin
CREATION OF TABLE:
INSERTION OF VALUES:
QUERY:
1.Display the details of all salesmen who are living in Bangalore city .
Ans:
SELECT *
FROM salesman
WHERE city='Bangalore';
O/P:
2. Display the salesman name, customer name ,purchase amount WHERE purchase amount is more
than 50,000 .
Ans:
SELECT name,cust_name,purchase_amt
FROM salesman s, customer c,orders o
WHERE s.salesman_id=o.salesman_id and
c.customer_id=o.customer_id and
o.purchase_amt>50000;
O/P:
3.Display the details of all the customer who belongs to VIP grade.
Ans:
SELECT *
FROM customer
WHERE grade='VIP';
O/P:
O/P:
NAME
-------------
Suresh
O/P:
6. Count the customer with purchase amount above the average purchase amount.
Ans:
SELECT count(*) as no_of_cust
FROM orders
WHERE purchase_amt > (SELECT avg(purchase_amt)
FROM orders);
O/P:
NO_OF_CUST
----------
4
7. Find the name and number of all salesman who have more than one customer.
Ans:
SELECT name,salesman_id
FROM salesman s
WHERE 1 < (SELECT count (*)
FROM customer
WHERE salesman_id=s.salesman_id);
O/P:
NAME SALESMAN_ID
------------ -----------
Rani 1000
8. List all salesman and indicate those who have and don’t have customer in their cities (use union
operator).
Ans:
Ans:
(SELECT s.salesman_id,name,cust_name,commission
FROM salesman s, customer
WHERE s.salesman_id=customer.salesman_id) union
(SELECT s.salesman_id, name,'NO MATCH',commission
FROM salesman s,customer c WHERE s.salesman_id=c.salesman_id and s.city!=c.city)
order by 2 desc;
O/P:
SALESMAN_ID NAME CUST_NAME COMMISSION
----------- -------------------- -------------------- ----------
1003 Suresh Jaya 54
1000 Rani Bhavya 20
1000 Rani Harsha 20
1000 Rani NO MATCH 20
1002 Ramesh Dimpana 45
1001 Raji NO MATCH 40
1001 Raji Sumi 40
1004 Raj Pavi 35
9. Create a view that finds the salesman who has the customer with the highest order of a day.
Ans:
CREATE view sm as
SELECT o.ord_date,s.salesman_id,s.name
FROM salesman s,orders o
WHERE s.salesman_id=o.salesman_id and
o.purchase_amt = (SELECT max (purchase_amt)
FROM orders c
WHERE c.ord_date=o.ord_date);
View created.
10. Demonstrate the delete operation by removing salesman with ID 1000 and all his order must be
deleted.
Ans:
1 row deleted.
6 rows selected.