Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

create table actors ( actorid INTEGER PRIMARY KEY, firstName VARCHAR(20) NOT NULL, Last Name VARCHAR(20) NOT NULL, yearofBirth INTEGER ); create table movies movieId INTEGER

image text in transcribed

create table actors ( actorid INTEGER PRIMARY KEY, firstName VARCHAR(20) NOT NULL, Last Name VARCHAR(20) NOT NULL, yearofBirth INTEGER ); create table movies movieId INTEGER PRIMARY KEY, title VARCHAR(50) NOT NULL, year INTEGER NOT NULL, director VARCHAR(20) NOT NULL create table movie_cast ( actorID INTEGER NOT NULL REFERENCES actors(actorid), movieId INTEGER NOT NULL REFERENCES movies(movieId), CONSTRAINT cast_pk PRIMARY KEY(actorid, movieId) ); - actors insert into actors values (1, 'Carrie', 'Fisher', 1956); insert into actors values (2, 'Mark', 'Hamill', 1951); insert into actors values 3, 'Harrison', 'Ford', 1944); insert into actors values (4, 'Billy Dee', 'Williams', 1937); insert into actors values (5, Danny', 'Trejo', 1937); insert into actors values (6, Michelle', 'Rodriguez', 1978); insert into actors values (7, Jessica', 'Alba', NULL); insert into actors values (8, 'John', 'Doe', NULL); -- movies insert into movies values (1, 'Star Wars', 1977, 'George Lucas' ); insert into movies values 2 Raiders of the Lost Ark, 1981, Steven Spielberg'); insert into movies values (3, 'Indiana Jones and the Temple of Doom, 1984, Steven Spielberg'); insert into movies values (4, 'Machete', 2010, 'Robert Rodriguez'); -- movie_cast insert into movie_cast values (1, 1); insert into movie cast values (2,1); insert into movie_cast values (3,1); insert into movie cast values (4,1); insert into movie_cast values (3, 2); insert into movie_cast values (3,3); insert into movie_cast values (5, 4); insert into movie_cast values (6,4); insert into movie cast values (7,4); Write a SQL select statement to find, for each actor, the number of movies where (s)he appears. Your query should return a relation with the same schema as the one shown below. Records should be sorted by actors' last name. Jessica firstname lastname count -|----- Alba 1 1 John Doe 01 | Carrie Fisher 11 Harrison Ford | 31 1 Mark Hamill | 11 Michelle Rodriguez 11 | Danny Trejo | 11 Billy Dee Williams 11 Make sure that your answer adopts the same output-schema and sorting order as above. Your SQL statement must work properly on any arbitrary database instance (i.e, it should not be restricted to the database instance given here). Your SQL statement will be evaluated in PostgreSQL 9.6: make sure to test it in www.db-fiddle.com or any other similar service; answers that fail to run on PostgreSQL 9.6 will be penalized. State any assumption you make. Do not use any DDL statement. Do not create views. Do not return duplicate tuples

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions