DBMS Lab
DBMS Lab
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 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
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 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
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.
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.
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 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;