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

LS SQL Assignment

Analytics Club, IITB


22B1535-S.S.GAYATHRI

Set 1 Queries:
1. Find the names of all the instructors from Biology department

SELECT name FROM instructor WHERE dept_name = 'Biology';

2. Find the names of courses in Computer science department which have 3


credits

SELECT title FROM course WHERE dept_name = 'Comp. Sci.' and credits = 3;

3. For the student with ID 12345 (or any other value), show all course id and
title of all courses registered for by the student.

SELECT takes.course_id, title FROM takes, course WHERE ID = 25946


AND takes.course_id = course.course_id;

4. As above, but show the total number of credits for such courses (taken by
that student). Don’t display the tot creds value from the student table, you
should use SQL aggregation on courses taken by the student.

SELECT sum(credits) FROM takes, course WHERE ID = 25946 AND


takes.course_id = course.course_id;

5. As above, but display the total credits for each of the students, along with
the ID of the student; don’t bother about the name of the student. (Don’t
bother about students who have not registered for any course, they can be
omitted)

SELECT ID, SUM (credits) FROM takes, course WHERE takes.course_id =


course.course_id GROUP BY ID;

6. Find the names of all students who have taken any Comp. Sci. course
ever (there should be no duplicate names)

SELECT DISTINCT name FROM takes, course, student WHERE


takes.course_id = course.course_id AND course.dept_name = 'Comp. Sci.'
AND takes.ID = student.ID;

7. Display the IDs of all instructors who have never taught a course. Interpret
“taught” as “taught” or “is scheduled to teach”.

SELECT ID FROM instructor WHERE ID NOT IN (SELECT ID FROM


teaches);

8. As above, but display the names of the instructors also, not just the IDs.

SELECT ID, name FROM instructor WHERE ID NOT IN (SELECT ID FROM


teaches);

Set 2 Queries:
You need to create a movie database. Create three tables, one for
actors(AID, name), one for movies(MID, title) and one for actor_role(MID,
AID, rolename). Use appropriate data types for each of the attributes, and
add appropriate primary/foreign key constraints.

create table actors (AID varchar(20) , name varchar(20) NOT NULL,


PRIMARY KEY(AID));

create table movies (MID varchar(20), title varchar(20) NOT NULL,


PRIMARY KEY(MID));

create table actor_role (MID varchar(20), AID varchar(20), rolename


varchar(20) NOT NULL, FOREIGN KEY (MID) references movies (MID),
FOREIGN KEY (AID) references movies (AID));

9. Insert data to the above tables (approx 3 to 6 rows in each table), including
data for actor ”Charlie Chaplin”, and for yourself (using your roll number as
ID).

INSERT INTO actors VALUES ('22B1535 ', S.S.GAYATHRI');


INSERT INTO actors VALUES ('24968N ', 'Charlie Chaplin ');
INSERT INTO actors VALUES ('67829H ', Jack Mink ');
INSERT INTO actors VALUES ('19295K ', 'Sheila Hopper ');
INSERT INTO movies VALUES ('123 ', ‘Death at the Park ');
INSERT INTO movies VALUES ('156 ', ‘Allegiant ');
INSERT INTO movies VALUES ('143 ', 'Garfield, The Great');
INSERT INTO movies VALUES ('100 ', 'Inception ');
INSERT INTO actor_role VALUES ('100 ', '67829H ', 'Missile man');
INSERT INTO actor_role VALUES ('143 ', '19295K ', 'Garfield ');
INSERT INTO actor_role VALUES ('123 ', '24968N ', 'High school student');
INSERT INTO actor_role VALUES ('156 ', '22B1535', ‘Tris ');
10. Write a query to list all movies in which actor ”Charlie Chaplin” has acted,
along with the number of roles he had in that movie.

SELECT movies.title, COUNT(rolename) FROM movies, actors, actor_role


WHERE actor_role.AID = actors.AID AND actor_role.MID = movies.MID
AND actors.name = 'Charlie Chaplin' GROUP BY movies.title;

11. Write a query to list all actors who have not acted in any movie

SELECT * FROM actors WHERE AID NOT IN (SELECT AID FROM actor_role);

12. List names of actors, along with titles of movies they have acted in. If
they have not acted in any movie, show the movie title as null.

SELECT name,movies.title FROM movies, actors, actor_role WHERE


actor_role.AID = actors.AID AND actor_role.MID = movies.MID union
SELECT name, null FROM actors WHERE AID NOT IN (SELECT AID
FROM actor_role);

You might also like