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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:56:39+00:00 2026-05-21T06:56:39+00:00

I have 4 tables namely, countries, states, cities, areas, apart from countries table the

  • 0

I have 4 tables namely,

countries, states, cities, areas,

apart from countries table the rest three(states,cities,areas) contains country_id foreign key.

i wanted to return the total number of count of country_id combined in three tables for which i used jon_darstar’s solution, here is the code i am using.

SELECT COUNT(DISTINCT(states.id)) + COUNT(DISTINCT(cities.id)) + COUNT(DISTINCT(areas.id))
FROM states
JOIN cities on cities.country_id = states.country_id
JOIN areas on areas.country_id = states.country_id
WHERE states.country_id IN (118);

the above code works perfectly fine although i am unable to understand the code properly, mainly the first line i.e

SELECT COUNT(DISTINCT(states.id)) + COUNT(DISTINCT(cities.id)) + COUNT(DISTINCT(areas.id))

Question 1 : doesn’t that select the
primary id of the three tables
states,cities and areas and make the
count? i know this is not happening
from the result i am getting then what
is actually happening here?

However if i remove the DISTINCT from the query string it shows me a different result i.e a count of 120 whereas actually it should be 15(this is the count number of country_id in all three tables).

Question 2 : What is happening if i
use DISTINCT and remove DISTINCT?
isn’t DISTINCT supposed to remove any
duplicate values. where is duplication
happening here?

thank you..

  • 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-21T06:56:40+00:00Added an answer on May 21, 2026 at 6:56 am

    For an example, if in a country A(having primary id a.id=118),there is State B(having primary id b.id),inside that state there is City C(having primary id c.id), In city C there’s Area D(having primary id d.id),E(having primary id e.id),F(f.id).lets visualize the query result in a database table.

    C St Ct Ar


    A->B->C->D

    A->B->C->E

    A->B->C->F

    (Here C=Country,St=States,Ct=Cities,Ar=Areas)

    Now just think what happens when you do count on above table to get total number of States within Country A without distinct.The result is 3,this way the Number of Cities is 3 and areas is 3,total 9.Because without distinct you’re getting duplicate values in which you’re not interested.

    Now,if you use distinct count you’ll get correct result cause here distinct states under
    country A is 1,City is 1 and Areas is 3,total:5(excluding duplicate values)..

    Hope this works!

    !!Design Issue!!!

    Like to add something:From your database design,i can see that you’re using country id as a reference for countries from country table(to states,areas and cities) then joining states and cities then states and areas (by their country id)don’t you think it’s creating cross join?.Better design choice is at areas table keep foreign key of city,this way go bottom up like in city keep states and in states keep country.Or make a table for Areas where you are keeping Countries,States,Cities foreign key and areas primary key.

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

Sidebar

Related Questions

I have four tables, namely countries,states,cities,areas which will be the best feasible solution for
I have two tables namely States, Package_Details. Below are the details of the table.
My prob in brief: I have two tables namely category and product. table: category
I have a database with three tables in my database namely catering, room, room_booking
I have three tables namely test1, test2, test3 test1 data =========== id test_id q_id
I have two tables, namely Price List (Table A) and Order Record (Table B)
I have two tables namely Stuff and Boss. Based on the slug(user_type) from the
i have a MySQL table namely estimate_item which contains few columns that holds decimal
I have 3 tables namely: Town; Village; Farmers...and I need to COUNT how many
I have tables linked by FK, I query on the first table using entity

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.