sql statement select what cant be joined

Posted on 2014-07-18
Last Modified: 2014-07-20

I have 2 tables:

**** TableA ****

NameA    valueA
-------  --------
john     10
mary     20
jack     30
tom       5

Open in new window

*** TableB ****

NameB    valueB
-------  --------
Sarah    10
Thomas   30
Ive      9

Open in new window

I know how to join this tables and get the ones that have the same value, I do it this way and get the following data:

select * from tableA inner join tableB on tableA.valueA = tableB.valueB

John 10 Sarah 10
Jack 30 Thomas 30

Open in new window


Q1. How do I get only the ones present in table A, in this example:
Tom 5
    Mary 20

Open in new window

Q2. How do I get only the ones present in table B, in this example:

Ive 9

Open in new window

Question by:jsbx
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 100 total points
ID: 40205225
There are several ways to get this data, have a read of this article: How to find rows that are in one table, but not another which shows you several ways to do it.

You can use NOT IN, NOT EXISTS, a LEFT JOIN checking for NULL values or the EXCEPT operator (for SQL Server)

Expert Comment

by:Philip Portnoy
ID: 40205239
Do you have a unique value in the tables? If you do then it's very easy.
Let's assume your name is a unique value.

For these values from tableA:
select * from tableA where name NOT IN (select from tableA inner join tableB on tableA.valueA = tableB.valueB) AND name NOT IN (select from tableA inner join tableB on tableA.valueA = tableB.valueB)

Open in new window

For values from tableB:
select * from tableB where name NOT IN (select from tableA inner join tableB on tableA.valueA = tableB.valueB) AND name NOT IN (select from tableA inner join tableB on tableA.valueA = tableB.valueB)

Open in new window

Please note that the fact that I don't know your database structure and what's the data in there this code is really "dirty" and might require optimization.

Author Comment

ID: 40205259
testing ...
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

LVL 32

Expert Comment

ID: 40205439
As pointed out there are severla ways to do this. Here is one -
For Q1
select * from tableA
where valueA in
(select valueA from tableA except select valueA from tableB)

For Q2
select * from tableb
where valueb in
(select valueB from tableB except select valueA from tableA)
LVL 69

Accepted Solution

Scott Pletcher earned 400 total points
ID: 40205441
This query will show you the full combination -- both, or either one -- and which table, or both, had data:

    COALESCE(a.nameA, b.nameb) AS Name,
    COALESCE(a.valueA, b.valueB) AS Value,
        WHEN a.nameA IS NULL THEN 'Only in B'  --or 'Missing from A'
        WHEN a.nameB IS NULL THEN 'Only in A'  --or 'Missing from B'
        ELSE 'In both A and B' END  --or 'Not missing in either'
        AS In_Which_Table
FROM tableA a
    b.nameB = a.nameA
LVL 32

Expert Comment

ID: 40205448
And another way -
select * from TableA as a
where not exists
(select 1 from tableB as b
 where a.valueA = b.valueB)

select * from TableB as b
where not exists
(select 1 from tableA as a
 where a.valueA = b.valueB)

Author Closing Comment

ID: 40205487
Thanks to all.
LVL 49

Expert Comment

ID: 40206882
Was this homework? I'm surprised no-one asked.

I'm also surprised no-one offered this: the visual chart available here is very useful

Author Comment

ID: 40207355
No it wasn't.
I just simplified the question in order to avoid misunderstandings since english is not my native lang.

Author Comment

ID: 40207367
Portlet paul
Anyway the article you pointed is remarkable.  Thanks.

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question