OriyanJ OriyanJ - 10 months ago 42
SQL Question

Query that returns parent items with no children

I have a table that looks like this:

enter image description here

It has products. Some are related to a parent product by a "Parent ID".

I need a query that will only select parent products that don't have any children related to it. Such as "Product-4".

Can anyone help me with this? It seems quite an easy solution.

select NAME from my_table where ID not in (select parent_ID from my_table)

should do it