As you know join means to join two or more things together. In the case of SQL, join means – “to combine two or more tables.” Join is a keyword which is used to fetch data from two different tables.
Different types of Joins are: There are mainly four types of SQL JOINS.
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
Suppose there are two tables in the database of a college. One table contains the ID, Name, email of all the teachers and the other table contains the ID and salary of all the teachers, so if we want the name and salary of any teacher, then we have to use Join – joins sql.
left join vs right join
LEFT JOIN | RIGHT JOIN |
---|---|
It joins two or more tables, returns all records from the left table, and matching rows from the right-hand table. | It is used to join two or more tables, returns all records from the right table, and matching rows from the left-hand table. |
The result-set will contain null value if there is no matching row on the right side table. | The result-set will contain null value if there is no matching row on the left side table. |
It is also known as LEFT OUTER JOIN. | It is also called as RIGHT OUTER JOIN. |
In which there is common data, but I want all the data of table1 together, for this we use left join, what is left join, what is the data matching inside both the tables. It brings with it whatever data is from the other table, it also brings it together.
MYSQL LEFT JOIN and RIGHT JOIN Today through this left join vs right join article we will understand about LEFT JOIN and RIGHT JOIN like suppose you have 2 tables table one and table two then we will table1 is considered as left table and which is our table2 we consider right table but I want to extract data by using it with select command.
One thing you have to keep in mind here, if no data is matching inside these two tables, then no data from table two will come inside table one, for this let me also explain you an example.
For example, suppose in the previous article we had created a table named Gurgaon and a table named City and the table named City has two columns inside it, one is the name of the city and one is the ID number.
The employee’s data comes inside the Gurgaon table, inside that we have made four-five columns and the CID column which is inside the city table, we made it primary key and this The table named Gurgaon has a column named city inside it.
We had made that column foreign key so what did it do, the data which was matching among themselves, that data had put us to sleep but there was some data which was not matching it left that data behind
But we want to see the data behind it, that is, we can see it, for this we use left join so we have to match these two tables with the city name The column named CID is matched with the column K. So the way to write LEFT JOIN is as follows
SELECT CLUMNS FROM TABLE1 LEFT JOIN TABLE2 ON TABLE1 . COL_NAME = TABLE2 . COL_NAME;
First of all you have to write SELECT, after that you have to write column name and if you want to see the data of column of whole table then you can use star and after write FROM table1 write LEFT JOIN Write > followed by the name of table2 followed by ON and after that in table1 write their name
select g.id, g.name ,g.per, g.age, g.GENDER, c.CITYNAME
from gurgaon g left join city c on g.city = c.CID;
left join vs right join
LEFT JOIN returns matching values/records of both tables and all values/records of Left Table. As well as RIGHT JOIN returns matching values/records of both tables and all values/records of Right Table.
Syntax of LEFT JOIN Clause
The following is the general syntax of LEFT JOIN:
SELECT column_list FROM table_name1
LEFT JOIN table_name2
ON column_name1 = column_name2
WHERE join_condition
The following is the general syntax of LEFT OUTER JOIN:
SELECT column_list FROM table_name1
LEFT OUTER JOIN table_name2
ON column_name1 = column_name2
WHERE join_condition
Syntax of RIGHT JOIN Clause
SELECT column_list FROM table_name1
RIGHT JOIN table_name2
ON column_name1 = column_name2
WHERE join_condition
RIGHT OUTER JOIN:
SELECT column_list FROM table_name1
RIGHT OUTER JOIN table_name2
ON column_name1 = column_name2
WHERE join_condition
LEFT JOIN Example
SELECT cust_id, cust_name, order_num, order_date
FROM customer LEFT JOIN orders
ON customer.cust_id = orders.order_id
WHERE order_date < '2020-04-30';
RIGHT JOIN Example
SELECT cust_id, cust_name, occupation, order_num, order_date
FROM customer
RIGHT JOIN orders ON cust_id = order_id
ORDER BY order_date;
I hope you get an idea about left join vs right join.