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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:17:30+00:00 2026-05-26T12:17:30+00:00

I need to execute this query it’s like ( main cats and sub cats

  • 0

I need to execute this query it’s like ( main cats and sub cats )

Here is my schema:

++++++++++++++++++++++++++++++++
+  id | Country     |  Parent  |
++++++++++++++++++++++++++++++++
+  1  |  India      |          |
++++++++++++++++++++++++++++++++
+  2  |  Usa        |          |
++++++++++++++++++++++++++++++++
+  3  | California  |   Usa    |
++++++++++++++++++++++++++++++++
+  4  | New York    |   Usa    |
++++++++++++++++++++++++++++++++
+  5  | New Delhi   |  India   |
++++++++++++++++++++++++++++++++
+  6  | France      |          |
++++++++++++++++++++++++++++++++

And I want to get the result like this:

+++++++++++++++++++++++++++++++++++++++++++++++++
+  id | Country     | Num |  Childs             |
+++++++++++++++++++++++++++++++++++++++++++++++++
+  1  |  India      |  1  |  New Delhi          |
+++++++++++++++++++++++++++++++++++++++++++++++++
+  2  | Usa         |  2  | California,New York |
+++++++++++++++++++++++++++++++++++++++++++++++++
+  3  | France      |  0  |                     |
+++++++++++++++++++++++++++++++++++++++++++++++++
  • 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-26T12:17:31+00:00Added an answer on May 26, 2026 at 12:17 pm

    Try this:

    SELECT t1.id, t1.Country, 
        (SELECT COUNT(id) FROM world t2
         WHERE t2.Parent = t1.Country) as Num,
        (SELECT GROUP_CONCAT(Country) FROM world t3
         WHERE t3.Parent = t1.Country) as Childs
    FROM world t1
    WHERE t1.Parent IS NULL
    

    HOW IT WORKS

    My query is made of a main part and two subqueries (the ones between parenthesis); you can think of a subquery as a normal query executed in a main query returing to this some field (column). Subqueries can interact with main query because they can use main fields in SELECT or WHERE.

    Main part gets all base countries (the ones without parent)

       SELECT t1.id, t1.Country
       FROM world t1
       WHERE t1.Parent IS NULL
    

    First subquery gets (for every item in main part) the count of countries whose parent is main item

    SELECT COUNT(id) FROM world t2
    WHERE t2.Parent = t1.Country
    

    Note that in WHERE clause I ask to match subquery id with parent id, so I get only the count of countries related to main item.
    Second subquery is similar to first one, but a different field is returned using GROUP_CONCAT function to get all subcountries separated with commas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to execute this query to find out the next auto_increment value that
Lets say that I need to execute this query with EF in business layer
i need to execute this query in postgres but i couldn't get rid of
I need to execute this simple mysql query in Zend; UPDATE `table` SET `field`='field'+1
I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this
I need execute an SQL query in ORACLE it takes a certain amount of
I need to execute MDX query from command line (MS AS 2005). I have
When i execute this query it seems to cause the mysqld.exe process to use
ALTER DATABASE test SET ENABLE_BROKER this is the sql query i wish to execute
I need to get a SQL Query that I can execute to extract certain

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.