0% found this document useful (0 votes)
9 views7 pages

DBMS AnnualFile

Uploaded by

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

DBMS AnnualFile

Uploaded by

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

Database Management System 2024-2025

Answer:
SQL Query
a. Create the tables

CREATE TABLE Colleges (

cname VARCHAR2(20) PRIMARY KEY,

city VARCHAR2(30) NOT NULL,

address VARCHAR2(100),

phone VARCHAR2(15),

afdate DATE

);

CREATE TABLE Staffs (

sid VARCHAR2(10) PRIMARY KEY,

sname VARCHAR2(50) NOT NULL,

saddress VARCHAR2(100),

contacts VARCHAR2(15)

);

CREATE TABLE StaffJoins (

sid VARCHAR2(10) REFERENCES Staffs(sid),

cname VARCHAR2(20) REFERENCES


Colleges(cname),

dept VARCHAR2(50),

DOJ DATE,

1|Page
Shreyansh Yeole BCA II
Database Management System 2024-2025

post VARCHAR2(50),

salary NUMBER(10, 2),

PRIMARY KEY (sid, cname)

);

CREATE TABLE Teachings (

sid VARCHAR2(10) REFERENCES Staffs(sid),

class VARCHAR2(20),

paperid VARCHAR2(10) REFERENCES


Subjects(paperid),

fsession DATE,

tsession DATE,

PRIMARY KEY (sid, paperid, fsession)

);

CREATE TABLE Subjects (

paperid VARCHAR2(10) PRIMARY KEY,

subject VARCHAR2(50),

paperno NUMBER,

papername VARCHAR2(100)

);

b. Insert 10 rows (example data)

c. List the names of the teachers teaching computer subjects


SELECT s.sname

FROM Staffs s

JOIN Teachings t ON s.sid = t.sid

JOIN Subjects sub ON t.paperid = sub.paperid

WHERE sub.subject = 'Computer Science';

2|Page
Shreyansh Yeole BCA II
Database Management System 2024-2025

d. List the names and cities of all staff working in your college (assuming 'C1')
Select s.sname, c.city

FROM staffJoins sj

JOIN Colleges c ON c.cname = sj.cname

JOIN Staffs s ON s.sid = sj.sid

Where c.cname = 'C1';

e. List the names and cities of all staff working in your college who earn more than 15,000
SELECT s.sname, c.city

FROM Staffs s

JOIN StaffJoins sj ON s.sid = sj.sid

JOIN Colleges c ON sj.cname = c.cname

WHERE sj.cname = 'C1' AND sj.salary>15000;

f. Find the staffs whose names start with 'M' or 'R' and end with 'A' and/or are 7 characters long
SELECT sname

FROM Staffs

WHERE

(sname LIKE 'M%A' OR sname LIKE 'R%A')

OR LENGTH(sname) = 7;

g. Find the staffs whose date of joining is 2005


SELECT sname

FROM Staffs s

JOIN StaffJoins sj ON s.sid = sj.sid

WHERE EXTRACT(YEAR FROM sj.DOJ) = 2005;

h. Modify the database so that staff 'N1' now works in 'C2' College

UPDATE StaffJoins

SET cname = 'C2'

WHERE sid = 'S1';

i. List the names of subjects that 'S1' teaches in this session or all sessions
3|Page
Shreyansh Yeole BCA II
Database Management System 2024-2025

Select sb.subject

From teachings t

JOIN subjects sb ON sb.paperid = t.paperid

Where t.sid = 'S1'

j. Find the classes that 's1' does not teach in the present session

SELECT *

FROM Teachings

WHERE sid != 'S1' AND fsession = TO_DATE('2023-01-01', 'YYYY-


MM-DD');

k. Find the colleges that have the most number of staff

SELECT c.cname, COUNT(sj.sid) AS staff_count

FROM Colleges c

JOIN StaffJoins sj ON c.cname = sj.cname

GROUP BY c.cname

ORDER BY staff_count DESC;

l. Find the staffs who earn a higher salary than the average salary of their
college.

SELECT s.sname, sj.salary, sj.cname

FROM Staffs s

JOIN StaffJoins sj ON s.sid = sj.sid

WHERE sj.salary > (

SELECT AVG(salary)

FROM StaffJoins sj2

WHERE sj2.cname = sj.cname

);

m. Find the colleges whose average salary is more than the average salary of C2.

SELECT c.cname

FROM Colleges c

JOIN StaffJoins sj ON c.cname = sj.cname

GROUP BY c.cname

HAVING AVG(sj.salary) > (

SELECT AVG(salary)

FROM StaffJoins

WHERE cname = 'C2'


4|Page
Shreyansh Yeole BCA II
Database Management System 2024-2025

);

n. Find the college with the smallest payroll.

SELECT cname, total_salary

FROM (

SELECT c.cname, SUM(sj.salary) AS total_salary

FROM Colleges c

JOIN StaffJoins sj ON c.cname = sj.cname

GROUP BY c.cname

ORDER BY total_salary ASC

WHERE ROWNUM = 1;

o. Find the colleges where the total salary is greater than the average salary of all colleges.

SELECT c.cname

FROM Colleges c

JOIN StaffJoins sj ON c.cname = sj.cname

GROUP BY c.cname

HAVING SUM(sj.salary) > (

SELECT AVG(total_salary)

FROM (

SELECT SUM(salary) AS total_salary

FROM StaffJoins

GROUP BY cname

);

p. List the maximum, average, and minimum salary of each college.

SELECT c.cname,

MAX(sj.salary) AS max_salary,

AVG(sj.salary) AS avg_salary,

MIN(sj.salary) AS min_salary

FROM Colleges c

JOIN StaffJoins sj ON c.cname = sj.cname

GROUP BY c.cname;

q. Acquire details of staff by name in a college or each college.

5|Page
Shreyansh Yeole BCA II
Database Management System 2024-2025

SELECT s.sid, s.sname, s.saddress, s.contacts, sj.cname, sj.dept, sj.DOJ, sj.post, sj.salary

FROM Staffs s

JOIN StaffJoins sj ON s.sid = sj.sid

WHERE s.sname = 'Maria' OR sj.cname = 'C3';

r. Find the names of staff who earn more than each staff of C2 College.

SELECT s.sname, sj.salary

FROM Staffs s

JOIN Staffjoins sj ON sj.sid = s.sid

WHERE sj.salary> ALL(

SELECT sj2.salary

FROM StaffJoins sj2

WHERE sj2.cname = 'C2'

s. Give all principals a 10% salary increase unless it exceeds 20,000; then give a 5% increase.

UPDATE StaffJoins

SET salary = CASE

WHEN salary * 1.1 <= 20000 THEN salary * 1.1

ELSE salary * 1.05

END

WHERE post = 'Principal';

t. Find all staff who do not work in the same city as the colleges they are assigned to.

SELECT s.sname, c.city AS college_city, s.saddress AS staff_city

FROM Staffs s

JOIN StaffJoins sj ON s.sid = sj.sid

JOIN Colleges c ON sj.cname = c.cname

WHERE s.saddress != c.city;

u. List the names of teachers teaching in more than one department.

6|Page
Shreyansh Yeole BCA II
Database Management System 2024-2025

SELECT *

FROM Staffs s

JOIN StaffJoins sj ON s.sid = sj.sid

GROUP BY s.sname

HAVING COUNT(DISTINCT sj.dept) > 1;

7|Page
Shreyansh Yeole BCA II

You might also like