0% found this document useful (0 votes)
12 views99 pages

DBMS Lab

1. The document describes a DBMS lab management system for managing chats (snacks), customers, and eats (purchases) tables. 2. It includes 16 SQL queries to retrieve information from the tables like listing chats by price, finding customers who ate specific chats, and calculating totals purchased by customers. 3. The last part demonstrates deleting and updating records to reflect data manipulation operations.

Uploaded by

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

DBMS Lab

1. The document describes a DBMS lab management system for managing chats (snacks), customers, and eats (purchases) tables. 2. It includes 16 SQL queries to retrieve information from the tables like listing chats by price, finding customers who ate specific chats, and calculating totals purchased by customers. 3. The last part demonstrates deleting and updating records to reflect data manipulation operations.

Uploaded by

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

DBMS LAB

Chats(Snacks) Management System


Chatz

Customers
Schema
Chats
ChatID Name Price

Customer
CustomerID Name Address PhoneNo

Eats
CustomerID ChatID Qty Date-Time
MySQL tables creation
Chats table

Customer table

Eats table
Data Entry
1. List the details of all chats available

SELECT * FROM chats;

2. List the chat names of all chats available

SELECT Name FROM chats;

3. List only those chats whose price is > 30 Rs/-


SELECT * FROM `chats` where Price >30;
4. List only those chats whose price is > 30 Rs/- and ends with
‘puri’

SELECT * FROM `chats` where Price >30 and


Name like '%puri';
5. List only those chats whose price is either <40 Rs/- or is
“FriedRice’

SELECT * FROM `chats` where


Price<40 or Name='FriedRice';

6. Partition by chatID in Eats and show total qty of each chat


SELECT ChatID,sum(Qty)
FROM `eats`
group by ChatID;
7. Find chatID in Eats whose total qty is < 7
SELECT ChatID,sum(Qty)
FROM `eats`
group by ChatID
having sum(Qty)<7;

8. Print details of chatnames eaten along with date-time


SELECT C.Name,E.`Date-Time`
from chats C,eats E
where C.ChatID=E.chatID;
9. Print details of chatnames eaten along with date-time
and customer name
SELECT C.Name,E.`Date-Time`,CS.Name
from chats C,eats E,customer CS
where C.ChatID=E.chatID and
E.customerID=CS.CustomerID;
10. Find customers who have ate atleast once – Masal
puri or Panipuri or Friedrice SELECT distinct(ct.Name)
from customer ct, chats c, eats e
where ct.CustomerID=e.CustomerID
and c.ChatID=e.ChatID
and c.Name in
('Masalpuri','Panipuri','Friedrice');
11. Find total amount paid so far by each customer

SELECT CS.Name,sum(Qty*Price)
from chats C,eats E,customer CS
where C.ChatID=E.chatID and E.customerID=CS.CustomerID
group by CS.Name;
12. Find highest billing customer
SELECT CS.Name, sum(Qty*Price)
from chats C,eats E,customer CS
where C.ChatID=E.chatID and E.customerID=CS.CustomerID
group by CS.Name
order by sum(Qty*Price) DESC
LIMIT 0,1;
13. Display all customer names along with number of
chats they ate
SELECT c.CustomerID,count(e.ChatID)
FROM customer c LEFT OUTER JOIN eats e
ON c.CustomerID=e.CustomerID
group by c.CustomerID;
14. Display all chats with total quantity ate so far
SELECT c.Name,SUM(e.qty)
FROM chats c LEFT OUTER JOIN eats e
ON c.ChatID=e.ChatID
group by c.ChatID;
15. Find customer who has tasted all chats
select Name
from customer ct
where not exists(
(SELECT chatID
from chats)
EXCEPT
(Select distinct(e.ChatID)
from eats e
where e.CustomerID=ct.CustomerID));
16. Find chats eaten common to customers, Jacobs and
Samantha select *
from chats
where chatID in (
(SELECT e.chatID
FROM customer c,eats e
where c.CustomerID=e.CustomerID
and c.Name='Jacobs')
INTERSECT
(SELECT e.chatID
FROM customer c,eats e
where c.CustomerID=e.CustomerID
and c.Name='Samantha'));
Library Database
Publishers
Books Authors

Copies of
Book
Library Programmes
Book_Copies
Book_Lending
SCHEMA DIAGRAM
TABLES CREATION IN MYSQL
DATA ENTRY
QUERIES

1. Retrieve details of all books in the library – id, title, name


