Next up we're talking about limit.
Limit allows us to control the number of results we get back.
Now, a lot of the time, this may not be that useful unless we're also sorting, but I'll show both
of them together.
On its own limit, followed by a number, will limit the number of results we get back.
So as I said, I mean, we could just select the first five, let's do author or book ID, I think is
what we have and title and released year from books and we get what is it, 20 rows.
But I could limit that by just saying limit and then a number like five and that will give me the first
five.
But what does that really mean?
The first five?
It's just the first five based off of this order, which is the order that are stored in the table in
maybe not that useful, but what if I wanted to get the first five books that were released, the earliest
five released books?
Well, I could sort or order by released here first, and that will give me all of the books sorted
by release year.
Although we have that darn null in there that I really should get rid of.
And then after that I can limit it to five.
Now we get the first five.
So Cannery Row is 1945, up to where I'm calling from, 1989.
Not a lot of super early literature in this table.
Okay, so that's the basic usage of limits.
You put some number in there, we can limit it to ten, and that will give me the first ten.
Of course, if I change the order, if I order by release tier descending now, I'm going to get the
ten most recent books 2017 down to 2003.
So most of the time I use limit alongside order by.
But maybe if I'm just actually honestly one of the times I use limit the most is when I'm teaching and
I have a table with a lot of results, a lot of values in it.
And I just want to show students an example.
I might limit it to the first five, but in the real world, when you're not teaching, I usually use
limit with order by.
But we don't have to only get the first five.
We can do fancy things like this right here where I actually have two numbers.
Now this is actually equivalent to this one right here.
So we are getting the first five going from 0 to 5.
Let me just show that.
Let me do this on different lines so that it's easier to look at.
So order by released.
Yea.
All right.
And then after that I'll say limit the first five.
Just like that.
But instead of saying five, I'm going to do zero comma five.
And what you'll see is when I do that, what happens is we get the exact same results.
But I could instead say from 1 to 5.
And the way that it works is that I've said, start at one and go for five.
So I still want five total, but I want to begin with that first row, which it means not the zero with
row.
So the zero with row would have been this one right here.
It's a little confusing.
Let's try one more example.
So once again, I'm just going to limit it to ten this time so you can see what it looks like.
But instead of ten, I'm going to say, let's go from row three and let's only go for two rows.
And this is what happens.
We started at row three in this result.
So zero one, two, three is white noise.
And then we carried on for two rows.
So white noise and where I'm calling from, that's what we got.
So this does allow us to select a subset in the middle of a grouping, not just the first three rows
or the first five rows, but I could do from row one to row ten or from row 25 to 27 if I needed to
do that.
For some reason I will say usually I'm just working with the first rows and I don't use two numbers
for limit.
But you can just remember this first number is the starting row and the second number is the count how
many you want to retrieve.
So this one would start at row five and it would include seven results.
Let's try that one here.
So from five comma seven, we started it.
When I say row five, it Let me just show this here.
If I did ten.
Row five would be zero one, two, three, four, five.
And that is indeed where we started Interpreter of Maladies.
And then we included seven results one, two, three, four, five.
And it keeps going past what I have in this verse ten.
Another thing we can do if I do a select, I don't have a just title from books and I'll just well,
we'll do it alphabetically.
Sure.
Order by title.
We have 20 rows, but let's say I don't know that and I use a number that's out of bounds that doesn't
exist in this table.
Like, I don't know, 40.
What happens if I do a limit?
40?
It's just going to give me as many as it can.
I don't get an error.
Same thing if I said start at row 18 and go for this many rows.
I don't get an error.
I just get the last two because there's 20 rows.
We start at 18 and we just go to the end.
So that is one option.
I guess if you want to get rows from the end and that's it for limits.
So often I use it with a short first order, buy something and then limit our results.
You can use one number which is probably more common.
Just limit the first five results and that's all you get back.
Or you can say, I want a certain number of results starting at some point.