Solved

SQL Query to get orphaned nodes

Posted on 2014-11-11
4
227 Views
Last Modified: 2014-11-11
Hi all,
I have an interesting query that I am trying to create...

I have a table that contains entities, each of these *may* have a parent (only single level)

What I would like to do is query the table to find entries that say they have a parent, but the parent entry does not exist.

tblData
ID, DESCRIPTION, PARENT
'A','Main Parent','-'
'B','First Child','A'
'C','Second Child','A'
'D','Another Parent','-'
'E','An Orphan','Z'

In this example, I would like to execute a query to get the record relating to 'E'

I've tried a few ways, but can't get it to work!!!

Any ideas would be very much appreciated..

Thanks a lot,

James
0
Comment
Question by:jatkin
4 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 250 total points
ID: 40434781
select a.*
from tblData a
left join tblData b on a.Parent = b.ID
where a.Parent != '-' and b.ID is null

Open in new window

0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40434783
Try this:

Select T.ID
from tblData T
LEFT JOIN tblData U
on T.[Parent] = U.ID
where U.ID is null

Open in new window

0
 
LVL 33

Expert Comment

by:ste5an
ID: 40434792
You should use a foreign key constraint to avoid this situation.

SELECT  C.*
FROM    #tblData C
        LEFT JOIN #tblData P ON C.PARENT = P.ID
WHERE   P.ID IS NULL;

Open in new window

0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 40434793
Perfect!!! Thanks :-)
Hope it is okay to split points equally as there was only 1 minute between posts!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

840 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