Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

How to select the nth row of the table in PostgreSQL?

$
0
0

Here we will discuss the technique to fetch nth row of the table in PostgreSQL. I will use the concept of the LIMIT and OFFSET Clause.



Syntax to fetch nth row of the table in PostgreSQL

 


 SELECT col_1, col_2,...

  FROM table_name

  [ ORDER BY ... ]

  [ LIMIT { number | ALL } ] [ OFFSET number ]


LIMIT Clauseis used to limit the data amount returned by the SELECT statement. 

OFFSETallows retrieving just a portion of the rows that are generated by the rest of the query.

I will create a table to understand the concept. 


 

CREATETABLEpublic."Customer"

(

    "Id"serialNOTNULL,

    "FName"textCOLLATEpg_catalog."default",

    "LName"textCOLLATEpg_catalog."default",

    "UserName"textCOLLATEpg_catalog."default",

    "Password"textCOLLATEpg_catalog."default",

    "Contact"textCOLLATEpg_catalog."default",

    "Address"textNOTNULLDEFAULT 0.0,

    CONSTRAINT"pk_Customer_Id"PRIMARYKEY ("Id")

 

);

 

--Inserted some records

 

INSERTINTOpublic."Customer"(

  "FName","LName","UserName","Password","Contact","Address")

  VALUES ('Dilip Kumar','Singh','dilip@123','xyz','9098876676','Noida');

 

INSERTINTOpublic."Customer"(

  "FName","LName","UserName","Password","Contact","Address")

  VALUES ('Dilshad','Ahmad','dilshad@123','xyz','8898646427','Delhi');

  

INSERTINTOpublic."Customer"(

  "FName","LName","UserName","Password","Contact","Address")

  VALUES ('Ashish','Singh','ashish@123','xyz','9087778765','Ghaziabad');

 

INSERTINTOpublic."Customer"(

  "FName","LName","UserName","Password","Contact","Address")

  VALUES ('Mr Dilip','Singh','dk@123','xyz','9087876654','Gorakhpur');

 

INSERTINTOpublic."Customer"(

  "FName","LName","UserName","Password","Contact","Address")

   VALUES ('Permanand','Tripathi','peram@123','xyz','98988876676','Gorakhpur');

 

INSERTINTOpublic."Customer"(

  "FName","LName","UserName","Password","Contact","Address")

 

   VALUES ('Naina','Singh','naina@123','xyz','9998875756','Gr Noida');

 


Get the Nth row of the table in PostgreSQL

If you want to fetch the record of the 5th row then execute the following query.

 

 

SELECT*FROMpublic."Customer"LIMIT 1 OFFSET 4;

 

 If you want to fetch the record of 3th row then execute the following query.

 

 

SELECT*FROMpublic."Customer"LIMIT 1 OFFSET 2;

 

 

Now we will create a function that will get the nth row of the "Customer" table.

 

 

CREATEORREPLACEFUNCTIONget_nth_row_customer(nNumberinteger)

RETURNSTABLE

(

       "Id"int,

    "FName"text,

    "LName"text,

    "UserName"text,

    "Password"text,

    "Contact"text,

    "Address"text

)     

AS$BODY$

BEGIN         

RETURNQUERYSELECT*FROMpublic."Customer"LIMIT 1 OFFSETnNumber-1;

END;

$BODY$LANGUAGEplpgsql;

 

 

Get the Nth row of the table throw function get_nth_row_customer

 

 

SELECT*FROMget_nth_row_customer(4);

 

 

Result:

 Id |   FName   |  LName   | UserName  | Password |   Contact   |  Address

----+-----------+----------+-----------+----------+-------------+-----------

  5 | Permanand | Tripathi | peram@123 | xyz      | 98988876676 | Gorakhpur

(1 row)



Viewing all articles
Browse latest Browse all 265

Trending Articles