Can we use "*" with distinct ? If yes how ? If not why ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we use "*" with distinct ? If yes how ? If not why ?

Query about DISTINCT

19th Feb 2020, 6:36 PM
Nishant Joshi
Nishant Joshi - avatar
3 Answers
+ 3
This should work for the most part. It might depend on your database. For instance, MS SQL Server will throw an error if any column is a TEXT datatype. The reason is because text datatypes are not comparable in the way varchars and numbers are. This doesn't seem to be a problem in MySQL. I'm assuming you're asking because you ran into a problem. If so, what errors are you receiving?
19th Feb 2020, 7:32 PM
David Carroll
David Carroll - avatar
+ 2
SELECT DISTINCT * FROM MyTable This can be used if MyTable has duplicate records and you wish to eliminate the duplicates. You can select INTO another table if you wish to purge the duplicates. Afterward, DROP the original table and rename the new one to the original name. There are more uses for SELECT DISTINCT *, such as eliminating duplicates that can occur from table or view JOINs, or SELECTs from multiple tables.
20th Feb 2020, 7:23 AM
Brian
Brian - avatar
+ 1
The most common use of * is to find the total number of records in a table. For eg- SELECT COUNT(*) FROM table_name; I personally have not come across a query which makes use of both * and distinct together. Let us see what others have to say.
19th Feb 2020, 6:52 PM
Avinesh
Avinesh - avatar