Cover Image for MySQL DISTINCT
88 views

MySQL DISTINCT

The MySQL DISTINCT keyword is used to eliminate duplicate rows from the result set of a SELECT query. It ensures that only unique values are returned for the specified columns. This can be particularly useful when you want to retrieve distinct or unique values from one or more columns in a table.

The basic syntax for using DISTINCT in a SELECT query is as follows:

SELECT DISTINCT column1, column2, ...
FROM table_name;

Here’s how it works:

  • column1, column2, and so on are the columns for which you want to retrieve distinct values.
  • table_name is the name of the table from which you want to retrieve the data.

For example, consider a table called products with a column named category. If you want to retrieve a list of distinct categories from this table, you can use the following SQL query:

SELECT DISTINCT category
FROM products;

This query will return a list of unique values in the category column without duplicates.

It’s important to note that DISTINCT considers all the columns specified in the SELECT statement to determine uniqueness. If you select multiple columns, it will ensure that the combination of values in those columns is unique.

Here’s an example with multiple columns:

SELECT DISTINCT column1, column2
FROM table_name;

In this case, the query will return unique combinations of values from column1 and column2.

The DISTINCT keyword is a powerful tool when you need to retrieve unique or distinct values from one or more columns in your database tables. It helps simplify the results of your queries and remove duplicate records.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS