Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Assignment SQL

A. The schema of a relational Database is the following:

Student ( Snum int, Name varchar(30), Major varchar(10), Level char(2), Age int );
Class ( Name varchar(10), Time Datetime, Room char(5), Fid int );
Enrolled ( Snum int, ClassName varchar(10));
Faculty (Fid int, Name char(10), Dept char(10));

Write the following queries in SQL. No duplicates should be printed in any of the answers.
Write algebraic expressions

1. Find the names of all CS Majors (Major = "CS") who are enrolled in the course
"Math92".
2. Find the names of all CS Majors (Major = "CS") who are enrolled in the course
"Math92" and are older than some History freshman (Level="FR").
3. Find the names of all classes that either meet in room R128 or are taught by
"H.Merlin".
4. Find the names of all pairs of students who are enrolled in some class together.
5. Find the names of all pairs of students who are enrolled in two classes that meet at the
same time (including pairs of students who are enrolled in the same class).
6. Find the names of faculty members who teach in every room in which some class is
taught in the time period "MW9-10".
7. Find the names of faculty members such that the combined enrollment of the courses
that they teach is less than 5.
8. Print the Level and the average age of students for that Level, for each Level.
9. Find the name of the student who is enrolled in the most classes.
10. Find the names of all students who are not enrolled in any class taught by "H.Merlin".
1. Select DISTINCT s.Name From Student s 5. SELECT DISTINCT S1.Name, S2.Name
JOIN Enrolled e ON s.Snum = e.Snum FROM Student S1, Student S2
WHERE s.Major = 'CS' WHERE S1.Snum < S2.Snum
AND e.ClassName = 'Math92'; AND S1.Snum IN (SELECT E1.Snum FROM Enrolled E1, Class C1 WHERE E1.ClassName = C1.Name)
AND S2.Snum IN (SELECT E2.Snum FROM Enrolled E2, Class C2 WHERE E2.ClassName = C2.Name)
2. SELECT DISTINCT s.Name FROM Student s AND C1.Time = C2.Time;
JOIN Enrolled e ON s.Snum = e.Snum
WHERE s.Major = 'CS'
AND e.ClassName = 'Math92'
AND s.Age > ANY( 6. SELECT DISTINCT F.Name FROM Faculty F
SELECT Age
8. SELECT Level, AVG(Age) as AverageAge
FROM Student
FROM Student
WHERE Major = 'History' AND Level = 'FR'
GROUP BY Level
);

3.SELECT Name 9. SELECT S.Name


FROM Class FROM Student S
WHERE Room = 'R128' JOIN Enrolled E ON S.Snum = E.Snum
GROUP BY S.Snum, S.Name
UNION ORDER BY COUNT(E.ClassName) DESC
LIMIT 1;
SELECT c.Name
FROM Class c 10. SELECT Name
JOIN Faculty f ON c.Fid = f.Fid FROM Student
WHERE f.Name = 'H.Merlin'; WHERE Snum NOT IN (
4. SELECT DISTINCT S1.Name, S2.Name SELECT DISTINCT E.Snum
FROM Student S1, Student S2 FROM Enrolled AS E
WHERE S1.Snum < S2.Snum JOIN Class AS C ON E.ClassName = C.Name
AND S1.Snum IN (SELECT E1.Snum FROM Enrolled E1) JOIN Faculty AS F ON C.Fid = F.Fid
AND S2.Snum IN (SELECT E2.Snum FROM Enrolled E2) WHERE F.Name = 'H.Merlin'
AND E1.ClassName = E2.ClassName; );
B. A relational movie database.The database consists of the following five tables
ACTOR (id, fname, lname, gender)
MOVIE (id, name, year, rank)
DIRECTOR (id, fname, lname)
CAST (pid, mid, role)
MOVIE_DIRECTOR (did, mid)

id column in ACTOR, MOVIE & DIRECTOR tables is a key for the respective table.
CAST.pid refers to ACTOR.id, CAST.mid refers to MOVIE.id
MOVIE_DIRECTOR.did refers to DIRECTOR.id and MOVIE_DIRECTOR.mid refers to
MOVIE.id

Write the following queries in SQL. No duplicates should be printed in any of the answers.
Write algebraic expressions

1. List all the actors who acted in at least one film in 2nd half of the 19th century and in
at least one film in the 1st half of the 20th century
2. List all the directors who directed a film in a leap year
3. List all the movies that have the same year as the movie 'Shrek (2001)', but a better
rank. (Note: bigger value of rank implies a better rank)
4. List first name and last name of all the actors who played in the movie 'Officer 444
(1926)'
5. List all directors in descending order of the number of films they directed
6. Find the film(s) with the largest cast (with the size of the cast)
7. Find the film(s) with the smallest cast (with the size of the cast)
8. Find all the actors who acted in films by at least 10 distinct directors (i.e. actors who
worked with at least 10 distinct directors).
9. Find all actors who acted only in films before 1960.
10. Find the films with more women actors than men.
11. For every pair of male and female actors that appear together in some film, find the
total number of films in which they appear together. Sort the answers in decreasing
order of the total number of films.
1. SELECT DISTINCT fname, lname FROM ACTOR
WHERE id IN(
SELECT C.pid FROM CAST C
WHERE C.mid IN(
SELECT M.id FROM MOVIE M
WHERE EXIST(M.year >= 1850 AND M.year <= 1899)
AND EXIST(M.year >= 1900 AND M.year <= 1949)
)
)

2. SELECT DISTINCT fname, lname FROM DIRECTOR


WHERE id IN(
SELECT MD.did FROM MOVIE_DIRECTOR MD
WHERE MD.mid IN(
SELECT M.id FROM MOVIE M
WHERE (M.year % 4 =0 AND M.year % 100 != 0) OR (M.year % 400 = 0)
)
)

You might also like