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

MySQL Order By


Sort the Result

Use the ORDER BY statement to sort the result in ascending or descending order.

The ORDER BY keyword sorts the result ascending by default. To sort the result in descending order, use the DESC 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;

con.query("SELECT * FROM customers ORDER BY name", function (err, result) {

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

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

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

Which will give you this result:

[
  { id: 3, name: 'Amy',
address: 'Apple st 652'},

{ id: 11, name: 'Ben', address: 'Park Lane 38'},
  { id: 7,
name: 'Betty', address: 'Green Grass 1'},
  { id: 13, name: 'Chuck', address:
'Main Road 989'},
  { id: 4, name: 'Hannah', address:
'Mountain 21'},
  { id: 1, name: 'John', address: 'Higheay 71'},
  { id: 5, name: 'Michael', address: 'Valley 345'},
  { id:
2, name: 'Peter', address: 'Lowstreet 4'},
  { id: 8, name: 'Richard',
address: 'Sky st 331'},

{ id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
  { id: 9, name: 'Susan', address: 'One way
98'},
  { id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
  { id: 14, name: 'Viola', address: 'Sideway
1633'},
  { id: 12, name: 'William',
address: 'Central st 954'}
]

ORDER BY DESC

Use the DESC keyword to sort the result in a descending order.

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;

con.query("SELECT * FROM customers ORDER BY name DESC", function (err, result) {

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

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

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

Which will give you this result:

[
  { id: 12, name: 'William',
address: 'Central st 954'},
  { id: 14, name: 'Viola', address: 'Sideway
1633'},
  { id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
  { id: 9, name: 'Susan', address: 'One way
98'},

{ id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
  { id: 8, name: 'Richard',
address: 'Sky st 331'},
  { id:
2, name: 'Peter', address: 'Lowstreet 4'},
  { id: 5, name: 'Michael', address: 'Valley 345'},
  { id: 1, name: 'John', address: 'Higheay 71'},
  { id: 4, name: 'Hannah', address:
'Mountain 21'},
  { id: 13, name: 'Chuck', address:
'Main Road 989'},
  { id: 7,
name: 'Betty', address: 'Green Grass 1'},

{ id: 11, name: 'Ben', address: 'Park Lane 38'},
  { id: 3, name: 'Amy',
address: 'Apple st 652'}
]

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 Order By – FAQs

Quick answers about learning MySQL Order By in Node.js.

This free note from CodingNow 2.0 explains MySQL Order By 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 Order By, is 100% free with no signup required.
With focused practice, most students grasp MySQL Order By 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