text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
references this people table, and
we'll need another column that | 2,191.78 | 3.39 |
is a foreign key that references
the flights table, such | 2,195.17 | 2.97 |
that I can relate those
two tables together. | 2,198.14 | 2.55 |
So that table could look like this. | 2,200.69 | 2.67 |
This now is a simplified passengers
table that only has two columns. | 2,203.36 | 3.58 |
It has a person id column
and a flight id column. | 2,206.94 | 3.56 |
The idea of this table now is
it's known as an association | 2,210.5 | 3.06 |
table, or a joined table that just
associates one value from one table | 2,213.56 | 4.23 |
with another value from another table. | 2,217.79 | 2.34 |
This row here, one and one, means
the person with an id of one | 2,220.13 | 4.26 |
is on flight number one. | 2,224.39 | 2.22 |
I could look up that person
inside of the people table, | 2,226.61 | 3.06 |
look up that flight inside
of the flights table, | 2,229.67 | 2.19 |
and figure out who the person
is and what flight they're on. | 2,231.86 | 2.92 |
Down here, two and four, means
whoever the person with an ID of 2 | 2,234.78 | 3.89 |
is on whichever flight
happens to have an ID of 4. | 2,238.67 | 5.7 |
So this now has allowed
us to be able to represent | 2,244.37 | 2.82 |
the types of relationships we want. | 2,247.19 | 1.74 |
We have a table for airports and
a table for flights and any flight | 2,248.93 | 4.32 |
is going to map to two different
airports, one destination one origin. | 2,253.25 | 4.14 |
And any airport might appear
on multiple different flights. | 2,257.39 | 2.74 |
It's sort of a one to many relationship. | 2,260.13 | 2.6 |
Then over here, when
it comes to passengers, | 2,262.73 | 2.34 |
we've stored people inside
of a separate table, | 2,265.07 | 2.52 |
and then had a many to many
mapping between people and flights | 2,267.59 | 3.39 |
so that any person could be
on multiple different flights. | 2,270.98 | 2.82 |
Like here, for example, person number
two is on both flights one and four. | 2,273.8 | 4.62 |
Likewise, a flight could
have multiple people. | 2,278.42 | 2.47 |
So in this case flight number
six has passengers five and six | 2,280.89 | 3.83 |
that are on that flight as well. | 2,284.72 | 1.68 |
We've been able to represent
those relationships. | 2,286.4 | 3.34 |
Of course a byproduct of doing this is
that now our tables are a little bit | 2,289.74 | 4.01 |
messier to look at. | 2,293.75 | 1.38 |
Messy in the sense that it's
not immediately obvious to me, | 2,295.13 | 2.71 |
when I look at this table,
what data I'm looking at. | 2,297.84 | 2.48 |
I see these numbers, but I don't
know what these numbers mean. | 2,300.32 | 3.03 |
I've separated all these
tables into different places. | 2,303.35 | 2.825 |
Now it's a little harder for me to
figure out who is on which flight. | 2,306.175 | 2.875 |
I have to look at this data, look
up people in the people table, | 2,309.05 | 2.91 |
look up flights in the
flights table, and somehow | 2,311.96 | 2.22 |
associate all of that information
back together in order | 2,314.18 | 3.63 |
to draw any sort of conclusion. | 2,317.81 | 2.1 |
But luckily, SQL makes
it pretty easy for us | 2,319.91 | 2.19 |
to be able to take data across
multiple different tables | 2,322.1 | 3.33 |
and join them all back together. | 2,325.43 | 2.19 |
We can do this using a JOIN
query that takes multiple tables | 2,327.62 | 4.05 |
and joins them together. | 2,331.67 | 1.29 |
So the syntax for a JOIN query
might look something like this. | 2,332.96 | 3.58 |
And here we'll go back to
just the two-table setup where | 2,336.54 | 2.48 |
I have flights and passengers,
where every passenger is | 2,339.02 | 3.18 |
associated with one flight. | 2,342.2 | 1.41 |
But you could extend this
and join multiple tables | 2,343.61 | 2.19 |
to deal with our more
complex example as well. | 2,345.8 | 2.35 |
But here, I'd like to select
every person's first name | 2,348.15 | 3.2 |
and their origin and their destination. | 2,351.35 | 2.82 |
I'm going to select that
from the flights table, | 2,354.17 | 2.22 |
but I need to join it
with the passengers table. | 2,356.39 | 2.91 |
Then I say ON to indicate
how it is these two | 2,359.3 | 2.67 |
tables are related to one another. | 2,361.97 | 2.022 |
In this case, I'm saying
the way these two tables are | 2,363.992 | 2.208 |
related to one another is that the
flight id column of the passengers | 2,366.2 | 4.05 |
table is associated with the
id column of the flights table. | 2,370.25 | 4.08 |
The flights table has an id that
uniquely identifies every flight, | 2,374.33 | 3.63 |
and the passengers table
has a flight id column | 2,377.96 | 2.82 |
that uniquely identifies the
flight that we're referring to | 2,380.78 | 3.12 |
for this particular passenger. | 2,383.9 | 1.74 |
And so the result I might get
is a table that looks like this. | 2,385.64 | 2.7 |
That gives me everyone's
first name, but also | 2,388.34 | 2.39 |
their origin and their destination. | 2,390.73 | 1.77 |
Our origin and destination are going
to be drawn from that table of flights | 2,392.5 | 6.06 |
and the first name is going to be
drawn from the table of passengers. | 2,398.56 | 3.21 |
But by using a JOIN query,
I've been able to take data | 2,401.77 | 2.64 |
from two separate tables and
join them both back together. | 2,404.41 | 4.08 |
And there are a number of different
types of JOIN queries that I can run. | 2,408.49 | 3.36 |
What we saw here was just
the default JOIN, which | 2,411.85 | 2.31 |
is otherwise known as an INNER JOIN. | 2,414.16 | 2.79 |
Effectively, an INNER JOIN
will take the two tables, | 2,416.95 | 2.88 |
it will cross compare them based
on the condition that I specified | 2,419.83 | 2.94 |
and only return back to me the results
where there's a match on both sides. | 2,422.77 | 3.99 |
Where we match a passenger's flight
id with an id in the flights table. | 2,426.76 | 4.67 |
There are various
different kinds of outer | 2,431.43 | 1.75 |
joins if I want to be OK
with the idea that maybe | 2,433.18 | 2.61 |
something on the left
table that I'm joining | 2,435.79 | 2.01 |
doesn't match with
anything on the right, | 2,437.8 | 1.708 |
or maybe something on the right
table doesn't match with something | 2,439.508 | 2.75 |
on the left. | 2,442.258 | 0.642 |
But just know there are other types of
JOIN queries that I can run as well. | 2,442.9 | 4.74 |
Other strategies that can be
helpful when dealing with SQL tables | 2,447.64 | 3.04 |
are optimizations we can make
to make queries more efficient. | 2,450.68 | 3.44 |
One thing we can do with our
tables is to create an index | 2,454.12 | 3.57 |
on a particular table. | 2,457.69 | 1.44 |
You can think of an index as kind of
like the index in the back of a book, | 2,459.13 | 3.7 |
for example, where if you wanted to be
able to search for a topic in a text | 2,462.83 | 3.43 |
book, you could open the textbook and
just page by page look for every topic | 2,466.26 | 3.795 |
and just try and find the
topic you're looking for. | 2,470.055 | 2.125 |
But often what you'll be
able to do if the table has | 2,472.18 | 2.49 |
an index is go to the index of the
book, find the topic you're looking for, | 2,474.67 | 4.78 |
and that will quickly give
you a reference for how | 2,479.45 | 2.15 |
to get to the right page in question. | 2,481.6 | 2.13 |
An index on a table operates
in much the same way. | 2,483.73 | 2.64 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.