Global Insight Media.

Your daily source of verified news and insightful analysis

current events

What is distinct in MySQL

By Daniel Johnston

Description. The MySQL DISTINCT clause is used to remove duplicates from the result set. The DISTINCT clause can only be used with SELECT statements.

What is distinct in SQL with examples?

The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

Why we use distinct in SQL?

SQL DISTINCT clause is used to remove the duplicates columns from the result set. The distinct keyword is used with select keyword in conjunction. It is helpful when we avoid duplicate values present in the specific columns/tables. The unique values are fetched when we use the distinct keyword.

What is difference between unique and distinct in SQL?

Unique and Distinct are two SQL constraints. The main difference between Unique and Distinct in SQL is that Unique helps to ensure that all the values in a column are different while Distinct helps to remove all the duplicate records when retrieving the records from a table.

What does distinct mean in database?

The distinct keyword is used with SELECT statement to retrieve unique values from the table. Distinct removes all the duplicate records while retrieving records from any table in the database.

Does distinct include NULL?

The DISTINCT clause counts only those columns having distinct (unique) values. … COUNT DISTINCT does not count NULL as a distinct value.

What is unique SQL?

The UNIQUE constraint in SQL is used to ensure that no duplicate values will be inserted into a specific column or combination of columns that are participating in the UNIQUE constraint and not part of the PRIMARY KEY.

Is distinct same as unique?

The main difference between unique and distinct is that UNIQUE is a constraint that is used on the input of data and ensures data integrity. While DISTINCT keyword is used when we want to query our results or in other words, output the data.

Is distinct or GROUP BY better?

Group by is expensive than Distinct since Group by does a sort on the result while distinct avoids it. But if you want to make group by yield the same result as distinct give order by null ..

Why distinct is bad in SQL?

Well, improper use of “distinct” not only hides the real problem (duplicate entries in the tables, lack of condition in the on clause) as discussed above but also degrades query performance. … This will bring the IO Cost (logical reads) of the query goes up significantly.

Article first time published on

Should I use distinct?

If you’re querying a table that is expected to have repeated values of some field or combination of fields, and you’re reporting a list of the values or combinations of values (and not performing any aggregations on them), then DISTINCT is the most sensible thing to use.

Can distinct be used on multiple columns?

Answer. Yes, the DISTINCT clause can be applied to any valid SELECT query. It is important to note that DISTINCT will filter out all rows that are not unique in terms of all selected columns.

How distinct will work in SQL?

When only one expression is provided in the DISTINCT clause, the query will return the unique values for that expression. When more than one expression is provided in the DISTINCT clause, the query will retrieve unique combinations for the expressions listed. In SQL, the DISTINCT clause doesn’t ignore NULL values.

What is the use of distinct keyword?

The DISTINCT keyword is used to fetch distinct records from a database table. The DISTINCT clause is basically used to remove duplicates from the result set of a SELECT statement and only selects DIFFERENT values.

What does the word distinct?

distinct, separate, discrete mean not being each and every one the same. distinct indicates that something is distinguished by the mind or eye as being apart or different from others. two distinct versions separate often stresses lack of connection or a difference in identity between two things.

How can I see unique values in MySQL?

MySQL – Distinct Values To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.

What is the difference between primary and unique key?

Key Differences Between Primary key and Unique key: Primary key will not accept NULL values whereas Unique key can accept NULL values. A table can have only one primary key whereas there can be multiple unique key on a table.

How do I create a unique key in MySQL?

The syntax for creating a unique constraint using an ALTER TABLE statement in MySQL is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, … column_n); table_name.

Is a unique key?

A unique key is a group of one or more than one fields or columns of a table which uniquely identify database record. A unique key is the same as a primary key, but it can accept one null value for a table column. It also cannot contain identical values.

How do I use distinct in one column in SQL?

Adding the DISTINCT keyword to a SELECT query causes it to return only unique values for the specified column list so that duplicate rows are removed from the result set. Since DISTINCT operates on all of the fields in SELECT’s column list, it can’t be applied to an individual field that are part of a larger group.

How do I count distinct rows in SQL?

Syntax. SELECT COUNT(DISTINCT column) FROM table; This statement would count all the unique entries of the attribute column in the table . DISTINCT ensures that repeated entries are only counted once.

Can distinct be used in where clause?

By using the WHERE clause with a DISTINCT clause in MySQL queries, we are putting a condition on the basis of which MySQL returns the unique rows of the result set.

Is distinct costly in SQL?

In a table with million records, SQL Count Distinct might cause performance issues because a distinct count operator is a costly operator in the actual execution plan.

Is distinct faster than GROUP BY?

DISTINCT is used to filter unique records out of all records in the table. It removes the duplicate rows. SELECT DISTINCT will always be the same, or faster than a GROUP BY.

Does distinct reduce performance?

Yes, as using DISTINCT will (sometimes according to a comment) cause results to be ordered. Sorting hundreds of records takes time. Try GROUP BY all your columns, it can sometimes lead the query optimiser to choose a more efficient algorithm (at least with Oracle I noticed significant performance gain).

What is difference between distinct and group by?

GROUP BY lets you use aggregate functions, like AVG , MAX , MIN , SUM , and COUNT . On the other hand DISTINCT just removes duplicates. This will give you one row per department, containing the department name and the sum of all of the amount values in all rows for that department.

What is difference between distinct?

Distinct and different are similar words, but they are not always used the same way. Distinct means “different in a way that you can see, hear, smell, feel, etc.” or “noticeably different.” Different means “not of the same kind” or “partly or totally unlike.”

Is unique a query?

SQL SELECT query with DISTINCT/UNIQUE keyword retrieves only unique data from the column ‘Age’. There are only 2 distinct values ’25’ and ’27’ available in the table. Whereas, SQL SELECT query fetches all 4 records from the table if we do not use DISTINCT/UNIQUE keywords.

Is SELECT distinct faster than SELECT?

Most of the SELECT DISTINCT queries will perform exactly as fast as their simple SELECT counterparts, because the optimizer will do away with the step necessary for eliminating duplicates.

Is SELECT distinct bad practice?

As a general rule, SELECT DISTINCT incurs a fair amount of overhead for the query. Hence, you should avoid it or use it sparingly. The idea of generating duplicate rows using JOIN just to remove them with SELECT DISTINCT is rather reminiscent of Sisyphus pushing a rock up a hill, only to have it roll back down again.

Can we use distinct in SQL Server?

The SQL Server (Transact-SQL) DISTINCT clause is used to remove duplicates from the result set. The DISTINCT clause can only be used with SELECT statements.