ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Right Join in MySql

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='7' AND `tutorial_submenu`='52' AND `tutorial_status`=1 LIMIT 1

Right Join in MySql

RIGHT JOIN in MySQL

The RIGHT JOIN (or RIGHT OUTER JOIN) in MySQL returns all records from the right table and the matching records from the left table. If no match is found, NULL values are returned for the left table.


1. Syntax

SELECT columnsFROM left_tableRIGHT JOIN right_tableON left_table.common_column = right_table.common_column;

  • left_table β†’ The table on the left side of the JOIN clause.
  • right_table β†’ The table on the right side of the JOIN clause.
  • If there’s no match, NULL values appear in the left table’s columns.

2. Example Tables

πŸ“Œ Table: departments (Right Table)

dept_iddept_name
1IT
2HR
3Finance

πŸ“Œ Table: employees (Left Table)

emp_idnamedept_id
101Alice1
102Bob2
103CharlieNULL


3. RIGHT JOIN Example

SELECT employees.emp_id, employees.name, departments.dept_nameFROM employeesRIGHT JOIN departments ON employees.dept_id = departments.dept_id;

βœ… Result:

emp_idnamedept_name
101AliceIT
102BobHR
NULLNULLFinance

Explanation:

  • Alice and Bob have matching department IDs.
  • Finance department has no matching employee, so NULL appears in emp_id and name.

4. Difference Between RIGHT JOIN and LEFT JOIN

FeatureLEFT JOINRIGHT JOIN
Returns all rows fromLeft TableRight Table
Unmatched rows appear asNULL in Right TableNULL in Left Table

πŸ’‘ Example of LEFT JOIN (Opposite of RIGHT JOIN):

SELECT employees.emp_id, employees.name, departments.dept_nameFROM employeesLEFT JOIN departments ON employees.dept_id = departments.dept_id;


5. RIGHT JOIN with WHERE Clause

To filter results:

SELECT employees.emp_id, employees.name, departments.dept_nameFROM employeesRIGHT JOIN departments ON employees.dept_id = departments.dept_idWHERE departments.dept_name = 'Finance';

βœ… Result:

emp_idnamedept_name
NULLNULLFinance


Key Takeaways

βœ… RIGHT JOIN returns all rows from the right table and matches from the left table.
βœ… If no match is found, NULL values appear in the left table columns.
βœ… Use WHERE to filter results.
βœ… Opposite of LEFT JOIN.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql