Learn to utilize the incredibly powerful and useful database tool, Microsoft's SQL Server as well as SQL Server Management Studio through this series of tutorials! This episode covers a detailed understanding of the ORDER BY and TOP operations for sorting data in specific methods and grabbing data at extremeties!
Learn the basics of a standard Select, From, Where statement query in just minutes! SQL is an incredible tool for any employee to be familiar with for their job or as an independent software developer!
If you want to see anything specific on the channel or have technical questions about anything leave a comment on our of our videos and we will get back as soon as we can!
Thanks for watching and supporting the channel!
hello and welcome welcome back to the
channel today's video is going to be a
continuation in the introduction to sql
series that we've begun and so far we've
taken a look at creating a statement
using select from and where to filter
data data data out of a table and today
what we're going to be looking at is
using the order by function as well as
top um so
so uh
let's just dive right into it here
essentially what i've done is i've used
the
query that we've already discussed
select all data from employee table no
filtering conditions anything else like
that
and we get
jim pam all the way through creed
we get the entire
uh
employee table so what we want to do
today we previously have already looked
at filtering out by saying where
any of these conditions like position is
sales and this will give us just the
sales people we've already looked at
stuff like that but what we're going to
look at today is a new piece
called order by and this is essentially
telling you how the data is going to be
viewed
so let's say we want to view the entire
employee database based on who makes the
most money versus the least money you go
order by salary and all of a sudden
the default way we were seeing it where
jim was at the top and then it just went
in the order of the data coming in
that's no longer true it starts from the
lowest salary
and goes up to the highest salary
and so order buy is super useful when
you have data that might be stored
alphabetically or it might not have come
in exactly when it happened and you need
to order something numerically you can
order by any column and if it's a string
value so if we did order by
position then what's kind of nice is
we're going to see it groups people from
the same division right next to each
other and string columns when you go
order by are going to be
by default alphabetical so now you're
saying well when we did order by salary
we had the
smallest values at the top
which is the default is it's going to go
in what it calls ascending order sql
considers starting at the lowest number
and going downward to higher numbers
ascending order
so if you want to invert that you use
the d e s c and that stands for
descending and you basically tell the
sql query hey
now i want to see the highest number at
the top and the lowest number at the
bottom
and so there's the purpose of using
order by descending technically you
could say order by ascending and that's
going to give you what we saw but if you
know that this is the default you don't
have to worry about it too much
if you
didn't know that that was the default
then
it's nice to know that ascending does
exist so the inverse of this is true
um and then for string variables
if you filter a string column descending
you're just going to get reverse
alphabetical order so you see we go down
to creed at the top and and pam at the
beginning when we filter by string value
so something that i really like to
combine this with let's go back to
salary and we'll just leave it as the
default
i like to talk about the select
top function as well
so top is saying you're only going to
get a certain number of them so
like let's say we want the top three
uh
salaries in the office
you would build this query and again to
get start at the highest number and go
down you need to do descending but now
what we'll see we only get the three
highest earners so if what you're really
concerned with in your data set is
outliers you could select the top of
whatever based on your column conditions
and the way you do that is you use top
and then the number
and then you still put in your column
conditions like before so if you didn't
want
to select everything you could say
select top three
just the employee name
so essentially you need to keep in mind
that you still have to have your column
conditions but using this top function
and then so right here
we get the top three salaries if we were
to change that back to default we'd get
the last three salaries you can use this
with any kind of data like dates
anything and
you use it as a really powerful tool to
filter out all the bottom data you can
also
we'll talk about this in in future
videos but you can also use kind of
advanced math queries like averages
rounding summation
and those kinds of things so
essentially the order by tool is a great
way for organizing your data based on a
specific column you want to see and then
selecting top whatever is a great way to
filter out
data if you're only interested in the
real extremities so
that's this video it's nice short video
punch out two really important concepts
in sql
hopefully you found it useful if you
have any questions on this or anything
else you've seen on the channel feel
free to let me know about in the
comments i'll try to get back to you as
soon as i can and if you found this or
anything else on the channel useful then
i really appreciate a liking to
subscribe it helps the channel out a lot
and as always good luck with your
projects and thanks for watching thanks
bye