of publisher, authors, number of copies in each
Programme, etc.
SELECT B.book_id,B.title, B.publisher_name,
A.author_name,C.Programme_id,C.No_of_Copies
FROM book B,book_authors A ,book_copies C
WHERE B.book_id=A.book_id
AND B.book_id=C.book_id;
2. Get the particulars of borrowers who have borrowed
more than 3 books, but from Jan 2017 to Jun 2017

SELECT Card_No,COUNT(*)
FROM book_lending
where Date_Out BETWEEN '2017-01-01' and '2017-06-30'
group by Card_No
having count(*)>3;
3. Delete a book in BOOK table. Update the contents of
other tables to reflect this data manipulation operation.

select * from book;


select * from book_authors;
select * from book_copies;
select * from book_lending;
DELETE FROM book where book_id=4;

select * from book;


select * from book_authors;
select * from book_copies;
select * from book_lending;
4. Partition the BOOK table based on year of publication.
Demonstrate its working with a simple query.

SELECT Book_id,Pub_Year,COUNT(Book_id)
OVER (PARTITION BY Pub_Year) AS py
FROM book;
5. Create a view of all books and its number of copies that
are currently available in the Library

CREATE OR REPLACE VIEW BNC AS


SELECT B.book_id, B.Title,SUM(C.No_of_Copies)
FROM BOOK B,book_copies C
where B.book_id=C.book_id
GROUP BY B.book_id;
SALESMAN
SCHEMA DIAGRAM
TABLES CREATION IN MYSQL
DATA ENTRY
1. Count the customers with grades above Bangalore’s
average.

SELECT COUNT(*)
FROM customers
where Grade > (SELECT AVG(Grade)
FROM customers
WHERE City='Bengaluru');
2. Find the name and numbers of all salesman who
had more than one customer.

SELECT S.Salesman_id, S.Name


FROM customers C,salesman S
where C.Salesman_id=S.Salesman_id
GROUP BY C.Salesman_id
HAVING COUNT(*)>1
3. List all the salesman and indicate those who have
and dont have customers in their cities (Use UNION
operation.) (SELECT S.Salesman_id, S.Name,'NO'
FROM SALESMAN S,CUSTOMERS C
WHERE S.Salesman_id=C.Salesman_id and
S.CITY!=C.CITY)
UNION
(SELECT S.Salesman_id, S.Name,'YES'
FROM SALESMAN S,CUSTOMERS C
WHERE S.Salesman_id=C.Salesman_id and
S.CITY=C.CITY);
4. Create a view that finds the salesman who has the
customer with the highest order of a day.
CREATE OR REPLACE VIEW SV AS
SELECT S.Salesman_id, S.Name,
SUM(O.Purchase_Amt) AS SP
FROM ORDERS O,salesman S
WHERE O.Salesman_id=S.Salesman_id
GROUP By O.Customer_id,O.Ord_Date
ORDER BY SP DESC
LIMIT 0,1;
5. Demonstrate the DELETE operation by removing
salesman with id 1000. All his orders must also be deleted.

select * from salesman;


select * from orders;
DELETE FROM salesman WHERE Salesman_id=1000;
select * from salesman;
select * from orders;
ACTOR DIRECTOR
MOVIES

MOVIES_CAS
T RATING
SCHEMA DIAGRAM
TABLES IN MYSQL
DATA ENTRY
1.List the titles of all movies directed by ‘Hitchcock’.

SELECT m.Mov_Title
FROM movies m , director d
where m.Dir_id=d.Dir_id
and d.Dir_Name='Hitchcock';
2. Find the movie names where one or more actors
acted in two or more movies.
select m.mov_title
from movies m, movie_cast mc
where m.Mov_id=mc.Mov_id
and mc.Act_id in (
SELECT act_id
FROM movie_cast c
GROUP BY act_id
HAVING count(c.Mov_id)>2);
3. List all actors who acted in a movie before 2000 and also
in a movie after 2015 (use JOIN operation).
select *
from actor
where act_id in (
select mc.act_id
from movies m join movie_cast mc
on m.Mov_id=mc.Mov_id
where m.Mov_Year<2000
intersect
select mc.act_id
from movies m join movie_cast mc
on m.Mov_id=mc.Mov_id
where m.Mov_Year>2015);
4. Find the title of movies and number of stars for each
movie that has at least one rating and find the highest
number of stars that movie received. Sort the result by
movie title.
select m.Mov_Title, avg(r.Rev_Stars), max(r.Rev_stars)
from movies m,rating r
where m.mov_id=r.mov_id
group by m.mov_id
order by m.Mov_title;
5. Update rating of all movies directed by ‘Steven
Spielberg’ to 5
update rating set Rev_Stars=5
where Mov_id in (
select m.Mov_id
from movies m ,director d
where m.Dir_id=d.Dir_id
and d.Dir_name='Steven Spielberg');
SEMSEC
STUDENT

