1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
Course Articles
  • Everywhere
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Courses
  3. MySQL Guides

Guide to MySQL DISTINCT Clause

  • ScientificTools.org
  • November 30, 2023 at 12:31 PM
  • 1,264 times read
Contents [hideshow]
  1. Examples of MySQL DISTINCT Clause
    1. Example 1: Selecting Unique Values from a Single Column
    2. Example 2: Removing Duplicates with DISTINCT
    3. Example 3: DISTINCT with Multiple Columns
    4. Handling NULL Values with DISTINCT
    5. DISTINCT with NULL Example
  2. Summary

MySQL DISTINCT is a keyword used in SQL queries, specifically in SELECTstatements, to eliminate duplicate rows from the result set. It ensures that the query returns only unique rows by removing duplicates. This can be particularly useful when you have multiple records that are identical in one or several columns and you want to retrieve only distinct combinations.

Here's a brief explanation of how it works:

  • When you use DISTINCT, it applies to all columns that are listed after the SELECT keyword.
  • If you specify multiple columns, DISTINCT will return unique combinations of values in these columns. That means a row is only considered a duplicate if all the column values listed are identical to those in another row.
  • DISTINCT is often used in queries where data normalization might lead to duplication in the result set, and you're interested in getting a list of unique values.

For example, if you have a database of books and you execute the query SELECT DISTINCT author FROM books;, it will return a list of authors, with each author listed only once, regardless of how many books they have in the database.

In summary, DISTINCT is a powerful tool in SQL for ensuring that the data returned in your query is unique across the specified columns.

Here's the basic syntax:

SQL
SELECT DISTINCT column_names
FROM table_name
WHERE condition
ORDER BY column;

In this syntax:

  • DISTINCT: This keyword ensures that the query returns only distinct (different) values.
  • column_names: These are the columns you want to retrieve.
  • table_name: The name of the table from where you're retrieving the data.
  • condition: Optional. It filters the records.
  • ORDER BY: Optional. It sorts the result set.

Examples of MySQL DISTINCT Clause

Let's consider a table named books with columns author, title, and genre.

Example 1: Selecting Unique Values from a Single Column

Suppose we want to list all unique genres:

SQL
SELECT DISTINCT genre
FROM books
ORDER BY genre;

Output Example:

Code
+---------+
| genre   |
+---------+
| Fiction |
| Mystery |
| Sci-Fi  |
| Fantasy |
...

Example 2: Removing Duplicates with DISTINCT

Without DISTINCT, if we select genres:

SQL
SELECT genre
FROM books
ORDER BY genre;

You might get duplicates:

Code
+---------+
| genre   |
+---------+
| Fiction |
| Fiction |
| Mystery |
| Sci-Fi  |
| Sci-Fi  |
...

Example 3: DISTINCT with Multiple Columns

To get unique combinations of author and genre:

SQL
SELECT DISTINCT    author, genre
FROM    books
ORDER BY     author,     genre;

Output Example:

Code
+---------------+---------+
| author        | genre   |
+---------------+---------+
| John Doe      | Fiction |
| John Doe      | Mystery |
| Jane Smith    | Sci-Fi  |
| Jane Smith    | Fantasy |
...

Handling NULL Values with DISTINCT

If a column has NULL values, DISTINCT will consider all NULLs as equal and return only one NULL.

DISTINCT with NULL Example

If genre has NULL values:

SQL
SELECT DISTINCT genre
FROM books;

Output Example:

Code
+---------+
| genre   |
+---------+
| NULL    |
| Fiction |
| Mystery |
...

Summary

  • Use DISTINCT in the SELECT statement to remove duplicate rows.
  • Works with single or multiple columns.
  • Treats NULL values as identical, returning only one NULL.
  • Previous Article MySQL SELECT FROM Statement Guide
  • Next Article Guide to MySQL AND Operator

Categories

  • MySQL

Archive

  1. 2023 (33)
    1. November (27)
      • Guide to MySQL IN and NOT IN Operators
      • Guide to MySQL OR Operator
      • Guide to MySQL AND Operator
      • Guide to MySQL DISTINCT Clause
      • MySQL SELECT FROM Statement Guide
      • What is EXPLAIN statement in MySQL and what it does ?
      • Guide to Query Caching in MySQL
      • How to Optimize MySQL Queries?
      • Guide to Entity-Relationship (ER) Diagrams in MySQL with Examples
      • What is Normal Forms and Normalization in MySQL
      • How to design a MySQL database with high performance
      • Principals of Secure Database Design in MySQL
      • How to secure and harden MySQL
      • User Management MySQL: Guide to Create, Manage and Permissions
      • Guide to Views in MySQL: Creating Updating Deleting
      • Guide to Stored Procedures and Functions in MySQL
      • Guide to Indexes in MySQL: Creating and Managing Indexes
      • Advanced Filtering in MySQL guide and examples
      • Practical guide for UNION and UNION ALL in MySQL with examples
      • Joining Tables in MySQL guide with examples
      • Guide to GROUP BY and HAVING clauses in MySQL with examples
      • MySQL ORDER BY Clause guide with practical examples
      • Complete MySQL WHERE Clause guide with examples
      • What is a MySQL subquery and how are subqueries executed in MySQL?
      • How to do queries in MySQL
      • How to create, modify, rename and delete tables using MySQL
      • MySQL CRUD Operations (Create, Read, Update, Delete)
    2. October (2)
    3. August (2)
    4. July (2)
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2026. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™