How to use SQL select distinct, selectdistinct

Source: Internet
Author: User
Tags how to use sql

How to use SQL select distinct, selectdistinct

Duplicate values may exist in the table. This is not a problem. However, sometimes you may want to list only different values. The keyword distinct is used to return a unique value.

Table:

Example 1

Copy codeThe Code is as follows:
Select distinct name from

The result is as follows:

Example 2
Select distinct name, id from

The result is as follows:

Distinct serves both the name and id. This method is supported by both Access and SQL Server.

Example 3: Statistics

Select count (distinct name) from A; -- number of names deduplicated In the table, which is supported by SQL Server, but not by Access
Select count (distinct name, id) from A; -- neither SQL Server nor Access is supported.

Example 4

Select id, distinct name from A; -- an error is prompted because distinct must start

Others

In the distinct statement, the select field can only be a field specified by distinct, and other fields cannot appear. For example, if Table A has A "Remarks" column, it is impossible to directly use distinct to obtain the distinc name and the corresponding "Remarks" field.
However, you can use other methods to discuss how SQL Server concatenates multiple rows in a column into one row.

Related Article

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: info-contact@alibabacloud.com 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.