Node.js MySQL ORDER BY

Node.js MySQL ORDER BY is used to in conjunction with SELECT FROM Query to sort the records in ascending or descending order with respect to a column.

In this tutorial, we will learn how to get rows of MySQL table in ascending or descending order based on specified column.

By default, Node.js MySQL ORDER BY results in an ascending order of elements, or you may also use ASC keyword. For descending order of records, DESC keyword should be used.

Example 1 – ORDER Rows from MySQL Table in Ascending Order – Node.js

In this example, we will get rows from MySQL table sorted in ascending order.

example.js

// include mysql module
var mysql = require('mysql');

// create a connection variable with the required details
var con = mysql.createConnection({
  host: "localhost",	// ip address of server running mysql
  user: "arjun",	// user name to your mysql database
  password: "password",	// corresponding password
  database: "studentsDB" // use the specified database
});

// make to connection to the database.
con.connect(function(err) {
  if (err) throw err;
  // if connection is successful
  con.query("SELECT * FROM students ORDER BY marks", function (err, result, fields) {
	// if any error while executing above query, throw error
    if (err) throw err;
	// if there is no error, you have the result
    console.log(result);
  });
});

Run the above Node.js MySQL ORDER BY example program.

Output

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node example.js 
[ RowDataPacket { name: 'Ross', rollno: 7, marks: 54 },
  RowDataPacket { name: 'John', rollno: 1, marks: 74 },
  RowDataPacket { name: 'Arjun', rollno: 2, marks: 74 },
  RowDataPacket { name: 'Prasanth', rollno: 3, marks: 77 },
  RowDataPacket { name: 'Adarsh', rollno: 4, marks: 78 },
  RowDataPacket { name: 'Sai', rollno: 6, marks: 84 },
  RowDataPacket { name: 'Monica Gellar', rollno: 8, marks: 86 },
  RowDataPacket { name: 'Bruce Wane', rollno: 10, marks: 92 },
  RowDataPacket { name: 'Raja', rollno: 5, marks: 94 },
  RowDataPacket { name: 'Lee', rollno: 9, marks: 98 },
  RowDataPacket { name: 'Sukumar', rollno: 11, marks: 99 } ]

The records are sorted in ascending order with respect to marks column.

ADVERTISEMENT

Example 2 – ORDER Rows from MySQL Table in Ascending Order

In this example, we will sort rows based on a column of type TEXT.

example.js

// include mysql module
var mysql = require('mysql');

// create a connection variable with the required details
var con = mysql.createConnection({
  host: "localhost",	// ip address of server running mysql
  user: "arjun",	// user name to your mysql database
  password: "password",	// corresponding password
  database: "studentsDB" // use the specified database
});

// make to connection to the database.
con.connect(function(err) {
  if (err) throw err;
  // if connection is successful
  con.query("SELECT * FROM students ORDER BY name", function (err, result, fields) {
	// if any error while executing above query, throw error
    if (err) throw err;
	// if there is no error, you have the result
    console.log(result);
  });
});

Run the above Node.js MySQL ORDER BY example program.

Output

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node example.js 
[ RowDataPacket { name: 'Adarsh', rollno: 4, marks: 78 },
  RowDataPacket { name: 'Arjun', rollno: 2, marks: 74 },
  RowDataPacket { name: 'Bruce Wane', rollno: 10, marks: 92 },
  RowDataPacket { name: 'John', rollno: 1, marks: 74 },
  RowDataPacket { name: 'Lee', rollno: 9, marks: 98 },
  RowDataPacket { name: 'Monica Gellar', rollno: 8, marks: 86 },
  RowDataPacket { name: 'Prasanth', rollno: 3, marks: 77 },
  RowDataPacket { name: 'Raja', rollno: 5, marks: 94 },
  RowDataPacket { name: 'Ross', rollno: 7, marks: 54 },
  RowDataPacket { name: 'Sai', rollno: 6, marks: 84 },
  RowDataPacket { name: 'Sukumar', rollno: 11, marks: 99 } ]

The records are sorted in ascending order with respect to name column.

Example 3 – ORDER Rows in Descending Order

In this example, we will sort rows in descending order of a specific column “name”.

example.js

// include mysql module
var mysql = require('mysql');

// create a connection variable with the required details
var con = mysql.createConnection({
  host: "localhost",	// ip address of server running mysql
  user: "arjun",	// user name to your mysql database
  password: "password",	// corresponding password
  database: "studentsDB" // use the specified database
});

// make to connection to the database.
con.connect(function(err) {
  if (err) throw err;
  // if connection is successful
  con.query("SELECT * FROM students ORDER BY name DESC", function (err, result, fields) {
	// if any error while executing above query, throw error
    if (err) throw err;
	// if there is no error, you have the result
    console.log(result);
  });
});

Run the above Node.js MySQL ORDER BY example program.

Output

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node DescOrderExample.js 
[ RowDataPacket { name: 'Sukumar', rollno: 11, marks: 99 },
  RowDataPacket { name: 'Sai', rollno: 6, marks: 84 },
  RowDataPacket { name: 'Ross', rollno: 7, marks: 54 },
  RowDataPacket { name: 'Raja', rollno: 5, marks: 94 },
  RowDataPacket { name: 'Prasanth', rollno: 3, marks: 77 },
  RowDataPacket { name: 'Monica Gellar', rollno: 8, marks: 86 },
  RowDataPacket { name: 'Lee', rollno: 9, marks: 98 },
  RowDataPacket { name: 'John', rollno: 1, marks: 74 },
  RowDataPacket { name: 'Bruce Wane', rollno: 10, marks: 92 },
  RowDataPacket { name: 'Arjun', rollno: 2, marks: 74 },
  RowDataPacket { name: 'Adarsh', rollno: 4, marks: 78 } ]

The records are sorted in descending order with respect to name column.

Conclusion

In this Node.js TutorialNode.js MySQL Module -Node.js MySQL ORDER BY – we have learnt to sort the records in ascending or descending order w.r.t. a column with example Node.js MySQL programs.