Hey guys welcome back to this SQL tutorial series. In this session we'll be
working with the order by and the top clause. We'll start with order by clause
so order by is used to solve the data in ascending or descending order and by
default order by sorts the records in ascending order. So let's have a look at
the syntax. We will start with the SELECT keyword and then give in the column list
then we'll use the from keyword and give the table name. Following which we will
type the keywords order by and give the columns with respect to which you want to solve
the data and finally we'll state whether the ordering should be in ascending or
descending and I've already told you guys that by default the sorting is done
in ascending order and if you want to sorting to be done in descending order
we will use the DESC keyword. So I want to sort the table with respect to
the salary column in ascending order so this would be the command for that
select start from the name of the table is employee after which I will use the
keywords order by and then give in the name of the column which would be salary
and since I want it with respect to ascending order so I don't need to add
anything else so let me hit execute right so we have successfully sorted
this table in ascending order right so julia has the lowest salary and Matt
over here has the highest salary. So this is an ascending order 73000, 80000
95,000 and goes on till one hundred and fifty nine thousand
let's say I want to sort this table in descending order of the salary so for
that all I need to do is add in the keyword DESC, let me hit execute right
so this time the table is sorted in descending order right so over here at
the top we have the maximum salary which is hundred and fifty nine thousand and
at the bottom we have the lowest salary which is seventy three thousand now
let's look at the top clause. the top clause is
used to fetch the top n records
from a table. So top clause is useful on large tables with thousands of records
and this is the syntax. They've got the Select keyword first then we type in top
and then there are number of records we want to see in our records followed by the
column list and then we use the from keyword and give in the name of the table
so I want to see only the top three records from the entire employee table
so this would be the command for that select and then I will use the top
keyword and since I want three records the number would be three and since I
want to see all of the columns I will put in star over here then I want all of
these records from the employee table so this is the entire command select top
three star from employee I'll hit execute it so I've got the top
three records from the table. Now I will use the top clause and the order by
clause and fetch the three oldest employees of this company. So let me
write the command for that select top the start so I've used the top
class to fetch the top three records. Now this would be from the employee table
and then I will use the order by clause so I want to order it with respect to
the age of the employee and I want it in descending order that is the employee who
has the maximum age should be at the top now let me hit execute so I have got
three records over here and the records are arranged in descending order of
their age. So Sam has the highest age 45 and Julia over here has the lowest age
30 right. So Sam, Matt and Julia are the three senior most employees of this
company and this is how we can work with order by Clause in the top clause. In the
next session we will work with a group by statement. So thanks for attending the
session. Let's meet in the next class.