TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

MySQL

MySQL SELECT DISTINCT clause

Overview

In this tutorial, we will learn MySQL SELECT DISTINCT clause with examples.We can use DISTINCT clause remove duplicates to produce the SELECT query result set in which every row is unique. If a query returns a result that contains duplicate rows so that the result set contains only unique rows.

Syntax

The simple DISTINCT clause SELECT statement has the following syntax:

mysql>SELECT DISTINCT column_list

            FROM <table_name>;

We can use this clause after the SELECT but before the column list. When processing the SELECT statement with DISTINCT clause MySQL will compare whole rows.

Examples

In this example, we will query the continents from the Country table from the world MySQL database. To know the table definition and the columns use the following query:

mysql> DESC Country;

To know the number of rows in the table:

mysql> SELECT COUNT(*) FROM Country;
+———-+
| COUNT(*) |
+———-+
| 239 |
+———-+
1 row in set (0.04 sec)

 

MySQL World Country Table Row Count

Run the following query to get the Continents.

mysql>SELECT Continent FROM Country;

Notice that the query returns duplicate rows in the result set. Now, run the following query with DISTINCT clause. Add the DISTINCT clause after the SELECT keyword in the query:

mysql> SELECT DISTINCT Continent FROM Country;
+—————+
| Continent |
+—————+
| North America |
| Asia |
| Africa |
| Europe |
| South America |
| Oceania |
| Antarctica |
+—————+
7 rows in set (0.02 sec)

Run the query without the DISTINCT clause and notice that the query returns duplicate rows. When we add DISTINCT clause to the statement, it removes the duplicates and returns only the unique rows.

 

MySQL SELECT DISTINCT clause

Note that DISTINCT clause treats all NULL values for the given column as having the same value.

—

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Related Posts

MySQL Workbench Windows 11

MySQL /

New MySQL Connection in Workbench

MySQL Command-line Client

MySQL /

Command-Line MySQL Client

Start MySQL Client

MySQL /

Start MySQL Client on Windows 11

Windows 11 Services Run Prompt

MySQL /

Start MySQL Server on Windows 11

MySQL Download Windows 11

MySQL /

Download & Install MySQL on Windows 11

‹ MySQL Import using SQL file› MySQL SELECT WHERE Clause

Recent Posts

  • Flowchart Control structures
  • Flowgorithm Layout Windows
  • Draw a Square using Turtle Graphics
  • Flowgorithm File Errors
  • Write to Text File using Flowgorithm
  • Read Text File using Flowgorithm
  • Flowgorithm Turtle Graphics Flowchart
  • Update Flowgorithm to Latest Version
  • Flowgorithm File Symbols
  • Flowgorithm Turtle Graphics

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version