SQL LIMIT | Display certain rows from a table

Date Published: 29/07/2020 Published By: JaiSchool

LIMIT is used to display a certain number of rows from the table.

SELECT column FROM table LIMIT n;

n = Number of rows

Database: jai_school
Table: students

Rows:

s_nnameclassroll_nodue_fee
1Ram818000
2Rahim8216000
3Shyam837000
4Anita849000
5Jai857000
6Ravina6113000
7Aman623000

Show the first 4 rows

SELECT * FROM students LIMIT 4;

Output:
first four rows - sql limit

LIMIT has 2 parameters if we want to see rows starting from 10 to 20 then we will write query LIMIT 9,19 because rows indexing number starts from 0.

LIMIT 0,12 means from starting row (indexing number 0) to the next 12 rows (after the row having 0 indexing number).

LIMIT 40,10 means the row having indexing number 40 (39th row) to the next 10 rows after indexing number 40.

Show the last 4 rows

SELECT * FROM students ORDER BY s_n DESC LIMIT 4;

Output:
last for rows - sql limit

LIMIT with WHERE

SELECT * FROM students WHERE class="8" LIMIT 3;

Output:
sql limit with where

LIMIT with WHERE & ORDER BY

SELECT * FROM students WHERE class="8" ORDER BY s_n DESC LIMIT 3;

Output:
sql limit with where and order by

Publish A Comment

Leave a Reply

Your email address will not be published. Required fields are marked *