MySQL SQL

MySQL RIGHT JOIN

MySQL RIGHT JOIN Keyword

All records from the right table (table 2) and any matching records from the left table (table 1) are returned using the RIGHT JOIN keyword.

MySQL RIGHT JOIN -

RIGHT JOIN Syntax

				
					SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
				
			

Demo Database

We’ll be using the well-known Northwind sample database in this tutorial.

A sample from the “Orders” table is shown below:

OrderIDCustomerIDEmployeeIDOrderDateShipperID
10308271996-09-183
103093731996-09-191
103107781996-09-202

And a selection from the “Employees” table:

EmployeeIDLastNameFirstNameBirthDatePhoto
1DavolioNancy12/8/1968EmpID1.pic
2FullerAndrew2/19/1952EmpID2.pic
3LeverlingJanet8/30/1963EmpID3.pic

MySQL RIGHT JOIN Example

All employees, along with any orders they may have placed, can be obtained by running the following SQL statement:

Example

				
					SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
				
			

Note: Even in cases when there are no matches in the left table (Orders), the RIGHT JOIN keyword retrieves all records from the right table (Employees).

Share this Doc

MySQL RIGHT JOIN

Or copy link

Explore Topic