Node.js Tutorial
Estimated reading: 4 minutes 26 views

๐Ÿง  Node.js โ€“ MySQL: Advanced Queries โ€“ JOINs & Table Dropping in Practice

๐Ÿงฒ Introduction โ€“ Move Beyond Basics with Powerful MySQL + Node.js Queries

Once you’ve mastered basic CRUD operations, itโ€™s time to leverage advanced SQL queries in Node.js applications. This includes using JOINs to combine data across multiple tables and safely dropping tables when needed.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to use INNER JOIN, LEFT JOIN, and RIGHT JOIN in Node.js
  • How to combine rows from related tables using foreign keys
  • How to safely drop (delete) tables using SQL via Node.js

๐Ÿ“˜ Topics Covered

๐Ÿ”น Topic๐Ÿ“– Description
๐Ÿ”— Node.js โ€“ MySQL Join QueriesCombine multiple tables using INNER, LEFT, and RIGHT JOINs
๐Ÿ—‘๏ธ Node.js โ€“ MySQL Drop TablePermanently remove a table from the database

๐Ÿ”— Node.js โ€“ MySQL Join Queries

๐Ÿ”น Scenario: Employees & Departments

Assume you have two tables:

CREATE TABLE departments (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department_id INT,
  FOREIGN KEY (department_id) REFERENCES departments(id)
);

โœ… Letโ€™s use JOIN to connect employee names with their departments.


๐Ÿ”น INNER JOIN โ€“ Get Only Matching Records

const sql = `
  SELECT employees.name AS employee, departments.name AS department
  FROM employees
  INNER JOIN departments ON employees.department_id = departments.id
`;

db.query(sql, (err, results) => {
  if (err) throw err;
  console.log(results);
});

๐Ÿงช Output:

[
  { employee: "Alice", department: "Sales" },
  { employee: "Bob", department: "IT" }
]

โœ… Only returns employees assigned to a valid department.


๐Ÿ”น LEFT JOIN โ€“ Include All Employees, Even Without Department

const sql = `
  SELECT employees.name AS employee, departments.name AS department
  FROM employees
  LEFT JOIN departments ON employees.department_id = departments.id
`;

db.query(sql, (err, results) => {
  if (err) throw err;
  console.log(results);
});

๐Ÿงช Output:

[
  { employee: "Alice", department: "Sales" },
  { employee: "Charlie", department: null }
]

โœ… Useful for identifying unassigned or incomplete records.


๐Ÿ”น RIGHT JOIN โ€“ Include All Departments

const sql = `
  SELECT employees.name AS employee, departments.name AS department
  FROM employees
  RIGHT JOIN departments ON employees.department_id = departments.id
`;

db.query(sql, (err, results) => {
  if (err) throw err;
  console.log(results);
});

๐Ÿงช Output:

[
  { employee: "Bob", department: "IT" },
  { employee: null, department: "HR" }
]

โœ… Useful for seeing which departments have no employees.


๐Ÿ—‘๏ธ Node.js โ€“ MySQL Drop Table

๐Ÿ”น Drop a Single Table

db.query("DROP TABLE employees", (err, result) => {
  if (err) throw err;
  console.log("Table 'employees' dropped.");
});

๐Ÿงช Output:

Table 'employees' dropped.

โœ… Always ensure backups before executing destructive operations.


๐Ÿ”น Drop Table If Exists

db.query("DROP TABLE IF EXISTS departments", (err, result) => {
  if (err) throw err;
  console.log("Table 'departments' dropped (if existed).");
});

๐Ÿงช Output:

Table 'departments' dropped (if existed).

โœ… This avoids errors if the table doesn’t exist.


๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Advanced SQL queries like JOINs are critical for connecting and analyzing data across tables, while DROP TABLE enables cleanup and schema management. Node.js makes it easy to execute and manage such operations programmatically.

๐Ÿ” Key Takeaways:

  • INNER JOIN fetches matching records across tables
  • LEFT JOIN includes unmatched records from the left table
  • RIGHT JOIN includes unmatched records from the right table
  • DROP TABLE permanently deletes a table structure

โš™๏ธ Real-World Uses:

  • Multi-table reporting in admin dashboards
  • Cleaning up test or outdated database schemas
  • Creating complex relationships in e-commerce systems (e.g., products & categories)

โ“ Frequently Asked Questions

โ“ What is the difference between INNER and LEFT JOIN?
โœ… INNER JOIN returns matching rows only. LEFT JOIN returns all rows from the left tableโ€”even if thereโ€™s no match.


โ“ Is it safe to use DROP TABLE in production?
โœ… Use with caution. Always back up data and use DROP TABLE IF EXISTS to avoid fatal errors.


โ“ Can I join more than two tables in a single query?
โœ… Yes. You can chain multiple JOINs to query across three or more related tables.


โ“ What happens if I drop a table with foreign keys?
โœ… MySQL will throw an error unless CASCADE is enabled or keys are removed first.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿง  Node.js โ€“ MySQL: Advanced Queries

Or Copy Link

CONTENTS
Scroll to Top