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 ).
Visual 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+---------+--------------+-----------+------------+ | ITEM_ID | ITEM_NAME | ITEM_UNIT | COMPANY_ID | +---------+--------------+-----------+------------+ | 1 | Chex Mix | Pcs | 16 | | 6 | Cheez-It | Pcs | 15 | | 2 | BN Biscuit | Pcs | 15 | | 3 | Mighty Munch | Pcs | 17 | | 4 | Pot Rice | Pcs | 15 | | 5 | Jaffa Cakes | Pcs | 18 | | 7 | Salt n Shake | Pcs | | +---------+--------------+-----------+------------+Sample table: company
+------------+---------------+--------------+ | COMPANY_ID | COMPANY_NAME | COMPANY_CITY | +------------+---------------+--------------+ | 18 | Order All | Boston | | 15 | Jack Hill Ltd | London | | 16 | Akas Foods | Delhi | | 17 | Foodies. | London | | 19 | sip-n-Bite. | New York | +------------+---------------+--------------+
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:
-- Selecting specific columns from the 'company' table and the 'foods' table
SELECT company.company_id, company.company_name, company.company_city, foods.company_id, foods.item_name
-- Joining the 'company' table with the 'foods' table using a RIGHT JOIN
FROM company
RIGHT JOIN foods
-- Matching rows from 'company' and 'foods' where the company_id values are equal
ON company.company_id = foods.company_id;
Explanation:
- This SQL query is retrieving data from two tables: 'company' and 'foods'.
- It selects specific columns from these tables: 'company_id', 'company_name', and 'company_city' from the 'company' table, and 'company_id' and 'item_name' from the 'foods' table.
- The query uses a RIGHT JOIN to combine rows from the 'company' table with matching rows from the 'foods' table.
- The join condition is specified in the ON clause, which matches rows based on the equality of 'company_id' values between the two tables.
- Since it's a RIGHT JOIN, all rows from the 'foods' table are included in the result set, and only matching rows from the 'company' table are included.
- If there are no matching rows in the 'company' table for a particular row in the 'foods' table, NULL values will be returned for the columns selected from the 'company' table.
- This query is useful for retrieving information about food items and the companies that produce them, ensuring that all food items are included in the result regardless of whether they have associated companies (hence the RIGHT JOIN).
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
Visual Presentation of the above example:
RIGHT JOIN: Relational Databases
Key points to remember
Click on the following to get the slides presentation -
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Previous: SQL LEFT JOIN
Next: SQL FULL OUTER JOIN
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql/joins/perform-a-right-join.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics