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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:17:06+00:00 2026-06-10T07:17:06+00:00

I have two tables: t1 with the following columns: name | key | length

  • 0

I have two tables:
t1 with the following columns: name | key | length
t2 with the following columns: name | country.

I need to select all distinct keys with length>2000 group by country. So, I made

SELECT count(distinct key), country
from db.t1
inner join db.t2
on t1.name=t2.name
where length>2000
group by country;

But, when I make the query:

SELECT count(distinct key)
from db.t1
where Length>2000;

I am supposed to get equal results but I’m getting different results. For example, in the first query, I get 125494 and in the second I get: 121653.

What is the reason for this different results?? Knowing that there are some fields in the country are ''. It seems to me they don’t appear as a group and i counted them and found that they are 134 records. but I can’t find out the reason.

  • 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-10T07:17:07+00:00Added an answer on June 10, 2026 at 7:17 am

    Unless key is UNIQUE (in which case, why bother with the DISTINCT keywords?), there is no reason that your two queries should return the same results.

    Suppose t1 contains:

    +------+-----+--------+
    | name | key | length |
    +------+-----+--------+
    |    a |   x |   5000 |
    |    b |   x |   5000 |
    |    b |   y |   5000 |
    |    c |   z |   5000 |
    +------+-----+--------+
    

    And t2 contains:

    +------+---------+
    | name | country |
    +------+---------+
    |    a |      uk |
    |    b |      fr |
    |    c |      de |
    +------+---------+
    

    Then your queries will return:

    1. First query:

      SELECT count(distinct key), country
      from db.t1
      inner join db.t2
      on t1.name=t2.name
      where length>2000
      group by country;
      

      Will yield:

      +---------------------+---------+
      | count(distinct key) | country |
      +---------------------+---------+
      |                   1 |      uk |
      |                   2 |      fr |
      |                   1 |      de |
      +---------------------+---------+
      
    2. Second query:

      SELECT count(distinct key)
      from db.t1
      where Length>2000;
      

      Will yield:

      +---------------------+
      | count(distinct key) |
      +---------------------+
      |                   3 |
      +---------------------+
      

    See it on sqlfiddle.

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

Sidebar

Related Questions

I have two tables named MEMBER - columns id(primary key), name, email & TOPICS
I have two database tables with the following structure: actions: action_id int(11) primary key
I have two tables that I am joining with the following query... select *
I have two tables with the following information : Products Name | ID |
I have two tables with the following (simplified) structures: table "Factors" which holds data
I am using VB.NET with LINQ to MS SQL. I have two following tables.
I have two tables t1 and t2 as following t1 A B C D
I have two tables as shown in the following diagram : as you saw
I have two tables invoices and pending_payments both of which have the following rows
Suppose I have two tables Main and Other like the following: Main +----------+-----------------------------------+ |

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.