Application of the distinct function in SQL

Source: Internet
Author: User
Tags table name

A large amount of data has been collected on a site, and about 100,000 of the data has been collected on a site in the same industry. Of course, the title has not been changed, or the original title. This is to facilitate the filtering of duplicate data. These hundreds of thousands of data records contain a large proportion of duplicate data, which was originally intended to be deleted, however, the summer blog failed to find a good method, and finally thought of the distinct parameter in SQL. This parameter has not been used for a long time. Its role is to filter out duplicate data and not display it, it seems that it is most appropriate to use it in this collection site. Next we will focus on the application of distinct in SQL.

Role of distinct:

In a data table, many values are often repeated. For example, if we only want to output a "summer Blog", we can use distinct, with distinct, only one "summer blog" can be displayed ".
Distinct syntax:

Because the output value is required, SELECT is required. The syntax is as follows:

Select distinct column name FROM table name
Example 1 of distinct:

To more intuitively see the effects of distinct, let's take a look at the effects before and after distinct. For example, we have the following Orders table:

If you do not use distinct to read data, the SQL statement is the "SELECT Company FROM Orders" statement, and the result is as follows:

We can see that the duplicate value W3School is displayed twice. If the content is repeated, it can be displayed only once, next, let's take a look at the value output after distinct. Let's take a look at the SQL statement after distinct is used, as shown below:
Select distinct Company FROM Orders
The result format after running is:

Currently, W3School is only displayed once.

Example 2 of distinct:

A simple small instance, if we have the following table:

Use distinct in SQL to remove duplicate data. The effect after running is as follows:

Example 3 of distinct:

Next, let's change the SQL statement slightly, not only remove the duplicate values in the table name, but also remove the ids. The SQL statement is as follows:

Select distinct name, id from

The following figure shows the effect after execution:

We can see that the repeated values in the name field and id field have been removed from the execution result.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.