Skip to content

Commit d56faed

Browse files
committed
Merge branch 'checkpoint-join-statements'
2 parents 89e30a1 + bc7d628 commit d56faed

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
31,Transfiguration
2+
32,Defence Against the Dark Arts
3+
33,Flying
4+
34,Study of Ancient Runes
5+
35,Care of Magical Creatures
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1,Albus Dumbledore,31
2+
2,Severus Snape,32
3+
3,Dolores Umbridge,32
4+
4,Bathsheda Babbling,34
5+
5,Rubeus Hagrid,35
6+
6,Wilhelmina Grubbly-Plank,35
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
1. How do you find related data that is held in two separate data tables?
2+
3+
ANSWER HERE
4+
By using JOIN statements, of course!
5+
6+
2. Explain, in your own words, the difference between a CROSS JOIN, INNER JOIN,
7+
LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.
8+
Give a real world example for each.
9+
10+
ANSWER HERE
11+
CROSS JOIN
12+
- Returns every combination of every column from every table.
13+
example: In a soccer league database containing a team table and player table,
14+
I want to see every team and player combination.
15+
16+
INNER JOIN
17+
- Joins table data based on shared (primary to foreign) keys.
18+
example: Would show me only soccer players on teams (and only teams that have players).
19+
20+
LEFT OUTER JOIN
21+
- Returns everything from left table and matching keys from the right table.
22+
example: if LEFT is teams, would show me all teams and what players (if any) are on that team.
23+
24+
RIGHT OUTER JOIN
25+
- Opposite of LEFT OUTER JOIN. Returns everything from the right table and
26+
only matching keys from the left table.
27+
example: if LEFT is teams, would show me all players and if they are on a team or not.
28+
29+
FULL OUTER JOIN
30+
- Performs an INNER JOIN as well as RIGHT and LEFT OUTER JOINs.
31+
example: I want to see teams and their players and also players that have
32+
no teams and teams that have no players.
33+
34+
35+
3. Define primary key and foreign key. Give a real world example for each.
36+
37+
ANSWER HERE
38+
- Primary key relates directly to the table, whereas a foreign key is a value shared
39+
by another table's primary key.
40+
example: Soccer team has an id (primary key), Soccer player has a team_id (foreign key).
41+
42+
43+
4. Define aliasing.
44+
45+
ANSWER HERE
46+
Aliasing is when the table names are abbreviated in a SELECT statement.
47+
48+
5. Change this query so that you are using aliasing:
49+
SELECT professor.name, compensation.salary, compensation.vacation_days
50+
FROM professor JOIN compensation ON
51+
professor.id = compensation.professor_id;
52+
53+
ANSWER HERE
54+
SELECT p.name, c.salary, c.vacation_days
55+
FROM professor AS p
56+
JOIN compensation AS c
57+
ON p.id = c.professor_id;
58+
59+
6. Why would you use a NATURAL JOIN? Give a real world example.
60+
61+
ANSWER HERE
62+
Let's say the State of Texas has a database with hundreds of park tables and
63+
each park table had dozens of columns that are identically named (such as playground
64+
boolean, bike_path boolean, etc).
65+
We could use a NATURAL JOIN and get a returned table without having to list every
66+
shared park table columns.
67+
68+
7. Using this Employee schema and data, write queries to find the following information:
69+
All employees with their shifts if they have any. Also include any unscheduled shifts.
70+
71+
ANSWER HERE
72+
SELECT *
73+
FROM shifts
74+
LEFT OUTER JOIN scheduled_shifts ON scheduled_shifts.shift_id = shifts.id
75+
ORDER BY scheduled_shifts.employee_id;
76+
77+
8. Using this Adoption schema and data, please write queries to retrieve the following
78+
information and include the results:
79+
All volunteers. If the volunteer is fostering a dog, include each dog as well.
80+
81+
ANSWER HERE
82+
SELECT *
83+
FROM volunteers
84+
LEFT OUTER JOIN dogs ON dogs.id = volunteers.foster_dog_id
85+
ORDER BY volunteers.id;
86+
87+
The cat's name, adopter's name, and adopted date for each cat adopted within the
88+
past month to be displayed as part of the "Happy Tail" social media promotion which
89+
posts recent successful adoptions.
90+
91+
ANSWER HERE
92+
SELECT adopters.first_name, adopters.last_name, cat_adoptions.date, cats.name
93+
FROM cat_adoptions
94+
JOIN cats ON cats.id = cat_adoptions.cat_id
95+
JOIN adopters ON adopters.id = cat_adoptions.adopter_id
96+
WHERE cat_adoptions.date > CURRENT_DATE - INTERVAL '30 DAYS';
97+
98+
Adopters who have not yet chosen a dog to adopt and generate all possible combinations
99+
of adopters and available dogs.
100+
101+
ANSWER HERE
102+
SELECT adopters.id, adopters.first_name, adopters.last_name, dogs.id, dogs.name
103+
FROM adopters
104+
FULL OUTER JOIN dog_adoptions ON dog_adoptions.adopter_id = adopters.id
105+
CROSS JOIN dogs
106+
WHERE dog_adoptions.adopter_id IS NULL AND dogs.id NOT IN (SELECT dog_adoptions.dog_id FROM dog_adoptions)
107+
ORDER BY adopters.id;
108+
109+
Lists of all cats and all dogs who have not been adopted.
110+
111+
ANSWER HERE
112+
SELECT cats.id, cats.name, cat_adoptions.adopter_id
113+
FROM cats
114+
LEFT OUTER JOIN cat_adoptions ON cats.id = cat_adoptions.cat_id
115+
WHERE cat_adoptions.adopter_id IS NULL;
116+
SELECT dogs.id, dogs.name, dog_adoptions.adopter_id
117+
FROM dogs
118+
LEFT OUTER JOIN dog_adoptions ON dogs.id = dog_adoptions.dog_id
119+
WHERE dog_adoptions.adopter_id IS NULL;
120+
121+
Volunteers who are available to foster. If they currently are fostering a dog, include
122+
the dog. Also include all dogs who are not currently in foster homes.
123+
124+
ANSWER HERE
125+
SELECT v.id, v.first_name, v.last_name, v.available_to_foster, v.foster_dog_id, d.name
126+
FROM volunteers AS v
127+
LEFT OUTER JOIN dogs AS d ON d.id = v.foster_dog_id
128+
WHERE v.available_to_foster IS TRUE;
129+
SELECT dogs.id, volunteers.foster_dog_id
130+
FROM dogs
131+
LEFT OUTER JOIN volunteers ON dogs.id = volunteers.foster_dog_id
132+
WHERE volunteers.foster_dog_id IS NULL;
133+
134+
The name of the person who adopted Rosco.
135+
136+
ANSWER HERE
137+
SELECT dog_adoptions.adopter_id, adopters.first_name, adopters.last_name, dogs.id, dogs.name
138+
FROM dog_adoptions
139+
JOIN adopters ON adopters.id = dog_adoptions.adopter_id
140+
JOIN dogs ON dogs.id = dog_adoptions.dog_id
141+
WHERE dogs.name = 'Rosco';
142+
143+
9. Using this Library schema and data, write queries applying the following scenarios:
144+
To determine if the library should buy more copies of a given book, please provide
145+
the names and position, in order, of all of the patrons with a hold (request for
146+
a book with all copies checked out) on "Advanced Potion-Making".
147+
148+
ANSWER HERE
149+
SELECT patrons.name, holds.rank, books.title
150+
FROM holds
151+
LEFT OUTER JOIN patrons ON patrons.id = holds.patron_id
152+
LEFT OUTER JOIN books ON books.isbn = holds.isbn
153+
WHERE books.title = 'Advanced Potion-Making'
154+
ORDER BY holds.rank;
155+
156+
Make a list of all book titles and denote whether or not a copy of that book is checked out.
157+
158+
ANSWER HERE
159+
SELECT transactions.id, books.title, transactions.checked_out_date
160+
FROM books
161+
LEFT OUTER JOIN transactions ON transactions.isbn = books.isbn;
162+
163+
In an effort to learn which books take longer to read, the librarians would like
164+
you to create a list of total checked out time by book name in the past month.
165+
166+
ANSWER HERE
167+
SELECT books.title, SUM(transactions.checked_in_date - transactions.checked_out_date)
168+
FROM books
169+
LEFT OUTER JOIN transactions ON transactions.isbn = books.isbn
170+
WHERE transactions.checked_out_date > CURRENT_DATE - INTERVAL '30 DAYS'
171+
GROUP BY books.title;
172+
173+
In order to learn which items should be retired, make a list of all books that have
174+
not been checked out in the past 5 years.
175+
176+
ANSWER HERE
177+
SELECT books.title
178+
FROM books
179+
LEFT OUTER JOIN transactions ON transactions.isbn = books.isbn
180+
GROUP BY books.title, books.isbn, transactions.checked_out_date
181+
HAVING transactions.checked_out_date < CURRENT_DATE - INTERVAL '5 YEARS'
182+
AND books.isbn NOT IN (SELECT transactions.isbn FROM transactions WHERE transactions.checked_out_date > CURRENT_DATE - INTERVAL '5 YEARS');
183+
184+
List all of the library patrons. If they have one or more books checked out, correspond
185+
the books to the patrons.
186+
187+
ANSWER HERE
188+
SELECT p.id, p.name, t.isbn, b.title, t.checked_out_date, t.checked_in_date
189+
FROM patrons AS p
190+
RIGHT OUTER JOIN transactions AS t ON p.id = t.patron_id
191+
RIGHT OUTER JOIN books AS b ON b.isbn = t.isbn
192+
ORDER BY p.id;
193+
194+
end

0 commit comments

Comments
 (0)