MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The DISTINCT
keyword in MySQL is used to return unique (distinct) values in the result set of a SELECT
statement. It eliminates all the duplicate records and fetches only the unique ones.
Here's a basic usage of the DISTINCT
keyword:
SELECT DISTINCT column_name FROM table_name;
For example, consider a table called employees
with a column department_id
. If you want to get a list of all unique departments in which employees work, you could use the DISTINCT
keyword like this:
SELECT DISTINCT department_id FROM employees;
This query will return a list of unique department_id
values from the employees
table.
If you want to select distinct values across multiple columns, you can include all the column names in your SELECT
statement. For example:
SELECT DISTINCT department_id, job_title FROM employees;
This query will return all unique combinations of department_id
and job_title
.
Important Note: When used with multiple columns, the DISTINCT
keyword will consider the combination of all the columns when determining the uniqueness of a record. For example, in the above query, a department with the same department_id
but different job_title
would be considered a unique combination and included in the result set.
Also, NULL
is considered a distinct value. If a column has NULL
values, each NULL
is considered distinct from the others, so a SELECT DISTINCT
query on that column would return one NULL
.
MySQL DISTINCT keyword example:
DISTINCT
keyword is used to retrieve unique values from a specified column or combination of columns in a result set.SELECT DISTINCT column_name FROM your_table;
How to use DISTINCT in MySQL:
DISTINCT
keyword in a SELECT
statement to filter and return unique values.SELECT DISTINCT product_category FROM products;
Removing duplicates with DISTINCT in MySQL:
DISTINCT
eliminates duplicate values from the result set, providing only unique values.SELECT DISTINCT employee_department FROM employees;
Filtering unique values using DISTINCT in MySQL:
DISTINCT
to filter out duplicate values and return only unique values.SELECT DISTINCT city FROM customers;
Distinct values in a specific column in MySQL:
DISTINCT
to retrieve unique values from a specific column in a MySQL table.SELECT DISTINCT order_status FROM orders;
Applying DISTINCT to multiple columns in MySQL:
DISTINCT
clause to retrieve unique combinations of values.SELECT DISTINCT first_name, last_name FROM employees;
Counting distinct values with MySQL DISTINCT:
COUNT(DISTINCT column_name)
to count the number of unique values in a specific column.SELECT COUNT(DISTINCT product_category) AS UniqueCategories FROM products;
Examples of using DISTINCT in MySQL queries:
DISTINCT
keyword in MySQL queries.SELECT DISTINCT customer_type FROM customers; SELECT DISTINCT city, country FROM addresses;