Node.js MySQL Delete Query Last Updated : 07 Oct, 2021 Comments Improve Suggest changes Like Article Like Report We use SQL DELETE Query to delete data with some condition from MySQL Table. Syntax: This will delete all rows of customers' tables but everything else will remain as it is (indexing, etc). DELETE FROM users This will delete all rows of users table where userId is 2. DELETE FROM users WHERE userId = 2 Modules: mysql: To handle sql connection and queries npm install mysql SQL users table preview: Example 1: Delete all rows from users table JavaScript 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 our query let query = 'DELETE FROM users'; db_con.query(query, (err, rows) => { if(err) throw err; console.log('Cleared users Table'); }); }); Output: Example 2: Delete all users whose name = 'pratik'. Approach 1: Hard code the value JavaScript 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"); // This is our Hard Coded Query let query = "DELETE FROM users WHERE name = 'pratik'"; db_con.query(query, (err, rows) => { if(err) throw err; console.log('Number of rows deleted = ' + rows.affectedRows); }); }); Approach 2: Dynamic Query JavaScript 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"); // notice the ? in below query let query = "DELETE FROM users WHERE name = ?"; // Dynamic Value let name = 'pratik'; // Notice the name variable below // It will replace the ? in query db_con.query(query, name, (err, rows) => { if(err) throw err; console.log('Number of rows deleted = ' + rows.affectedRows); }); }); Output: Comment More infoAdvertise with us Next Article Node.js MySQL Delete Query P pratikraut0000 Follow Improve Article Tags : Technical Scripter Web Technologies Node.js Technical Scripter 2020 NodeJS-MySQL +1 More Similar Reads Node.js MySQL Create Table Introduction: Learn to create a table in MySQL database using NodeJS. We will see how to use the Create Table command in NodeJS using the MySQL module. Prerequisite: Introduction to NodeJS MySQL Setting up environment and Execution: Step 1: Create a NodeJS Project and initialize it using the followi 2 min read Node.js MySQL Order By Clause Introduction: We use the SQL ORDER BY Clause to sort the data with respect to some column value in ascending or descending order. Syntax: SELECT * FROM users ORDER BY name; This will sort all rows of output in ascending order (by default) with respect to name column. SELECT address FROM users ORDER 2 min read Node.js MySQL Drop Table DROP TABLE Query is used to Delete or Drop a table from MySQL Database. Syntax: This will delete users table. But this will throw error if users table is not there. DROP TABLE users This will delete users table only if it exist. DROP TABLE IF EXISTS users Modules: mysql: To handle MySQL connection a 2 min read Node.js MySQL SUM() Function We use the SUM() function in MySQL to get Sum of the value of some columns. Syntax: SUM(column_name) Parameters: SUM() 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. Module Installation: Install the m 2 min read Node.js MySQL TRIM() Function TRIM() function is a built-in function in MySQL that is used to trim all spaces from both sides of the input string. Syntax: TRIM(string)Parameters: It takes one parameter as follows: string: It is the string to be trimmed from both sides.Return Value: It returns string after trimming from both side 2 min read Like