Pagination Cheat Sheet > Querying with SQL

 

Pagination

E-commerce applications like Amazon or Flipkart hold millions of products. But, the user does not require all the available products every time s/he accesses the application. Infact, fetching all the products takes too long and consumes huge amount of data.

Using pagination, only a chunk of the data can be sent to the user based on their request. And, the next chunk of data can be fetched only when the user asks for it.

  • We use
    LIMIT
    &
    OFFSET
    clauses to select a chunk of the results

Let's understand more about pagination concept using the following databse.

Database The database contains a

product
table that stores the data of products like name, category, price, brand and rating. You can check the schema and data of
product
table in the code playground.

LIMIT

LIMIT
clause is used to specify the
number of rows(n)
we would like to have in result.

Syntax

SELECT
column1,
column2,..
columnN
FROM
table_name
LIMIT n;
SQL

Example

Get the details of 2 top-rated products from the brand "Puma".

SELECT
name,
price,
rating
FROM
product
WHERE
brand = "Puma"
ORDER BY
rating DESC
LIMIT 2;
 
SQL
Expand
Output
namepricerating
Black Shirt6004.8
Blue Shirt10004.3

Try it Yourself!

  • Get the 3 lowest priced products from the brand "Puma".
    Note
    If the limit value is greater than the total number of rows, then all rows will be retrieved.

OFFSET

OFFSET
clause is used to specify the position (from nth row) from where the chunk of the results are to be selected.

Syntax

SELECT
column1,
column2,..
columnN
FROM
table_name
OFFSET n;
SQL

Example

Get the details of 5 top-rated products, starting from 5th row.

SELECT
name,
price,
rating
FROM
product
ORDER BY
rating DESC
LIMIT 5
OFFSET 5;
SQL

Output

namepricerating
Strawberry Cake104.6
Bourbon Special154.6
Realme Smart Band30004.6
Harry Potter and the Goblet of Fire4314.6
Black Jeans7504.5

Possible Mistakes

  • Using
    OFFSET
     before the
    LIMIT
    clause.
SELECT
*
FROM
product OFFSET 2
LIMIT 4;
SQL
Error: near "2": syntax error
SQL
  • Using only
    OFFSET
    clause.
SELECT
*
FROM
product
OFFSET 2;
SQL
Error: near "2": syntax error
SQL
Note
OFFSET
clause should be placed after the
LIMIT
clause. Default
OFFSET
value is 0.

Try it Yourself!

  • Get the details of 5 top-rated products, starting from 10th row.
View Schema
namecategorypricebrandrating
Blue ShirtClothing750Denim3.8
Blue JeansClothing800Puma3.6
Black JeansClothing750Denim4.5
Blue ShirtClothing1000Puma4.3
Chocolate CakeFood25Britannia3.7
Strawberry CakeFood60Cadbury4.1
Chocolate CakeFood60Cadbury2.5
Strawberry CakeFood10Britannia4.6
Smart WatchGadgets17000Apple4.9
Smart CamGadgets2600Realme4.7
Smart TVGadgets40000Sony4
Bourbon SmallFood10Britannia3.9
Bourbon SpecialFood15Britannia4.6
Bourbon With Extra CookiesFood30Britannia4.4
White ShirtClothing700Denim4.3
Black ShirtClothing600Puma4.8
Black T-ShirtClothing600Roadster4.2
White T-ShirtClothing700Levi's4
Blue T-ShirtClothing600Nike4.7
Realme Smart BandGadgets3000Realme4.6
Raw CashewFood370Absa3.9
Cashew NutsFood550Upcrop4.3
Chocolate CashewFood670Urban Platter3.5
Potato Chips India’s Magic MasalaFood42Lay's4.4
Banana ChipsFood550Calicut Kerala4.3
Potato Chips Cream & onionFood63Lay's4.5
Potato Chips Classic SaltedFood45Lay's4
Harry Potter and the Philosopher's StoneNovel2224.7
Harry Potter and the Chamber of SecretsNovel3434.4
Harry Potter and the Prisoner of AzkabanNovel2844.2
Harry Potter and the Goblet of FireNovel4314.6
OnePlus 6TSmartphone32990OnePlus4.5
Redmi K20Smartphone24999Redmi4.1

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form