Solved

sql statement select what cant be joined

Posted on 2014-07-18
11
248 Views
Last Modified: 2014-07-20
Hi,

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


Questions:

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

0
Comment
Question by:jsbx
11 Comments
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 100 total points
Comment Utility
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)
0
 
LVL 4

Expert Comment

by:Philip Portnoy
Comment Utility
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 tableA.name from tableA inner join tableB on tableA.valueA = tableB.valueB) AND name NOT IN (select tableB.name 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 tableA.name from tableA inner join tableB on tableA.valueA = tableB.valueB) AND name NOT IN (select tableB.name 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.
0
 

Author Comment

by:jsbx
Comment Utility
testing ...
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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)
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 400 total points
Comment Utility
This query will show you the full combination -- both, or either one -- and which table, or both, had data:


SELECT
    COALESCE(a.nameA, b.nameb) AS Name,
    COALESCE(a.valueA, b.valueB) AS Value,
    CASE
        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
FULL OUTER JOIN tableB b ON
    b.nameB = a.nameA
ORDER BY Name
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 31

Expert Comment

by:awking00
Comment Utility
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)
0
 

Author Closing Comment

by:jsbx
Comment Utility
Thanks to all.
jsbx
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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
http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
0
 

Author Comment

by:jsbx
Comment Utility
No it wasn't.
I just simplified the question in order to avoid misunderstandings since english is not my native lang.
Jsbx
0
 

Author Comment

by:jsbx
Comment Utility
Portlet paul
Anyway the article you pointed is remarkable.  Thanks.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now