NodeJS MySQL Max() Function
We will be using Max() function in MySQL to get Maximum value of some particular column.
Syntax:
MAX(column_name)
Parameters: MAX() function accepts a single parameter as mentioned above and described below.
- column_name: Column’s name from which we have to return the max value.
Return Value: MAX() function returns the maximum value from the particular column in the table.
Module Installation: Install the MySQL module using the following command.
npm install mysql
Database: Our SQL publishers table preview with sample data is shown below.
Example 1:
idex.js
const mysql = require( "mysql" ); let db_con = mysql.createConnection({ host: "localhost" , user: "root" , password: '' , database: 'gfg_db' }); db_con.connect((err) => { if (err) { console.log( "Database Connection Failed !!!" , err); return ; } console.log( "We are connected to gfg_db database" ); // here is the query let query = "SELECT MAX(salary) AS max_salary FROM publishers" ; db_con.query(query, (err, rows) => { if (err) throw err; console.log(rows); }); }); |
Output:
Example 2:
index.js
const mysql = require( "mysql" ); let db_con = mysql.createConnection({ host: "localhost" , user: "root" , password: '' , database: 'gfg_db' }); db_con.connect((err) => { if (err) { console.log( "Database Connection Failed !!!" , err); return ; } console.log( "We are connected to gfg_db database" ); // here is the query let query = "SELECT MAX(salary) AS max_salary FROM publishers WHERE id < 8" ; db_con.query(query, (err, rows) => { if (err) throw err; console.log(rows); }); }); |
Output:
Please Login to comment...