Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:46:41+00:00 2026-06-16T02:46:41+00:00

if you have parent table create table parent ( pid int not null, name

  • 0

if you have parent table

create table parent (
  pid int not null,
  name varchar(255)
)

and a parent-child join table

create table parent_child (
  pid int not null,
  cid int not null,
  foreign key (pid) references parent(pid),
  foreign key (cid) references child(cid)
)
create table child(
  cid int not null,
  name varchar(255)
)

How can I find all parent’s names where all their children have names in the following list (‘dave’,’henry’,’myriam’,’jill’).

I don’t want to see a parent if they have a child with a different name, but if they have 1 or more children and all their children have names in the list I want to see the parent’s name.

I did find this https://stackoverflow.com/a/304314/1916621 that will help me find a parent with children of the exactly those names, but I can’t figure out how to the parents who only have children with names in a subset of that list.

Extra points if someone knows performance tradeoff for different approaches.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:46:42+00:00Added an answer on June 16, 2026 at 2:46 am
    SELECT 
        p.pid, 
        p.name
    FROM 
        parent p
    WHERE NOT EXISTS (
        SELECT *
        FROM 
            parent_child pc 
            JOIN child c 
                ON pc.cid = c.cid
                AND c.name NOT IN ('dave','henry','myriam','jill')
        WHERE 
            p.pid = pc.pid
    ) AND EXISTS (
        SELECT *
        FROM 
            parent_child pc 
            JOIN child c 
                ON pc.cid = c.cid
                AND c.name IN ('dave','henry','myriam','jill')
        WHERE 
            p.pid = pc.pid
    )
    

    Another method… no sub-queries, but additional DISTINCT needed to eliminate duplication of parent records from joining to the parent_child table.

    SELECT DISTINCT
        p.pid, 
        p.name
    FROM 
        parent p 
        JOIN parent_child pc_exists ON pc_exists.pid = p.pid
        JOIN child c_exists 
            ON c_exists.cid = pc_exists.cid
            AND c_exists.name IN ('dave','henry','myriam','jill')
        LEFT JOIN parent_child pc_notExists ON pc_notExists.pid = p.pid
        LEFT JOIN child c_notExists 
            ON c_notExists.cid = pc_notExists.cid
            AND c_notExists.name NOT IN ('dave','henry','myriam','jill')
    WHERE
        c_notExists.cid IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these tables CREATE TABLE parent ( id NUMBER(10, 0) NOT NULL, name
I have for example two tables like: CREATE TABLE [dbo].[Parent]( [Id] [int] NOT NULL,
Say I have two tables: create table parent ( id number not null, constraint
I have an ai_order table. CREATE TABLE `ai_order`( `id` INT(11) NOT NULL AUTO_INCREMENT, `product_id`
Say I have two tables: create table parent ( number not null, constraint parent_pk
I currently have a parent table: CREATE TABLE members ( member_id SERIAL NOT NULL,
Let's asume I have a parent-child structure setup in SQL (server 2005): CREATE TABLE
Let's say we have these tables: CREATE TABLE A ( id SERIAL NOT NULL
I have two table Parent Table STUDENT ====> ID,NAME SqlCommand==>Select * from Student Child
I have a query which produces flattened hierarchy from a parent-child table (self join,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.