0% found this document useful (0 votes)
4 views8 pages

DBMS SQL Answers 11 To 15

The document contains a series of SQL queries related to various database tables including Student, Class, Faculty, Supplier, Product, Customer, and Employee. Each question provides specific SQL commands to retrieve information such as student names, class details, supplier information, and employee statistics. The queries demonstrate various SQL functionalities like joins, unions, and aggregate functions to extract meaningful data from the databases.

Uploaded by

gunadevasai
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)
4 views8 pages

DBMS SQL Answers 11 To 15

The document contains a series of SQL queries related to various database tables including Student, Class, Faculty, Supplier, Product, Customer, and Employee. Each question provides specific SQL commands to retrieve information such as student names, class details, supplier information, and employee statistics. The queries demonstrate various SQL functionalities like joins, unions, and aggregate functions to extract meaningful data from the databases.

Uploaded by

gunadevasai
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/ 8

Answers from Question 11 onwards (DBMS Assignment & Tutorials - 2)

Q11. SQL Queries (Based on Student, Class, Enrolled, Faculty Relations)

i. Names of all Juniors (level = JR) who are taught by I. Teach:

SELECT DISTINCT S.Sname

FROM Student S, Enrolled E, Class C, Faculty F

WHERE S.Snum = E.Snum AND E.Cname = C.Cname AND C.fid = F.fid

AND S.level = 'JR' AND F.fname = 'I. Teach';

ii. Names of all classes that either meet in room R128 or have five or more students enrolled:

SELECT DISTINCT Cname

FROM Class

WHERE room = 'R128'

UNION

SELECT Cname

FROM Enrolled

GROUP BY Cname

HAVING COUNT(Snum) >= 5;

iii. For all levels except JR, print level and average age:

SELECT level, AVG(age) AS average_age

FROM Student

WHERE level <> 'JR'

GROUP BY level;

iv. Faculty who taught only in room R128, with count of classes taught:

SELECT F.fname, COUNT(*) AS total_classes


FROM Faculty F, Class C

WHERE F.fid = C.fid

GROUP BY F.fid, F.fname

HAVING COUNT(DISTINCT room) = 1 AND MAX(room) = 'R128';

v. Students not enrolled in any class:

SELECT Sname

FROM Student

WHERE Snum NOT IN (SELECT Snum FROM Enrolled);

Q12. SQL Queries (Supplier, Product, Shipments)

i. Suppliers who supply product # P2:

SELECT DISTINCT Sname

FROM Supplier

WHERE Sno IN (SELECT Sno FROM Shipments WHERE Pno = 'P2');

ii. Suppliers not supplying any product supplied by Supplier #S2:

SELECT Sname

FROM Supplier

WHERE Sno NOT IN (

SELECT DISTINCT S1.Sno

FROM Shipments S1

WHERE S1.Pno IN (

SELECT Pno FROM Shipments WHERE Sno = 'S2'

);
iii. Product numbers supplied by more than one supplier:

SELECT Pno

FROM Shipments

GROUP BY Pno

HAVING COUNT(DISTINCT Sno) > 1;

iv. Product number, max & min quantity supplied per product:

SELECT Pno, MAX(Qty) AS max_qty, MIN(Qty) AS min_qty

FROM Shipments

GROUP BY Pno;

v. Supplier numbers with status less than current max:

SELECT Sno

FROM Supplier

WHERE Status < (SELECT MAX(Status) FROM Supplier);

Q13. SQL Queries (OrderDatabase: SALESMAN, CUSTOMER, ORDERS)

1. Count customers with grades above Bangalore's average:

SELECT COUNT(*)

FROM Customer

WHERE Grade > (

SELECT AVG(Grade)

FROM Customer

WHERE City = 'Bangalore'

);

2. Salesmen with more than one customer:


SELECT Salesman_id, COUNT(*) AS customer_count

FROM Customer

GROUP BY Salesman_id

HAVING COUNT(*) > 1;

3. Salesmen with/without customers in their cities (using UNION):

(SELECT Name

FROM Salesman S

WHERE EXISTS (

SELECT * FROM Customer C

WHERE S.Salesman_id = C.Salesman_id AND S.City = C.City

))

UNION

(SELECT Name

FROM Salesman S

WHERE NOT EXISTS (

SELECT * FROM Customer C

WHERE S.Salesman_id = C.Salesman_id AND S.City = C.City

));

4. View for salesman with highest order of the day:

CREATE VIEW TopSalesman AS

SELECT S.Name

FROM Salesman S, Orders O

WHERE S.Salesman_id = O.Salesman_id

AND O.Purchase_Amt = (

SELECT MAX(Purchase_Amt)
FROM Orders

WHERE Ord_Date = O.Ord_Date

);

5. DELETE salesman ID 1000 and his orders:

DELETE FROM Orders

WHERE Salesman_id = 1000;

DELETE FROM Salesman

WHERE Salesman_id = 1000;

Q14. SQL Queries (works, lives, located-In)

i. People working at Wipro with their city:

SELECT W.Person_name, L.City

FROM works W, lives L

WHERE W.Person_name = L.Person_name AND W.Company_name = 'Wipro';

ii. People who do not work for Infosys:

SELECT Person_name

FROM works

WHERE Company_name <> 'Infosys';

iii. People earning more than all Oracle employees:

SELECT Person_name

FROM works

WHERE Salary > ALL (

SELECT Salary FROM works WHERE Company_name = 'Oracle'


);

iv. People who work and live in same city:

SELECT w.Person_name

FROM works w, lives l, located-In loc

WHERE w.Person_name = l.Person_name AND w.Company_name = loc.Compny_name

AND l.City = loc.City;

v. Average, max, min salary per company:

SELECT Company_name, AVG(Salary) AS avg_salary,

MAX(Salary) AS max_salary, MIN(Salary) AS min_salary

FROM works

GROUP BY Company_name;

Q15. SQL Queries (Company DB: EMPLOYEE, DEPARTMENT, etc.)

i. Projects in Bangalore with dept and manager details:

SELECT P.Pnumber, P.Dnum, E.Lname, E.Address, E.Bdate

FROM PROJECT P, DEPARTMENT D, EMPLOYEE E

WHERE P.Plocation = 'Bangalore' AND P.Dnum = D.Dnumber AND D.Mgr_ssn = E.Ssn;

ii. Employees with more than one dependent:

SELECT E.Fname, E.Lname

FROM EMPLOYEE E

WHERE E.Ssn IN (

SELECT Essn

FROM DEPENDENT

GROUP BY Essn
HAVING COUNT(*) > 1

);

iii. Project name and total hours per week:

SELECT P.Pname, SUM(W.Hours) AS total_hours

FROM PROJECT P, WORKS_ON W

WHERE P.Pnumber = W.Pno

GROUP BY P.Pname;

iv. Employees who work on all projects controlled by Research dept:

SELECT E.Fname, E.Lname

FROM EMPLOYEE E

WHERE NOT EXISTS (

SELECT P.Pnumber

FROM PROJECT P, DEPARTMENT D

WHERE P.Dnum = D.Dnumber AND D.Dname = 'Research'

AND P.Pnumber NOT IN (

SELECT W.Pno

FROM WORKS_ON W

WHERE W.Essn = E.Ssn

);

v. Employees in dept with highest paid employee:

SELECT E1.Fname, E1.Lname

FROM EMPLOYEE E1

WHERE E1.Dno = (
SELECT E2.Dno

FROM EMPLOYEE E2

WHERE E2.Salary = (SELECT MAX(Salary) FROM EMPLOYEE)

);

You might also like