SQL RIGHT JOIN clause is very similar with SQL LEFT JOIN clause, it is used to select all the matched records from the right side table, regardless if it has matched records in the left side table or not.
SQL RIGHT JOIN Clause Syntax
SELECT Table1.Column1, Table1.Column2, ... , Table2.Column1, Table2.Column2, ... FROM Table1 RIGHT JOIN Table2 ON Table1.ColumnName = Table2.ColumnName
SQL RIGHT JOIN Clause Example
Table: Employees
EmployeeId | FirstName | LastName | Department | Salary |
---|---|---|---|---|
203 | Ahooc | Yuish | Finance | 78000 |
204 | Quisheen | Nnuchen | Finance | 45800 |
205 | Lshhwe | Aceed | Finance | 57000 |
Table: Tickets
TicketID | TicketNo | EmployeeId |
---|---|---|
1 | 2200 | 203 |
2 | 2338 | 204 |
3 | 2369 | 205 |
4 | 2410 | 206 |
Select all the tickets, if they belong to any employees, list the employees:
SELECT Employees.EmployeeId , Employees.FirstName , Employees.LastName , Tickets.TicketNo FROM Employees RIGHT JOIN Tickets ON Employees.EmployeeId = Tickets.EmployeeId
The result will look like:
EmployeeId | FirstName | LastName | TicketNo |
---|---|---|---|
203 | Ahooc | Yuish | 2200 |
204 | Quisheen | Nnuchen | 2338 |
205 | Lshhwe | Aceed | 2369 |
2410 |
Note: The ticket 2410 doesn’t have any records matched in the table Employees, but it is still listed in the result with the Employee information null.