DBAssignment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Q1.

1. SELECT DISTINCT s.St_Name

FROM Student s

JOIN Enrolled e ON s.student_id = e.student_id

JOIN Class c ON e.cname = c.name

JOIN Faculty f ON c.fid = f.fid

WHERE s.level = 'JR' AND f.fname = 'Dr. Ashok';

2. SELECT MAX(age) AS oldest_age

FROM Student

WHERE major = 'History'

OR student_id IN (SELECT e.student_id

FROM Enrolled e

JOIN Class c ON e.cname = c.name

JOIN Faculty f ON c.fid = f.fid

WHERE f.fname = 'Dr. Ashok');

3. SELECT name

FROM Class

WHERE room = 'R128'

OR name IN (SELECT cname

FROM Enrolled

GROUP BY cname

HAVING COUNT(*) >= 5);


4. SELECT DISTINCT St_Name
FROM Student
JOIN Enrolled AS e1 ON Student.student_id = e1.student_id
JOIN Class AS c1 ON e1.cname = c1.name
JOIN Class AS c2 ON c1.meets_at = c2.meets_at
JOIN Enrolled AS e2 ON e1.student_id = e2.student_id AND c1.name <> c2.name
WHERE e1.cname <> e2.cname;

5. SELECT fname
FROM Faculty
WHERE NOT EXISTS (
SELECT DISTINCT room
FROM Class
WHERE room NOT IN (
SELECT DISTINCT room
FROM Class
JOIN Faculty ON Class.fid = Faculty.fid
)
);

6. SELECT fname

FROM Faculty

WHERE fid IN (

SELECT f.fid

FROM Class AS c

JOIN Faculty AS f ON c.fid = f.fid

LEFT JOIN Enrolled AS e ON c.name = e.cname

GROUP BY f.fid

HAVING SUM(COALESCE(e.student_id, 0)) < 5

);

7. SELECT level, AVG(age) AS average_age

FROM Student

GROUP BY level;
8. SELECT level, AVG(age) AS average_age

FROM Student

WHERE level <> 'JR'

GROUP BY level;

9. SELECT Faculty.fname, COUNT(Class.name) AS total_classes_taught

FROM Faculty

JOIN Class ON Faculty.fid = Class.fid

WHERE Class.room = 'R128'

GROUP BY Faculty.fname

HAVING COUNT(DISTINCT Class.room) = 1;

10. SELECT St_Name

FROM (

SELECT St_Name, COUNT(*) AS num_classes

FROM Student

JOIN Enrolled ON Student.student_id = Enrolled.student_id

GROUP BY St_Name

) AS class_counts

WHERE num_classes = (

SELECT MAX(num_classes)

FROM (

SELECT COUNT(*) AS num_classes

FROM Enrolled

GROUP BY student_id

) AS max_counts

);
Q2:

Q3:

a)
-> The given table satisfies the requirements of 1NF since each column contains atomic
values, and there are no repeating groups.

->In the given table, both "EmpID" and "TrainerID" together form the composite primary
key. "Training" and "Score" are dependent on this composite key. Therefore, the table
meets the requirements of 2NF.

However, it does not satisfy 3rd Normal Form (3NF) The table violates 3NF because the
attribute Training depends on a non-key attribute (TrainerID), which itself depends on
the primary key (EmpID).

b)
To normalize the table up to Boyce-Codd Normal Form (BCNF), we need to eliminate the
transitive dependency by decomposing the table.

Will create two tables:

EmpTrainer (EmpID, TrainerID)


TrainerTraining (TrainerID, Training, Score)
EmpTrainer
EmpID Primary Key
TrainerID Foreign Key

TrainerTraining
TrainerID Primary Key
Training
Score

In the EmpTrainer table, EmpID is the primary key, and TrainerID is a foreign key
referencing the TrainerID in the TrainerTraining table.

Now both tables are in BCNF. EmpTrainer table has a single candidate key (EmpID) and
TrainerTraining table has a single candidate key (TrainerID).

You might also like