🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Node.js Notes
Topic #57

MySQL Limit


Limit the Result

You can limit the number of records returned from the query, by using the "LIMIT" statement:

Example

let mysql = require('mysql');

let con = mysql.createConnection({

host: "localhost",
  user: "yourusername",

password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;

let sql = "SELECT * FROM customers LIMIT 5";

con.query(sql, function (err, result) {

if (err) throw err;
    console.log(result);
  });
});

Save the code above in a file called "demo_db_limit.js" and run the file:

C:\Users\Your Name>node demo_db_limit.js

Which will give you this result:

[
  { id: 1, name: 'John', address: 'Highway 71'},
  { id:
2, name: 'Peter', address: 'Lowstreet 4'},
  { id: 3, name: 'Amy',
address: 'Apple st 652'},
  { id: 4, name: 'Hannah', address:
'Mountain 21'},
  { id: 5, name: 'Michael', address: 'Valley 345'}
]

Start From Another Position

If you want to return five records, starting from the third record, you can use the "OFFSET" keyword:

Example

let mysql = require('mysql');

let con = mysql.createConnection({

host: "localhost",
  user: "yourusername",

password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;

let sql = "SELECT * FROM customers LIMIT 5 OFFSET 2";

con.query(sql, function (err, result) {

if (err) throw err;
    console.log(result);
  });
});

Note: "OFFSET 2", means starting from the third position, not the second!

Save the code above in a file called "demo_db_offset.js" and run the file:

C:\Users\Your Name>node demo_db_offset.js

Which will give you this result:

[
  { id: 3, name: 'Amy',
address: 'Apple st 652'},
  { id: 4, name: 'Hannah', address:
'Mountain 21'},
  { id: 5, name: 'Michael', address: 'Valley 345'},

{ id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
  { id: 7, name: 'Betty',
address: 'Green Grass 1'}
]

Shorter Syntax

You can also write your SQL statement like this "LIMIT 2, 5" which returns the same as the offset example above:

Example

let mysql = require('mysql');

let con = mysql.createConnection({

host: "localhost",
  user: "yourusername",

password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;

let sql = "SELECT * FROM customers LIMIT 2, 5";

con.query(sql, function (err, result) {

if (err) throw err;
    console.log(result);
  });
});

Note: The numbers are reversed: "LIMIT 2, 5" is the same as "LIMIT 5 OFFSET 2"

Want to go beyond the notes?

Join CodingNow 2.0's Node.js course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

MySQL Limit – FAQs

Quick answers about learning MySQL Limit in Node.js.

This free note from CodingNow 2.0 explains MySQL Limit in Node.js — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Node.js topic on CodingNow 2.0, including MySQL Limit, is 100% free with no signup required.
With focused practice, most students grasp MySQL Limit in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now