It would only display all the rows if each row was unique in the database. The DISTINCT
keyword says "eliminate exact duplicates" in the output set. In your second query, you're selecting City
, then via the *
, all of the columns in the table. Say you had columns City, State, Zip
, then City, *
is the same as City, City, State, Zip
. Now, it would only return the unique combinations of these for all rows in the database. So if you had
Name, City, State, Zip
Joe, Chicago, IL, 60290
Steve, Chicago, IL, 60290
Joe, Chicago, IL, 60290
Joe, Los Angeles, CA, 90012
And selected City, *
you would get
Joe, Chicago, IL, 60290
Steve, Chicago, IL, 60290
Joe, Los Angeles, CA, 90012
with the duplicate row Joe, Chicago, IL, 60290
eliminated.
If you are getting all rows, I suspect it's because you have a unique column (perhaps a primary key) in the table schema that forces each row to be unique.
What you might want instead is a GROUP BY
query, but then you'll need to apply an aggregation operator to the remaining columns to choose which values for them you want.
SELECT State, COUNT(*) as [Customers in State]
FROM Customers
GROUP BY State
That will return a set of distinct states with the count of the number of rows corresponding to that state in the table.