🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Node.js Notes
Topic #58

MySQL Join


Join Two or More Tables

You can combine rows from two or more tables, based on a related column between them, by using a JOIN statement.

Consider you have a "users" table and a "products" table:

users

[
  { id: 1, name: 'John',
favorite_product: 154},
  { id:
2, name: 'Peter', favorite_product: 154},
  { id: 3, name: 'Amy',
favorite_product: 155},
  { id: 4, name: 'Hannah', favorite_product:},
  { id: 5, name: 'Michael',
favorite_product:}
]

products

[
  { id: 154, name:
'Chocolate Heaven' },
  { id: 155, name: 'Tasty Lemons' },
  {
id: 156, name: 'Vanilla Dreams' }
]

These two tables can be combined by using users' favorite_product field and products' id field.

Example

let mysql = require('mysql');

let con = mysql.createConnection({

host: "localhost",
  user: "yourusername",

password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  let
sql = "SELECT users.name AS user, products.name AS favorite FROM users
JOIN products ON
users.favorite_product = products.id";

con.query(sql, function (err, result) {

if (err) throw err;
    console.log(result);
  });
});

Note: You can use INNER JOIN instead of JOIN. They will both give you the same result.

Save the code above in a file called "demo_db_join.js" and run the file:

C:\Users\Your Name>node demo_db_join.js

Which will give you this result:

[
  { user: 'John', favorite:
'Chocolate Heaven' },
  { user: 'Peter', favorite: 'Chocolate Heaven' },
  {
user: 'Amy', favorite: 'Tasty Lemons' }
]

As you can see from the result above, only the records with a match in both tables are returned.


Left Join

If you want to return all users, no matter if they have a favorite product or not, use the LEFT JOIN statement:

Example

  SELECT users.name AS user,
products.name AS favorite
FROM users
LEFT
  JOIN products ON users.favorite_product = products.id

Which will give you this result:

[
  { user: 'John', favorite:
'Chocolate Heaven' },
  { user: 'Peter', favorite: 'Chocolate Heaven' },
  {
user: 'Amy', favorite: 'Tasty Lemons' },
  {
user: 'Hannah', favorite: null },
  { user: 'Michael', favorite: null }
]

Right Join

If you want to return all products, and the users who have them as their favorite, even if no user have them as their favorite, use the RIGHT JOIN statement:

Example

  SELECT users.name AS user,
products.name AS favorite
FROM users
RIGHT
  JOIN products ON users.favorite_product = products.id

Which will give you this result:

[
  { user: 'John', favorite:
'Chocolate Heaven' },
  { user: 'Peter', favorite: 'Chocolate Heaven' },
  {
user: 'Amy', favorite: 'Tasty Lemons' },
  {
user: null, favorite: 'Vanilla Dreams' }
]

Note: Hannah and Michael, who have no favorite product, are not included in the result.

Want to go beyond the notes?

Join CodingNow 2.0's Node.js course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

MySQL Join – FAQs

Quick answers about learning MySQL Join in Node.js.

This free note from CodingNow 2.0 explains MySQL Join in Node.js — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Node.js topic on CodingNow 2.0, including MySQL Join, is 100% free with no signup required.
With focused practice, most students grasp MySQL Join in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now