12 - Combining Two Tables- JOIN ( Inner , Left and Right)

 

What is join in SQL database?

SQL JOIN is a clause that is used to combine multiple tables and retrieve data based on a common field in relational databases


TABLES WITH NAME employees AND department_location WITH ONE COMMON COLUMN NAME department


employees



department_location




INNER JOIN

The INNER JOIN keyword selects records that have matching values in both tables.


select e.first_name,e.salary,d.department,d.location
from employees e
inner join department_location d
on e.department=d.department;





LEFT JOIN

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). 

The result is 0 records from the right side, if there is no match

select e.first_name,e.salary,d.department,d.location
from employees e
left join department_location d
on e.department=d.department;






RIGHT JOIN

The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the left table (table1). 

The result is 0 records from the left side, if there is no match.


select e.first_name,e.salary,d.department,d.location
from employees e
right join department_location d
on e.department=d.department;




























Comments

Popular posts from this blog

5 - DML commands used in MySQL WorkBench

INDEX OF Zeek MySQL

4 - DDL commands used in MySQL Workbench