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 8945899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:22:09+00:00 2026-06-15T12:22:09+00:00

I have a table with categories, each category as an ID, a Name and

  • 0

I have a table with categories, each category as an ID, a Name and a ParentID. The problem is that there are 3 levels, parent categories, sub categories and child categories.

I can extract parent categories with a simple SELECT and a WHERE ParentID IS NULL clause as such:

SELECT *
FROM Category
WHERE ParentID IS NULL

However, a WHERE ParentID IS NOT NULL clause will return both, sub categories as well as child categories.

I’m looking for a way to extract only sub categories, and only child categories.

  • 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-15T12:22:12+00:00Added an answer on June 15, 2026 at 12:22 pm

    Typically, for these sort of problems, it is better to use the Recursive Queries Using Common Table Expressions. Something like so:

    ;WITH CategoriesTree(CategoryID, CategoryName, ParentName, CategoryLevel)
    AS
    (
       SELECT 
         c.ID, 
         c.Name, 
         CAST('No Parent' AS VARCHAR(50)) AS ParentName, 
         0 AS CategoryLevel
       FROM @Categories c
       WHERE c.ParentID IS NULL
       UNION ALL
       SELECT c.ID, c.Name, p.CategoryName, p.CategoryLevel + 1
       FROM CategoriesTree p
       INNER JOIN @Categories c ON c.ParentID = p.CategoryID
    )
    SELECT * 
    FROM CategoriesTree
    Where CategoryLevel = some id;
    

    SQL Fiddle Demo

    This will give you:

    CATEGORYID       CATEGORYNAME         PARENTNAME      CATEGORYLEVEL
        1         Root Cateogry         No Parent               0
        2         Sub Cateogry 1        Root Cateogry           1
        3         Sub Cateogry 2        Root Cateogry           1
        4         Sub Cateogry 3        Root Cateogry           1
        8         sub Cateogry 1 of 3   Sub Cateogry 3          2
        7         Sub Cateogry 1 of 2   Sub Cateogry 2          2
        5         Sub Cateogry 1 of 1   Sub Cateogry 1          2
        6         sub Cateogry 2 of 1   Sub Cateogry 1          2
    

    How does this work?

    Using this query, you can control what level of categories you want to select. For instance, for the sample data, I used in the previous demo, here is the categories tree:

                            1: RootCategory                Category Level:  0
                                   |
                                   |
                     ---------------------------- 
                     |             |            |
                     |             |            |
                  2: Sub1        3: Sub2      4: sub3      Category Level:   1
                     |             |            |
              ------------         |            |
              |          |         |            |
              |          |         |            |
         5: Sub1of1  6: Sub2of1   7: sub1of2   8: sub1of3  Category Level:   2
    

    This query will give you this categories tree with the new generated CategoryLevel column.

    Note that: In the sample data I used in the demo, there was only one parent category (the categories with parentid IS NULL). However, the query will for work fine in case there were a lot of parent categories. And this because of the anchor query of the CTE, which is:

    SELECT 
         c.ID, 
         c.Name, 
         CAST('No Parent' AS VARCHAR(50)) AS ParentName, 
         0 AS CategoryLevel
    FROM @Categories c
    WHERE c.ParentID IS NULL;
    

    Then, you can use the generated column CategoryLevel to select only the child categories of the level that you are interested in.

    For example, if you need to select only the sub categories of the first sub categories of the root category, you can get these categories using the predicate CategoryLevel = 2:

    ;WITH CategoriesTree(CategoryID, CategoryName, ParentName, CategoryLevel)
    AS
    (
        ...
    )
    SELECT * 
    FROM CategoriesTree
    WHERE CategoryLevel = 2;
    

    This will give you:

    CATEGORYID       CATEGORYNAME         PARENTNAME      CATEGORYLEVEL
        8        sub Cateogry 1 of 3    Sub Cateogry 3          2
        7        Sub Cateogry 1 of 2    Sub Cateogry 2          2
        5        Sub Cateogry 1 of 1    Sub Cateogry 1          2
        6        sub Cateogry 2 of 1    Sub Cateogry 1          2
    

    SQL Fiddle Demo

    • 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 in which each category has a ParentId that can
I have a table of categories. Each category can either be a root level
I have a table categories that has: id, name, subcategory_id, parent_id . Another table
I have a table of categories in my database, as below. Category categoryId name
I have a table for product category that has a hierarchical structure. Each Category_ID
If I have a table of categories with a featured row that is default
I have a categories table view controller. In my app, categories can have subcategories.
Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
I have a tree (nested categories) stored as follows: CREATE TABLE `category` ( `category_id`
I have a cat table that has the following columns: cat_id | name |

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.