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

  • SEARCH
  • Home
  • 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 6237315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:00:21+00:00 2026-05-24T11:00:21+00:00

I have a table Category with Parent and Child relations my table schema is

  • 0

I have a table Category with Parent and Child relations my table schema is below:

CategoryID       ParentID        CategoryName
1                  0                A
2                  1                B
3                  1                C
4                  2                D
5                  0                E
6                  5                F
7                  5                G
8                  6                I
9                  1                J
10                 0                L

Now When I want to select all categories which did not have the ParentId 1 and it’s Child CategoryID, my output should be look like below when I am passing CategoryID 1

CategoryID        ParentID        CategoryName
5                   0                  E
6                   5                  F
7                   5                  G
8                   6                  H
10                  0                  L

how I can find the results look like that.

  • 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-05-24T11:00:22+00:00Added an answer on May 24, 2026 at 11:00 am

    The gist of this is to

    1. select all records that descend from Category 1
    2. exclude them from the final result

    SQL Statement

    ;WITH q AS (
        SELECT  CategoryID, ParentID
        FROM    Category
        WHERE   CategoryID =1           
        UNION ALL
        SELECT  c.CategoryID, c.ParentID
        FROM    Category c
                INNER JOIN q ON q.CategoryID = c.ParentID
    )
    SELECT  *
    FROM    Category c
    WHERE   NOT EXISTS (SELECT CategoryID FROM q WHERE q.CategoryID = c.CategoryID)
    

    Test data

    ;WITH Category (CategoryID, ParentID, CategoryName) AS (
        SELECT  1, 0, 'A'
        UNION ALL SELECT 2, 1, 'B'
        UNION ALL SELECT 3, 1, 'C'
        UNION ALL SELECT 4, 2, 'D'
        UNION ALL SELECT 5, 0, 'E'
        UNION ALL SELECT 6, 5, 'F'
        UNION ALL SELECT 7, 5, 'G'
        UNION ALL SELECT 8, 6, 'I'
        UNION ALL SELECT 9, 1, 'J'
        UNION ALL SELECT 10, 0, 'L'
    )   
    , q AS (
        SELECT  CategoryID, ParentID
        FROM    Category
        WHERE   CategoryID =1           
        UNION ALL
        SELECT  c.CategoryID, c.ParentID
        FROM    Category c
                INNER JOIN q ON q.CategoryID = c.ParentID
    )
    SELECT  *
    FROM    Category c
    WHERE   NOT EXISTS (SELECT CategoryID FROM q WHERE q.CategoryID = c.CategoryID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a categories table Category [ID, Title] and a Parent/Child table CategoryParent [ID,CategoryID,ParentCategoryID]
I have the following hierarchical table: Table Category: CategoryId, ParentCategoryId, CategoryName 1, null, SomeRoot
I have three tables Category, Movies and RelCatMov Category-table categoryid, categoryName 1 thriller 2
I have db table with parent child relationship as: NodeId NodeName ParentId ------------------------------ 1
I have a products table and a categories table. The categories have a parent-child
I have two table with parent child relationship and still confuse how to map
I have a Table Category, 1) Id 2) CategoryName 3) CategoryMaster with data as:
I'm really upset with Hibernate! I have a database table (mysql) that holds parent-child
I have a table with the following structure - Category {Id, Name, ParentId} I
Conditions : I have two tables, category(id_kat,name_kat) book(id_book,id_kat,name_book) id_kat has a child and parent

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.