Data can make more sense and may reveal patterns more readily when arranged in order rather than jumbled
randomly.
Right.
So in SQL, we order the results of a query using the mouse containing the keywords order by followed
by the name of the column or columns to sort.
So applying this class doesn't change the original table, only the result of the query here.
So now we firstly select the lastname firstname.
And after that we also had the hire date and salary.
Right?
And we selected this from teachers and after teachers before the semicolon here.
Or we can also delete semicolon, we can add later, later we will use order by and let's delete salary
because we will use that salary here, order by salary and the scanning, the scanning here.
So by default order by source value in a scanning order.
But here I sort in the scanning order by adding the desk keyword and the optional keyword specifies
sorting and scanning order.
So now by ordering the salary column from highest to lowest, I can determine which teachers earn the
most.
And as you can see here, Reynolds earns the most.
So if we if you are if you want also to print the values here, you see the values.
You can also add the salary to select after hire date.
Right?
Actually, instead of adding.
After the air date.
We can also add before the last name.
So with these numbers, we can identify the top earners of our.
Teachers, right?
So we will add the salary before the last name.
And here that's it.
The first earner, 65,000, 43,000 543,000 538,000 536,000
230,200.
And we can also do, for example, instead of salary, we can also do remember we have had an ID here,
so ID and here, let's actually see the ID here or after.
Let's add ID after the date.
And.
Here.
If you are using keyword as a name, which is it's not a best practice, you will use double quotes
outside the ID here.
So in this case, if I write ID just regularly, it will the SQL will identify it as a query and that's
why we will use the ID with it in the double quotes.
And here and as you can see here, six, five, four, three, two, one.
So that's because we use that in a scanning order of the ID So firstly six, five, four, three, two,
one.
So if we delete that.
And as you can see here.
One, two, three, four, five, six.
Okay, so the order by clause also accepts numbers instead of columns names.
So with the number identifying the SORT columns according to its position in the select number.
So this for example, order by three and desc here.
So you will see something different here.
And instead of this delete ID, we don't need that because here, as you can see here with this candid
here.
So the ability to sort in our queries gives us a great flexibility in how we retrieve and present the
data.
For example, we are not limited to sorting.
Just one column.
So we can also do this, for example.
Order by order by.
Ordered by school standing.
And higher date the scanning.
That's it.
And last name.
First name.
Let's actually instead of using salary now.
Uh, we don't.
We just want to print the.
Uh, I want the last name and hire date.
Yeah, that's it.
So now, as you can see here, we have last name and hire date here.
So that's it.
As kdb rzj c p.
So this is the ending.
And here, as you can see, here, we have this.
So in this case, we are retrieving the last names of teachers, their school and the date they were
hired.
So here we also need to school.
Here.
That's it.
So.
And the and here.
By sorting this school column in ascending order and hiredate.
In descending order, we create a listing of teacher grouped by a school with the most recently hired
teachers listed first.
So this shows us who the new teachers are at each school.
Right.
As you can see here.
Myers Middle School 2005 eight one.
This is the oldest teacher in Myers Middle School.
2011.
This is the newest teacher on the Myers Middle School.
2011 1013.
And here Reynolds has a Reynolds is the let's actually print the names right first.
So we will meet them.
Right.
I'm sorry.
Okay.
First name.
And that's it.
Yes.
So here Lee Reynolds is the oldest teacher of the Roosevelt School.
High school.
And Jane Smith is the newest teacher of Roosevelt High School.
So you can use order by on more than two columns, but you will soon reach a point of diminishing returns
where the effect will be hardly noticed.
Imagine if you added columns about teachers, how big diggers attained and the grade leveled out, and
the birth date to order by class.
It will be difficult to understand the various sort of directions.
Output once, much less communicate to that of others.
Digesting the data happens more easily when not output.
Answering specific question.
Therefore, a better strategy is to limit the number of columns in your query to only the most important
and run several queries to each question.
So.
And that's it with our lecture.
In the next lecture, we will also learn about the distinct.
My name is Stefan and I'm waiting.
Next lecture of Oxley Academy.