fetch

7. Display nth row in SQL (Top 50 SQL Questions)| GeeksforGeeks



Sharing buttons:

hello and welcome to the next session of

interview preparatory course on top 50

SQL interview questions hope you are

excited to solve another a scale

question so let's get started

the question is to display n to row in

SQL for example he might ask you to find

the second row of the EMP table or him

at ask you to find a fourth row of the

imputable

in a row see by making use of this

rhonamcd column we can able to get the

required row the 4th row of the EMP

table is associated with the employee

Jones we are supposed to display only

the 4th row and remember one more thing

the rhone I'm so low column will not

work for greater than and is equal to

operator hence we need to use only a

less than operator while dealing with

Rotom pseudo column select star from EMP

where row no less than or equal to for

the first four records are displayed but

we need only Jones record so we need to

cut this first we need to eliminate

these three records by making use of

this minus operator we can do that what

- operator will do is that select star

from EMP where row num less than or

equal to 3 what this minus operator will

do is that the common records present in

first table is eliminated for example

the first three records are present in

table 1 the all these first three

records or eliminated - from the first

table that is how it works see we have

got the record of jorts

that is a fourth record now we can solve

this in another way also for example if

I want to find the fourth record the

same fourth record star from select row

numb our comma name comma sell from EMP

now this R will we are getting alias for

onnum that will is temporary column will

be assigned for row gnam in the EMP

table now here you can able to work with

is equal to operator with an enum

advantage of this approach is you can

work with is equal to operator as well

where directly you can display where or

equal to we don't want to display fourth

record right the fourth reason for the

record has been displayed that one day

of this approach is that you can

directly work with is equal to operator

and also you can display row number as

well in the previous example we can we

can display the record but we

unable to display Ronin well so if you

want to display the 60 record this is a

second approach now what if they asked

you to display all the columns present

in the table for example you need to

find all the details all the columns of

the fourth employ for example if you

want to find all them's of the second

employ whatever it may be

select star from select Ronin car create

a temporary storage we need to display

all the columns or EMP dot start a

gastric from EMP wear or equal to two or

whatever it may be or equal to 4 we am

taking the advantage of this approach is

that you you can able to find all the

columns all the columns of that

particular row including the row name

bar select star from created a crate

alias for row number e MP dot astrick

from EMP then this must be placed in a

sub query after that use our condition

where R equal to 4 that means roll

number equal to 4 very simple in this

approach you can make use of is equal to

operator as well and also you can

display row number and one more great

advantage is that you can display all

the columns of the required record in

this extension to the same question

displays 2nd 3rd and 7th recourse these

are not even record these are not odd

records they can be any random records

so just make something like this where

row number in 2 comma 3 comma 7 that's

it we are able to display the required

second record third the record on 7th

record this is the most optimized

approach that I have given to you that's

it with this we have come to the end of

the discussion if you have enjoyed the

session and it is really helpful to you

don't forget to like the video also do

share your questions with us in the

comment section let's all them together

and help other geeks as well thanks for

watching