Next up, we're going to learn how to sort our results.
Right now, if I just do a simple select, how about just book ID, title and author L name from books?
Our data is technically in an order.
It is sorted based off of the order that our data is in the database.
Right.
If I insert something next, if I do an insert into books, I'm going to delete this row in just a moment.
So I'm not going to put all the values in, but let's say I'm going to put title and author L Neiman.
Author l name values and I'll just call the title My Life and it's written by me.
Last name Steel.
It doesn't matter.
But my point that I want to make is that that data is inserted, it's in there.
And when I select back out, I select all my my rows without any sort of sorting.
That newest result is going to be at the end, by the way.
You might be wondering why did it jump to 22?
I recorded this video once before.
I wasn't happy with it, so I deleted some rows 20 and 21.
And so those ideas were basically taken up and the auto increment just keeps moving on.
So it's a relic from a bad recording I made that I just didn't really I didn't like how the video unfolded,
so I redid it anyway.
You'll see that it's at the end here.
So there is an order.
It's based off of that insertion order, but that's probably not what we want.
So there is an order here, but often we want to sort in some other way, sort by the titles alphabetically
or by the last name of authors or by the number of the release here or the stock that we have.
The quantity of each book in the way that we do that is by using order by and it's order space by two
different words, and it comes after our select.
So we have whatever we're trying to select.
And then at the end we can say, by the way, here's how I want you to order it by order by and then
some column name.
So author L name.
Let's see if it works.
So here's what I had before.
Book ID, title and author, last name.
Why don't we actually just get rid of the title just to make this a little easier to look at and instead
do author f name too.
So book ID authors, first name, author's last name.
And I'm going to do an order by.
Author ll name just as my first example.
And we see the order completely changes and we get these alphabetized last names.
So Carver is our first last name and then all the way down to Steinbeck.
If I instead said author first name.
We now see.
Oh, there's no.
I've got a null in there because I didn't put their first name in when I created that book.
So Noelle comes first before any of my other alphabetical values.
And then we have Dan Dave all the way down to Raymond.
So that's our first example of using order by.
Just remember, it comes at the end.
I've seen people make that mistake and try and do something like select book ID, author, F name,
author, L name, and then put order by here from books and it just doesn't work.
It's invalid SQL syntax.
Now there's a bit more to know about order by.
The first thing is that it ascends or it sorts in ascending order by default.
So alphabetically that means going from A to Z.
But we can change that by saying we want descending order DSC, which somewhat confusingly we also use
DSC to mean describe right when we describe a table, it's short for describe books.
But when we use it after order by, let's do the same query again, Order by author first name, right.
We're going from A to Z.
Of course we don't have an A, so it's D down to R.
But if I then say DSC and that has to go here right after the order by, put my semicolon in.
Now we go from R at the top down to Dan and then null, which is considered the lowest value alphabetically.
So you can change that disc.
I think you can even say a SC if you wanted to.
For ascending, which is again the default.
So we're going from NULL then dan A down to Z for Raymond.
Ah, we don't have a Z and we can also do numbers.
Of course, this is probably more common than alphabetizing things.
Let's see, we've got let's look at our books again.
We've got how about release gear or stock quantity or pages.
Let's sort by pages to start.
So let's select title and pages from books.
Okay.
And I want to sort by pages.
So I'll do an order by pages.
And then do I want the default order of ascending or do I want descending?
Or if I do ascending the default, we start with null because of that one bad book I inserted, which
is fine.
I didn't specify Page's value.
And then we have 176 pages up to 634 pages.
And then of course we could change that to be ascending instead.
Additionally, we could sort by release here.
What about if I tried this released year?
What happens here if I'm not even selecting released year?
It's there.
I'm pretty sure it's called released here.
We could always verify.
Yeah, released.
Underscore year it's numeric, but I'm not selecting it.
I'm only selecting title and pages.
What happens?
Well, it actually still works If you take a look here, we have Cannery Row all the way up to Lincoln
in the Bardo.
Now, if I put release year in here so that we can actually see it.
You'll see the order doesn't change.
We go from Cannery Row again.
I'm ignoring the null Cannery Row up to 2017 for Lincoln in the Bardo.
So that's probably not that common to sort by or order by some column that you're not even selecting
because you won't be able to see the values that you're sorting by, but you can do it.
So we have a bit more to talk about with Order by, but I'm going to split it into two videos to try
and, I don't know, preserve your sanity.
I guess I'll see you in the next one.