Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return rows from left table not found in right table?

I have two tables with similar column names and I need to return records from the left table which are not found in the right table? I have a primary key(column) which will help me to compare both tables. Which join is preferred?

like image 801
StarJedi Avatar asked Sep 05 '14 12:09

StarJedi


People also ask

How do I return a row from left table to right table?

The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.

How do I get all the rows from the left table?

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.

How do you return not matching records from two tables?

SELECT B. Accountid FROM TableB AS B LEFT JOIN TableA AS A ON A.ID = B. Accountid WHERE A.ID IS NULL; LEFT JOIN means it takes all the rows from the first table - if there are no matches on the first join condition, the result table columns for table B will be null - that's why it works.


1 Answers

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

enter image description here

like image 164
Shamseer K Avatar answered Oct 18 '22 16:10

Shamseer K