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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:07:50+00:00 2026-05-16T05:07:50+00:00

I have a simple hierarchy of categories in an SQLite3 database, each row storing

  • 0

I have a simple hierarchy of categories in an SQLite3 database, each row storing a parent ID or NULL as appropriate.

I would like to find out whether a particular category is a leaf, essentially by determining, for each row, whether it has a parent ID defined. Or rather, determining a count of child rows for each row.


Table definition:

CREATE TABLE category (
    id INTEGER PRIMARY KEY AUTOINCREMENT
    name TEXT NOT NULL
    parent_id INTEGER DEFAULT NULL
);

Sample data:

id          name        parent_id 
----------  ----------  ----------
34          People      
35          Countries   
36          USA         35
37          Pop         36
38          Rock        36
39          Japan       35
40          Pop         39
42          Rock        39
43          J-Pop       40

Desired output:
The original data plus a count of how many subcategories (children) each row has.

id          name        parent_id   direct_children
----------  ----------  ----------  ---------------
34          People                  0
35          Countries               2
36          USA         35          2
37          Pop         36          0
38          Rock        36          0
39          Japan       35          2
40          Pop         39          1
42          Rock        39          0
43          J-Pop       40          0

This appears potentially simple(?), but as I’m generally lost beyond simple JOINs, I so far haven’t got very far with this. I’ve checked similar questions, but they either seem to be joining across tables, or want to do a more complex count of all children across the hierarchy, not just the direct children rows.

Changing the table schema is a possibility (e.g. if a child_id or child_count) would be necessary, but I’d rather not.

Any input would most appreciated.

  • 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-16T05:07:50+00:00Added an answer on May 16, 2026 at 5:07 am

    You could probably do that with a subquery:

    select  c.*
    ,       (select count(*) from category c2 where c2.parent_id = c.id) 
                as direct_children
    from    category c
    

    Or a join:

    select  parent.id
    ,       parent.name
    ,       parent.parent_id
    ,       count(child.id) as direct_children
    from    category parent
    left join    
            category child
    on      child.parent_id = parent.id
    group by
            parent.id
    ,       parent.name
    ,       parent.parent_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple setter method for a property and null is not appropriate
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In general, is it a best practice to have simple POJO Java classes implement
I develop tools in Autodesk Maya. Many of the tools I build have simple
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple webform that will allow unauthenticated users to input their information,
I have a simple 2-column layout with a footer that clears both the right
I have a simple page with my ScriptManager and my UpdatePanel , and my
I have a simple little test app written in Flex 3 (MXML and some

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.