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.
Unless
keyisUNIQUE(in which case, why bother with theDISTINCTkeywords?), there is no reason that your two queries should return the same results.Suppose
t1contains:And
t2contains:Then your queries will return:
First query:
Will yield:
Second query:
Will yield:
See it on sqlfiddle.