visit
1. SELECT all items
If our table has millions of rows, a query that returns all items may take a lot of time to load. You can always use LIMIT 50 or something like that so that your table doesn't take forever to load. In our case, SQL ZOO already only returns the 50 first rows of our query, so we don't have to worry about it. Let's see what that looks like.Alright, id and stop are the keys we used to connect the two tables, that's why they're always the same in every line. We got the name of the stop, the bus number, and the bus company. We learned from an earlier problem that we would usually refer to a bus by their num and company like '124 SMT'. So, by looking at our table we could assume that at the stop in Aberlady we can get the 124 SMT bus.
So far so good, but it took me a lot more time to understand what the pos was. At the beginning of the page, there's a link that takes us to a place where they tell us what every column in the table means. They say pos is the order of the stops, but I still couldn't visualize what it meant just by looking at the table. Now, the good news for us here is that we're not going to need to use pos to solve our problem, but I'll still tell you how you can understand it in the next step.
2. Use ORDER BY to understand what is going on
If we order by the num then by position, we'll see that the '1 LRT' bus, for example, leaves from Leith then goes on a round trip until it gets back to Leith after stopping at 9 other stops. If we go down the table, we see that not all buses go on a round trip as they won't always end up at the same stop where they started.
As I said, we won't be needing to use pos for this exercise but it could be useful if we were trying to find, for example, not only the buses available but also the shortest possible routes.
Ok, now that we got a clearer understanding of what is going on, let's see what the problem wants us to do.3. Never forget that a self JOIN creates a new table
When you do a self JOIN, you might be tempted to try to visualize it as a table somehow joined to itself. But the way to think about it is as two separate tables that look exactly the same. You join the new one to the side of the first one. Because they are exactly the same, any column can become a key. In other words, you can pick what your primary keys are going to be. That's where the fun starts!Right now, there's only one stop per row, but for the next step, we need another one, our transfer stop. So, here's what we can do: we can connect a new route table to the table we already have ON num AND company, like this:
You see, our r2 table got appended to our r1 table adding up all of their columns. Both the r1 and r2 always have the same num and company (in the same lines) because that's where we joined them.
We already saw that the '1 LRT' line runs through 11 stops. That's why we can see the stop name repeat for eleven rows at each new stop. In other words, from Leith, you can get to any of those stops listed on the far-right column. Let's reduce the number of columns we're showing so we can get a better view of our data.4. With Self JOINs, think horizontally
It's easy to get a little lost before you're about to filter your data. It helps if you visualize your tables as a horizontal map. On your left, you have the place from where you're leaving: 'Craiglockhart'. To your right, you'll have—I'll show it very soon—your transfer stop, then your destination.So, let's filter the column name by 'Craiglockhart'.
So, what we're looking at now, if we take into consideration the stop column, is a list of all the stops you can get to leaving from Craiglockhart.
Now, imagine that we can do the same for Lochend, but instead of having it as the departure, we have it as the arrival. Let's see what that would look like.Now, the stop column is showing all the stops from where we can get on a bus and get to Lochend.
In our query before this one, we saw all the stops we could get to leaving from Craiglockhart. In this one, we have all the stops we can leave from to get to Lochend. In other words, all we have to do is join these two tables together. Again, have that in mind: JOINs join tables horizontally—or to the side. There's something called UNION that stacks tables vertically—on top of each other, but JOINs glue them horizontally. Is that bueno?
So, how do we connect these two queries that we just saw? I will show you the long way first and then a shorter syntax.We now have four route tables and one stops table—which is showing us the transfer stop. What we have to do now is to filter the stop on r1 by 'Craiglockhart' and the stop in r4 by 'Lochend' and we'll get to our answer. To do that, we could JOIN another stops table at the beginning of our table and append another stops to the end of our table. But, instead, we can use what we learned with subqueries, like so.