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

MySQL uses DISTINCT to filter duplicate data

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.

  1. MySQL DISTINCT keyword example:

    • Description: The DISTINCT keyword is used to retrieve unique values from a specified column or combination of columns in a result set.
    • Example:
      SELECT DISTINCT column_name FROM your_table;
      
  2. How to use DISTINCT in MySQL:

    • Description: Use the DISTINCT keyword in a SELECT statement to filter and return unique values.
    • Example:
      SELECT DISTINCT product_category FROM products;
      
  3. Removing duplicates with DISTINCT in MySQL:

    • Description: DISTINCT eliminates duplicate values from the result set, providing only unique values.
    • Example:
      SELECT DISTINCT employee_department FROM employees;
      
  4. Filtering unique values using DISTINCT in MySQL:

    • Description: Apply DISTINCT to filter out duplicate values and return only unique values.
    • Example:
      SELECT DISTINCT city FROM customers;
      
  5. Distinct values in a specific column in MySQL:

    • Description: Use DISTINCT to retrieve unique values from a specific column in a MySQL table.
    • Example:
      SELECT DISTINCT order_status FROM orders;
      
  6. Applying DISTINCT to multiple columns in MySQL:

    • Description: Combine multiple columns in the DISTINCT clause to retrieve unique combinations of values.
    • Example:
      SELECT DISTINCT first_name, last_name FROM employees;
      
  7. Counting distinct values with MySQL DISTINCT:

    • Description: Use COUNT(DISTINCT column_name) to count the number of unique values in a specific column.
    • Example:
      SELECT COUNT(DISTINCT product_category) AS UniqueCategories FROM products;
      
  8. Examples of using DISTINCT in MySQL queries:

    • Description: Demonstrate various use cases of the DISTINCT keyword in MySQL queries.
    • Examples:
      SELECT DISTINCT customer_type FROM customers;
      SELECT DISTINCT city, country FROM addresses;