Home > Database > SQL > body text

distinct usage in SQL

尊渡假赌尊渡假赌尊渡假赌
Release: 2024-01-26 15:14:14
Original
2984 people have browsed it

DISTINCT in SQL is a keyword used to query unique result sets. It can be used in SELECT statements, COUNT aggregate functions and other statements. The basic syntax is "SELECT DISTINCT column1, column2", where DISTINCT The keyword is placed after the SELECT keyword, followed by the column name or expression to be queried, separated by commas.

distinct usage in SQL

In SQL, DISTINCT is a keyword used to query unique result sets. The DISTINCT keyword can be used in SELECT statements, COUNT aggregate functions and other statements.

The basic syntax of DISTINCT is as follows:

SELECT DISTINCT column1, column2, ...
FROM table_name
WHERE condition;
Copy after login

Among them, the DISTINCT keyword is placed after the SELECT keyword, followed by the column name or expression to be queried, separated by commas. . It means removing duplicates from the query results and retaining only unique records. If the column name is omitted, all columns are queried.

For example, suppose there is the following students table:

+----+--------+-------+
| id | name   | score |
+----+--------+-------+
| 1  | Alice  | 90    |
| 2  | Bob    | 80    |
| 3  | Alice  | 85    |
| 4  | Alice  | 95    |
| 5  | Charlie| 75    |
+----+--------+-------+
Copy after login

If you execute the following SQL statement:

SELECT DISTINCT name FROM students;
Copy after login

, the following results will be returned:

+--------+
| name   |
+--------+
| Alice  |
| Bob    |
| Charlie|
+--------+
Copy after login

This is Because the DISTINCT keyword filters out duplicate values ​​in the name column, only unique values ​​are retained.

It should be noted that the DISTINCT keyword can only be used to query columns, not rows. If you want to query for unique records with multiple columns, you need to specify these column names after the DISTINCT keyword. In addition, the DISTINCT keyword is not part of the SQL standard, so the implementation of different databases may vary slightly.

The above is the detailed content of distinct usage in SQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template