SQL RIGHT JOIN - The Coding Shala
Home >> Learn SQL >> SQL RIGHT JOIN
Table 2. 'dept' table
The following SQL statement is an example of SQL RIGHT JOIN -
SQL RIGHT JOIN
The SQL RIGHT JOIN is a part of the OUTER JOIN. The SQL RIGHT JOIN or SQL RIGHT OUTER JOIN returns all the records from the right table and matching records from the left table. If there is no match in the left table then it returns NULL values from the left table.SQL RIGHT JOIN Syntax
The basic syntax of the RIGHT JOIN is as follows -
SELECT table1.column1, table2.column2, .. FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
SQL RIGHT JOIN Example
The following two tables are used for the examples -
Table 1. 'emp' table
emp_id emp_name city dept_no salary 1 Akshay Pune 101 50000 3 Nikhil Pune 101 51000 5 Mohit Delhi 103 40000 6 Shubham Surat 105 42000 7 Akash Mumbai 106 45000
Table 2. 'dept' table
dept_no dept_name total_emp 101 Product Dev 50 102 Consulting 100 103 Product Consult 20 104 Marketing 150 105 Sales 250
The following SQL statement is an example of SQL RIGHT JOIN -
select emp.emp_id, emp.emp_name, dept.dept_no, dept.dept_name from emp right join dept on emp.dept_no = dept.dept_no;
Other Posts You May Like
Comments
Post a Comment