-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseinfeld-queries.sql
More file actions
69 lines (64 loc) · 2.54 KB
/
Copy pathseinfeld-queries.sql
File metadata and controls
69 lines (64 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*****************************************************************************
For each of the main four characters, return all episodes without them.
*****************************************************************************/
SELECT first_name, last_name, season_no, episode_no, episode_name
FROM `Character`, Episode E JOIN Season USING(season_id)
WHERE
character_id IN (1, 2, 3, 4)
AND character_id NOT IN (
SELECT character_id
FROM Portrays
WHERE episode_id = E.episode_id
);
/*****************************************************************************
Find all episodes with Newman.
*****************************************************************************/
SELECT season_no, episode_no, episode_name
FROM Portrays
JOIN `Character` USING(character_id)
JOIN Episode USING(episode_id)
JOIN Season USING(season_id)
WHERE referred_to_as = 'Newman';
/*****************************************************************************
List all actors which appeared in episode 1, season 1.
*****************************************************************************/
SELECT first_name, last_name
FROM Portrays
JOIN Actor USING(actor_id)
JOIN Person USING(person_id)
JOIN Episode USING(episode_id)
JOIN Season USING(season_id)
WHERE episode_no = 1 AND season_no = 1;
/*****************************************************************************
Find the first epiode in which Uncel Leo appears.
*****************************************************************************/
SELECT season_no, episode_no, episode_name
FROM Portrays
JOIN `Character` USING(character_id)
JOIN Episode USING(episode_id)
JOIN Season USING(season_id)
WHERE referred_to_as = 'Uncle Leo'
ORDER BY aired_on LIMIT 1;
WITH PCES AS (
SELECT *
FROM Portrays
JOIN `Character` USING(character_id)
JOIN Episode USING(episode_id)
JOIN Season USING(season_id)
)
SELECT season_no, episode_no, episode_name
FROM PCES
WHERE referred_to_as = 'Uncle Leo'
AND aired_on = (
SELECT MIN(aired_on) FROM PCES WHERE referred_to_as = 'Uncle Leo'
);
/*****************************************************************************
Create a table storing the number of episodes in each season.
*****************************************************************************/
SELECT season_no, COUNT(*) as episode_count
-- FROM Season, Episode WHERE Season.season_id = Episode.season_id
-- FROM Season JOIN Episode ON Season.season_id = Episode.season_id
-- GROUP BY Season.season_id;
-- FROM Season JOIN Episode USING(season_id)
FROM Season NATURAL JOIN Episode
GROUP BY season_id;