Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of inner join

Tags:

sql

database

What will be the opposite of inner join? For a table table Person (int PersonId, varchar PersoName, int AddrId), I want to know the rows in Person with bad AddrId which don't have a row in the Address table.

like image 414
fastcodejava Avatar asked Aug 06 '10 04:08

fastcodejava


People also ask

What is opposite of inner join?

If you consider an inner join as the rows of two tables that meet a certain condition, then the opposite would be the rows in either table that don't.

What is not inner join?

The opposite of an INNER JOIN is an OUTER JOIN and it comes in two flavors: LEFT and RIGHT depending on which side of of the JOIN you want to "outer"

Is an outer join the opposite of an inner join?

There are four main types of joins: inner join, full outer join, left outer join and right outer join. The major difference between inner and outer joins is that inner joins result in the intersection of two tables, whereas outer joins result in the union of two tables.

What is inner join and outer join?

Different Types of SQL JOINs Here are the different types of the JOINs in SQL: (INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table.


1 Answers

What will be the opposite of inner join?

An OUTER join, which can be of three options:

  • LEFT
  • RIGHT
  • FULL

This is a good visual representation of JOINs

I want to know the rows in Person with bad AddrId which don't have a row in the Address table.

Using LEFT JOIN/IS NULL

   SELECT p.*      FROM PERSON p LEFT JOIN ADDRESS a ON a.addrid = p.addrid     WHERE a.addrid IS NULL 

Using NOT EXISTS

SELECT p.*   FROM PERSON p  WHERE NOT EXISTS(SELECT NULL                     FROM ADDRESS a                    WHERE a.addrid = p.addrid) 

Using NOT IN

SELECT p.*   FROM PERSON p  WHERE p.addrid NOT IN (SELECT a.addrid                           FROM ADDRESS a) 
like image 64
OMG Ponies Avatar answered Sep 20 '22 05:09

OMG Ponies