Krivalar Tutorials 
Krivalar Tutorials



SQLv- RIGHT OUTER JOIN

<< Previous - SQL LEFT OUTER Join

Next - SQL FULL Join >>




What is RIGHT JOIN in SQL?

  • RIGHT OUTER JOIN returns the result set that contains all rows of the table specified on the right side of the join clause and the matching rows from the table on the LEFT side.
  • in other words, the query will return all the rows from the second table, even when there is no match on the left side table.



RIGHT JOIN - Syntax


SELECT Table1.column-name1 Table1.column-name2  Table2.column-name 2......
FROM Table-name1
LEFT JOIN Table-name2
ON Table1.column =  Table2.column;

Example with RIGHT JOIN

Consider the demo tables that are given below:

  1. Student_Personal Table:
    
    +--------+---------+------+-----------+
    | RollNo | Name    | Age  | City      |
    +--------+---------+------+-----------+
    |      1 | Aruna   |   18 | Chennai   |
    |      2 | Varun   |   19 | Bangalore |
    |      3 | Ara     |   19 | Kerala    |
    |      4 | Markdin |   18 | Mumbai    |
    |      5 | Kannan  |   20 | Kerala    |
    |      6 | Kanika  |   18 | Chennai   |
    |      7 | Jose    |   19 | Kerala    |
    |     10 | Sreeja  |   20 | Bangalore |
    +--------+---------+------+-----------+
    
  2. Student_ID Table:
    
    +--------+-------+---------+------+
    | RollNo | ID_NO | Name    | Dept |
    +--------+-------+---------+------+
    |      1 |   101 | Arun    | CSE  |
    |      2 |   102 | Varun   | IT   |
    |      3 |   103 | Ara     | ECE  |
    |      4 |   104 | Markdin | ECE  |
    |      5 |   105 | Kannan  | IT   |
    |      6 |   106 | Aruna   | CSE  |
    |      7 |   107 | Jose    | ECE  |
    |      8 |   108 | Jithu   | CSE  |
    |      9 |   109 | Sai     | ECE  |
    +--------+-------+---------+------+
    

RIGHT JOIN query statement can be used to join these two tables:


mysql> SELECT Student_Personal.Rollno,Student_Personal.Name,Student_ID.ID_NO,Student_ID.Dept
    -> FROM Student_Personal
    -> RIGHT JOIN Student_ID
    -> ON student_Personal.RollNo = Student_ID.RollNo;

When you execute this above statement, you would get the following result-set



+--------+---------+-------+------+
| Rollno | Name    | ID_NO | Dept |
+--------+---------+-------+------+
|      1 | Aruna   |   101 | CSE  |
|      2 | Varun   |   102 | IT   |
|      3 | Ara     |   103 | ECE  |
|      4 | Markdin |   104 | ECE  |
|      5 | Kannan  |   105 | IT   |
|      6 | Kanika  |   106 | CSE  |
|      7 | Jose    |   107 | ECE  |
|   NULL | NULL    |   108 | CSE  |
|   NULL | NULL    |   109 | ECE  |
+--------+---------+-------+------+
9 rows in set (0.00 sec)

<< Previous - SQL LEFT OUTER Join

Next - SQL FULL Join >>









Searching using Binary Search Tree