Node.js MySQL - SQRT()

In this post, we will discuss how to return square root of values from a column in MySQL XAMPP Server through Node.js application with SQRT() function.

It is important to install mysql package in node.js.

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

SQRT() Function:

SQRT() will return the square root of values in a table. It takes column name as a parameter.

Syntax:

SQRT(column)

Steps for Node.js script:

Now let's see steps

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query that uses SQRT() function
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT SQRT(column),.... FROM table_name 
      WHERE condition/s...", function (error, result) {
         console.log(result);
      });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the details table with the following records:

alt=

SQRT Example 1:-

Let's return square roots of values in price column.

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {
  // Write sql query that uses SQRT
  connection_data.query("SELECT price, SQRT(price) from details;", 
  function (error, result) {
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket { price: 12, 'SQRT(price)': 3.4641016151377544 },
  RowDataPacket { price: 34, 'SQRT(price)': 5.830951894845301 },
  RowDataPacket { price: 23, 'SQRT(price)': 4.795831523312719 },
  RowDataPacket { price: 45, 'SQRT(price)': 6.708203932499369 }
]

So, the square root for all 4 rows were returned.

SQRT Example 2:-

Let's return square roots of values in price column with name as facility1.

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {  
  // Write sql query that uses SQRT
  connection_data.query("SELECT price, SQRT(price) from 
  details where name='facility1';", function (error, result) {
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[ RowDataPacket { price: 12, 'SQRT(price)': 3.4641016151377544 } ]

So, the square root of price is returned with name-'facility1'.

Summary

In this post, we seen how to use SQRT() function on MySQL table in XAMPP Server with 2 examples along with WHERE Clause.