I have a table that contains the following columns; a, b, e and another table, table2 that contains c,d. Both tables have a column name.
I made a query:
SELECT distinct a, b
from db.table
where e <>'65';
And I got say, 1885 records
Then, I needed more information from another table (c, d). I used inner join to get these additional information. The query becomes:
SELECT distinct a, b, c, d
from db.table
inner join db.table2
on table.name=table2.name2
where e <>'65';
Now, I was expecting the same result but with additional columns (c, d). But I got a different number of results: 51144. Can any body explain please why the number increased while I just added more columns and did not change the condition ?
DISTINCT returns as the key word says, the distinct results from the select.
The fact that you are adding additional columns to the distinct select will possibly cause the distinct result set to be more rows. Lets have a look at an example
Distinct from
would be
but now adding additional columns to this, let say
will result in
Further more, your inner join can limit the result set, as inner join will only return values that are in both table1 and table2, so it a given value is present in table1, but not in table2 it will not be returned.
Or as was mentioned by @zerkms, if there were 2 keys defining the relationship between the 2 tables, you might get more that you expected.