Often when using SQL to select information from tables, the user receives redundant data consisting in the presence of absolutely identical repeating rows. To eliminate this situation, use the SQL distinct argument in the Select clause. This article will discuss examples of the use of this argument, as well as situations in which it is better to refuse to use the argument.
Before we begin to consider specific examples, we will create a couple of necessary tables in the database.
Table preparation
, , . Oboi () id ( ), type ( – , .), color (), struct () price (). Ostatki () id_oboi ( Oboi) count ( ).
. 9 :
Oboi |
id | type | color | struct | price |
1 | | | | 56,9 |
2 | | | | 114,8 |
3 | | | | 504 |
4 | | | | 1020,9 |
5 | | | | 150,6 |
6 | | | | 95,4 |
7 | | | | 372 |
8 | | | | 980,1 |
9 | | | | 1166,5 |
– :
Ostatki |
id_oboi | count |
1 | 8 |
2 | 12 |
3 | 24 |
4 | 9 |
5 | 16 |
6 | 7 |
7 | 24 |
8 | 32 |
9 | 11 |
distinct SQL.
distinct Select
distinct Select . , Select, . , SQL «select distinct». distinct , .
, :
SELECT distinct Ostatki.Count, distinct Oboi.* FROM Oboi INNER JOIN Ostatki ON Oboi.id = Ostatki.id_oboi |
, , . .
distinct
, , , . «Select distinct *» .
, , , :
SELECT Oboi.type FROM Oboi order by type |
:
, . Select distinct:
SELECT distinct Oboi.type FROM Oboi order by type |
:
, , , , . , , .
distinct
SQL distinct . Min Max , , .
, , , , :
SELECT sum(Ostatki.count) FROM Ostatki |
143. :
SELECT sum(distinct Ostatki.count) FROM Ostatki |
119, 3 7 . , .
SQL distinct Count. , , :
SELECT count(distinct Oboi.type) FROM Oboi |
5 – , , . : « 20 !», , , .
, Count distinct, . , distinct Select' .
SQL distinct :
- . , ( ).
- . .
, , , – . distinct:
SELECT distinct Oboi.type, Oboi.color FROM Oboi ORDER BY Oboi.type |
– :
, ( ) , ( distinct):
, , distinct .
distinct
distinct – All. . , , All – , .
We hope you now understand when distinct (SQL) is applied. The description gave you complete information about the appropriateness of applying this argument in solving various problems. Indeed, as it turned out, even such a simple argument in its application hides the quite tangible probability of losing some data and displaying inaccurate information.