Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Mysql Order By in NodeJs

Mysql Order By in NodeJs

Using ORDER BY in MySQL with Node.js lets you sort your query results by one or more columns β€” ascending (ASC) or descending (DESC).

Let’s see how to use it in your Node.js app step-by-step πŸ‘‡


βœ… Step 1: Install MySQL Package

bash

npm install mysql const mysql = require('mysql');const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase'});connection.connect();const sql = 'SELECT * FROM users ORDER BY age ASC';connection.query(sql, (err, results) => { const sql = 'SELECT * FROM users ORDER BY name DESC';connection.query(sql, (err, results) => { if (err) throw err; console.log('Users ordered by name (descending):'); console.table(results);});


πŸ”Ή Using mysql2 with async/await

js

const mysql = require('mysql2/promise');async function getSortedUsers() { const connection = await mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase' }); const [rows] = await connection.execute('SELECT * FROM users ORDER BY age DESC'); console.table(rows); await connection.end();}getSortedUsers();


🧠 ORDER BY Tips

TaskSQL Example
Sort by multiple columnsORDER BY age ASC, name DESC
Limit + sortORDER BY age DESC LIMIT 3
Order NULLs last (MySQL 8+)ORDER BY age IS NULL, age ASC

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql