SQL Assignment for Analytics Club IITB
SQL Assignment for Analytics Club IITB
To create a movie database, define tables for actors, movies, and actor roles with appropriate constraints. For example: 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 actors(AID)); Primary keys like actors' AID and movies' MID uniquely identify records. Foreign keys ensure referential integrity by linking roles to actors and movies .
You can determine which instructors have never taught or been scheduled to teach a course with the query: SELECT ID, name FROM instructor WHERE ID NOT IN (SELECT ID FROM teaches); This query is important as it helps identify instructors who are listed in the system but have no teaching assignments, which might indicate data entry errors or inactive personnel .
Challenges include ensuring data integrity and establishing clear relationships among entities. Foreign key constraints address these by enforcing referential integrity, preventing orphan records, and ensuring that relationships between tables—such as actors and their roles in movies—are maintained correctly .
To find the names of all instructors from the Biology department, you can use the SQL query: SELECT name FROM instructor WHERE dept_name = 'Biology'; This query considers the 'dept_name' attribute to filter records specifically for the Biology department .
Use the query: 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; This query lists all movies Charlie Chaplin acted in and counts his roles in each, revealing his versatility in taking on multiple roles within the same movie .
The query SELECT sum(credits) FROM takes, course WHERE ID = 25946 AND takes.course_id = course.course_id aggregates credits for a student, providing insights into their workload and academic commitments. This data is useful for advising on course loads, measuring academic progress, and planning graduation timelines .
To insert data, use statements like INSERT INTO actors VALUES ('24968N', 'Charlie Chaplin'); These statements populate the database with essential entities, allowing subsequent queries and analyses of relationships like actor roles. Accurate data insertion is critical for maintaining database integrity and supporting meaningful applications .
To list all actors and their movie titles, including those with no roles, the query is: 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); This provides a complete view of actor participation, highlighting both engagements and inactivity, crucial for thorough data reporting and analysis .
You can find students who have taken any Computer Science course with the query: 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; Using DISTINCT is beneficial as it prevents duplicate names, making the result set cleaner and more accurate, which is particularly useful in reporting .
The query to find total credits for each student along with their ID is: SELECT ID, SUM(credits) FROM takes, course WHERE takes.course_id = course.course_id GROUP BY ID; Aggregation with SUM is necessary to calculate the total credits across all courses a student is registered for, providing insights into their academic workload .