How to return rows from left table not found in right table?
Try This
SELECT f.*
FROM first_table f LEFT JOIN second_table s ON f.key=s.key
WHERE s.key is NULL
For more please read this article : Joins in Sql Server
If you are asking for T-SQL then lets look at fundamentals first. There are three types of joins here each with its own set of logical processing phases as:
- A
cross join
is simplest of all. It implements only one logical query processing phase, aCartesian Product
. This phase operates on the two tables provided as inputs to the join and produces a Cartesian product of the two. That is, each row from one input is matched with all rows from the other. So if you have m rows in one table and n rows in the other, you get m×n rows in the result. - Then are
Inner joins
: They apply two logical query processing phases:A Cartesian product
between the two input tables as in a cross join, and then itfilters
rows based on a predicate that you specify inON
clause (also known asJoin condition
). Next comes the third type of joins,
Outer Joins
:In an
outer join
, you mark a table as apreserved
table by using the keywordsLEFT OUTER JOIN
,RIGHT OUTER JOIN
, orFULL OUTER JOIN
between the table names. TheOUTER
keyword isoptional
. TheLEFT
keyword means that the rows of theleft table
are preserved; theRIGHT
keyword means that the rows in theright table
are preserved; and theFULL
keyword means that the rows inboth
theleft
andright
tables are preserved.The third logical query processing phase of an
outer join
identifies the rows from the preserved table that did not find matches in the other table based on theON
predicate. This phase adds those rows to the result table produced by the first two phases of the join, and usesNULL
marks as placeholders for the attributes from the nonpreserved side of the join in those outer rows.
Now if we look at the question: To return records from the left table which are not found in the right table use Left outer join
and filter out the rows with NULL
values for the attributes from the right side of the join.