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.
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;
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 | +----+--------+-------+
If you execute the following SQL statement:
SELECT DISTINCT name FROM students;
, the following results will be returned:
+--------+ | name | +--------+ | Alice | | Bob | | Charlie| +--------+
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!