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

DataGrip Desktop Shortcut

MySQL /

Launch DataGrip on Windows

DataGrip Download

MySQL /

Install DataGrip IDE on Windows 11

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

‹ MySQL Import using SQL file› MySQL SELECT WHERE Clause

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version