I have two tables with one primary key present on both of them lets call them LEFT table and RIGHT table.I want to make a join such that I get all the rows of left table only ,i.e. if ROW from RIGHT table matches the JOIN condition it should get horizontally appended to the specific row.Else all NA in second half of the row,so that number of rows in the join can only by equal to number of rows in LEFT table
2 Answers
You are describing a LEFT JOIN
. e.g.
select table1.column1 as "left", table2.column2 as "right"
from table1
left join table2 on table1.column1 = table2.column2;
This will list all values in table1.column1 on the left, and any values in table2.column2 that match on the right. If they don't match, the field in the right column will be null.
I have two tables with one primary key present on both of them lets call them LEFT table and RIGHT table.I want to make a join such that I get all the rows of left table only.
The SQL LEFT JOIN
returns all rows from the left table.
This means that a left join returns all the values from the left table, plus matched values from the right table or NULL in case of no matching join predicate.
The Syntax is as follows:-
SELECT table1.column1, table2.column2...
FROM table1
LEFT JOIN table2
ON table1.common_field = table2.common_field;
Note:- Here table1 is left table & table2 is right table.