How to get Top / Bottom N Records from SQL Server Table in SQL Server - TSQL Tutorial Part 113

Sharing buttons:

welcome to Tech presence Adama in this

video we are going to learn how to get

top or bottom and rows or records from

sequence of a table or view in sequel

server first of all open your SSMS as

the equal server management studio and

connector with sequel server I'm going

to use the database called tech brothers

IT and I'm creating a new table called

dbo customer so it has ID name and

address for our demo let's create this

table and insert few records so you see

that I'm inserting ID 1 then ID 4 5 & 2

& 3 & name and address is a so let's

insert the data run the selector

statement for the blue customer to see

the data here we have ID and then we

have name and address now if I have this

requirement to select me any random top

2 records so I can go ahead and say top

2 so this will give me top 2 records up

from this table so order is not

confirmed because we did not use any

order so we can run this one and you see

that it returned me 1 & 4 and only top 2

records are now if we are interested to

have the order so let's run this query

and say we want to order by ID and then

get the top or 2 records

so maybe you have a great date or

authentic column which is in the order

and let's say order by ID so I don't

have to write ascending now if you write

it fine otherwise it's gonna work the

same way so you write ascending is the

same thing so it is ordering the output

by ID in ascending order now once you

provide the order by that means you want

to see the data in that order for that

column and now you can use the top

Clause top - it means that I would like

to see Tartu records but from that

sordid data by idea sending up so here

so you see that now and this is the

records I am getting up so think if you

have ID as a dentist so there will be

keep incrementing and this can really

help like many one and get the

to records or the first two records

which was inserted to the table now if

you want to go other way around you say

okay I would like to see the last

inserted record in this table and my ID

is identical um so you can say

descending now you see that it is

returning as the last two records own so

first of all if you would like to see as

it is here so you see that we have five

four five and four and that now if we

will use a table with that so it is

going to return master in the dead top

records from this result set up so this

is really helpful the top you know with

the order by when you want to get the

top two records or bottom two records or

bottom end records so let's say three

records or four records whatever the

number you want to get so this really

helps and you have you can use the order

by with the columns and return those

results according to your requirement so

thanks very much for watching this video

and I hope this video will help you to

get to the top of bottom and rows and

you will be able to use order by Clause

with the ver and provide the results to

your users oh thanks and I will see you

at the next video