SQL Right Join
RIGHT JOIN
The SQL RIGHT JOIN, joins two tables and fetches rows based on a condition, which is matching in both the tables ( before and after the JOIN clause mentioned in the syntax below) , and the unmatched rows will also be available from the table written after the JOIN clause ( mentioned in the syntax below ).
Pictorial presentation of SQL Right Join:
Syntax:
SELECT * FROM table1 RIGHT [ OUTER ] JOIN table2 ON table1.column_name=table2.column_name;
SQL RIGHT join fetches a complete set of records from table2, i.e. the rightmost table after JOIN clause, with the matching records (depending on the availability) in table1. The result is NULL in the left side when no matching will take place.
Syntax diagram - SQL Right Join
Example of SQL Right Join or right outer join
Sample table: foods
Sample table: company
To get company ID, company name and company city columns from company table and company ID, item name columns from foods table, after an OUTER JOINING with these mentioned tables, the following SQL statement can be used:
SQL Code:
SELECT company.company_id,company.company_name,
company.company_city,foods.company_id,foods.item_name
FROM company
RIGHT JOIN foods
ON company.company_id = foods.company_id;
Explanation:
This SQL statement would return all rows from the foods table and only those rows from the company table where the joined fields are equal and if the ON clause matches no records in the company table, the join will still return rows, but the NULL in each column of company table.
Output:
COMPANY_ID COMPANY_NAME COMPANY_CITY COMPANY_ID ITEM_NAME ---------- ------------------------- ------------------------- ---------- -------------- 18 Order All Boston 18 Jaffa Cakes 15 Jack Hill Ltd London 15 Pot Rice 15 Jack Hill Ltd London 15 BN Biscuit 15 Jack Hill Ltd London 15 Cheez-It 16 Akas Foods Delhi 16 Chex Mix 17 Foodies. London 17 Mighty Munch Salt n Shake
Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition
Pictorial Presentation of the above example:
RIGHT JOIN: Relational Databases
Key points to remember
Click on the following to get the slides presentation -
Practice SQL Exercises
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.
Previous: SQL LEFT JOIN
Next: SQL FULL OUTER JOIN
SQL: Tips of the Day
SQL Server SELECT into existing table.
INSERT INTO dbo.TABLETWO SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:
INSERT INTO dbo.TABLETWO (col1, col2) SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
Database: SQL Server
Ref: https://bit.ly/3y6tpA3
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework