- I have this database for a CS/database-theory homework question for a hypothetical movie store company:
- For those who might be unfamiliar with the concept, a movie store is a retail location where patrons can film productions on VHS tape, or this newfangled format called "DVD".
- Who knows, maybe some time in the distant, far-off, future we might be able to view movies directly over the Internet?
- The DDL and sample data is below.
- For those who might be unfamiliar with the concept, a movie store is a retail location where patrons can film productions on VHS tape, or this newfangled format called "DVD".
- I need to write a query that will show all movies that are available in all three Chicago stores: (
WI01
,WI02
, andWI03
).- By looking at the raw data below ourselves, we can see that only these 3
movieId
values (D00001
,D00006
, andD00007
) havemovie_store
rows for everystore
located in Chicago.
- By looking at the raw data below ourselves, we can see that only these 3
CREATE TABLE movie (
movieId varchar(6) NOT NULL PRIMARY KEY,
title nvarchar(50) NOT NULL
);
CREATE TABLE store (
storeId varchar(4) NOT NULL PRIMARY KEY,
city nvarchar(20) NOT NULL
);
CREATE TABLE movie_store (
movieid varchar(6) FOREIGN KEY REFERENCES movie ( movieId ),
storeid varchar(4) FOREIGN KEY REFERENCES store ( storeId ),
PRIMARY KEY ( movieId, storeId )
);
GO
INSERT INTO movie ( movieId, title )
VALUES
('D00001', N'True Lies'),
('D00002', N'Predator'),
('D00003', N'Last Action Hero'),
('D00004', N'Red Heat'),
('D00005', N'Conan 1'),
('D00006', N'Conan 2'),
('D00007', N'Red Sonja');
INSERT INTO store ( storeId, city ) VALUES
('WI01', N'Chicago'),
('WI02', N'Chicago'),
('WI03', N'Chicago'),
('IL01', N'Atlanta'),
('IL02', N'Nashville');
INSERT INTO movie_store ( movieId, storeId ) VALUES
-- True Lies:
('D00001', 'WI01'),
('D00001', 'WI02'),
('D00001', 'WI03'),
-- 'Predator:
('D00002', 'IL01'),
('D00002', 'IL02'),
-- Last Action Hero:
('D00003', 'WI01'),
-- Red Heat:
('D00004', 'WI01'),
('D00004', 'WI02'),
('D00004', 'IL02'),
-- Conan 1:
('D00005', 'WI01'),
('D00005', 'WI02'),
-- Conan 2:
('D00006', 'WI01'),
('D00006', 'WI02'),
('D00006', 'WI03'),
-- Red Sonja:
('D00007', 'WI01'),
('D00007', 'WI02'),
('D00007', 'WI03');
- During my problem-solving research I found a site explaining the
ALL
operator.- My query is getting unique
storeIds
for Chicago. - It is then trying to get the movie
title
with astoreId
record for each one of the Chicago locations.
- My query is getting unique
WITH chicagoStores AS (
SELECT DISTINCT
storeId
FROM
store
WHERE
city = 'Chicago'
)
SELECT DISTINCT
m.title
FROM
movie AS m
INNER JOIN movie_store AS y ON m.movieid = y.movieid
INNER JOIN store AS s ON y.storeid = s.storeid
WHERE
s.storeId = ALL( SELECT storeId FROM chicagoStores )
But my query returns zero rows (and no errors), am I misunderstanding the ALL
operator?