COURSES
SCHEMA DIAGRAM
TABLES IN MYSQL
DATA ENTRY
1.List all the student details studying in fourth semester
‘C’ section.

SELECT S.USN, S.SName,S.Phone, S.Gender


FROM Student S, SEMSEC SS, CLASS C
WHERE S.USN=C.USN and SS.SSID=C.SSID
AND SS.Sem=4 and SS.SEC='C'
2. Compute the total number of male and female
students in each semester and in each section

SELECT SS.Sem,SS.Sec,S.Gender, COUNT(*)


FROM Student S, SEMSEC SS, CLASS C
WHERE S.USN=C.USN and SS.SSID=C.SSID
GROUP BY SS.Sem,SS.Sec, S.Gender
3. Create a view of Test1 marks of student USN
‘1BI15CS101’ in all Courses.

CREATE OR REPLACE VIEW IAM AS


SELECT SUBCODE,TEST1
FROM iamarks
where USN='1BI15CS101'
4. Calculate the FinalIA (average of best two test marks)
and update the corresponding table for all students.

UPDATE iamarks i
SET i.FinalIA=(SELECT
GREATEST((Test1+Test2)/2,(Test2+Test3)/2,
(Test1+Test3)/2)
FROM iamarks
WHERE i.USN=USN and
i.Subcode=Subcode);
5. Categorize students based on the following criterion:
If FinalIA = 17 to 20 then CAT = ‘Outstanding’
If FinalIA = 12 to 16 then CAT = ‘Average’
If FinalIA< 12 then CAT = ‘Weak’
Give these details only for 8th semester A, B, and C section
students.
SELECT S.USN, S.SName,
(CASE
WHEN i.FinalIA BETWEEN 17 and 20 THEN 'OUTSTANDING
WHEN i.FinalIA BETWEEN 12 and 16 THEN 'AVERAGE'
ELSE 'WEAK'
END) AS CAT
FROM Student S, semsec SS, iamarks i
WHERE S.USN=i.USN and SS.SSID=i.SSID
and SS.Sem=8 and SS.Sec IN ('A','B','C')
COMPANY DB

EMPLOYEE DEPARTMENTS

WORKS_ON DLOCATION
SCHEMA DIAGRAM
TABLES IN MYSQL
DATA ENTRY
1. Make a list of all project numbers for projects that involve an
employee whose last name is ‘Scott‟, either as a worker or as a
manager of the department that controls the project.
SELECT w.Pno
from employee e,works_on w
where e.SSN=w.SSN
and e.Name like '%Scott'
UNION
SELECT p.Pno
from employee e,project p,department d
where d.mgrssn=e.ssn
and p.dno=d.dno
and e.Name like '%Scott';
2. Show the resulting salaries if every employee working on the
‘IoT’ project is given a 10 percent raise.

SELECT e.ssn, e.Name, e.salary as old_sal,


e.salary*1.1 as new_sal
from employee e,project p,works_on w
where e.ssn=w.ssn
and p.pno=w.pno
and p.pname='IoT';
3. Find the sum of the salaries of all employees of the ‘Accounts’
department, as well as the maximum salary, the minimum salary,
and the average salary in this department

SELECT SUM(E.Salary),MIN(E.Salary),
MAX(E.Salary),AVG(E.Salary)
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.DNO=E.DNO
AND D.Dname='Accounts';
4. Retrieve the name of each employee who works on all the
projects controlled by department number 5 (use NOT EXISTS
operator).
SELECT E.Name
FROM EMPLOYEE E
WHERE NOT EXISTS((SELECT PNO FROM
PROJECT WHERE DNO='5') EXCEPT
(SELECT PNO
FROM WORKS_ON
WHERE E.SSN=SSN));
5. For each department that has more than five employees,
retrieve the department number and the number of its
employees who are making more than Rs. 6,00,000
select Dno, COUNT(SSN)
from employee
where Dno IN (
SELECT e.DNo
from employee e
group by e.DNo
having count(e.SSN)>5)
and Salary>600000;

You might also like