DISTINCT keyword in SQL
The DISTINCT keyword in SQL is used in conjunction with the SELECT statement to remove all duplicate records and retrieve only records.
In SQL, the DISTINCT keyword is used in conjunction with the SELECT statement to remove all duplicate records and retrieve only records in the table.
Syntax to use DISTINCT in SQL
The basic syntax of the DISTINCT keyword to remove duplicate records is as follows:
SELECT DISTINCT cot1, cot2,. cotN
FROM ten_bang
WHERE [dieu_kien]
Example of DISTINCT in SQL
Suppose the NHANVIEN table has the following records:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
First, let's see how the SELECT query returns the duplicate salary record.
SQL> SELECT LUONG FROM NHANVIEN
ORDER BY LUONG;
In the following result, LUONG 2000 appears twice as a duplicate record from the original table.
+----------+ | LUONG | +----------+ | 1500.00 | | 2000.00 | | 2000.00 | | 4500.00 | | 6500.00 | | 8500.00 | | 10000.00 | +----------+
Now, use the DISTINCT keyword with the SELECT query and see the result.
SQL> SELECT DISTINCT LUONG FROM NHANVIEN
ORDER BY LUONG;
+----------+ | LUONG | +----------+ | 1500.00 | | 2000.00 | | 4500.00 | | 6500.00 | | 8500.00 | | 10000.00 | +----------+
Thanks to DISTINCT, the results obtained do not have any duplicate entries.
In the next section, we will learn about sorting results in SQL, remember to follow them.
Previous article: GROUP BY command in SQL
Next lesson: Sort results in SQL
- 12 free keyword analysis tools replace Google Keyword Tool
- How to use Google Keyword Planner
- 12 best keyword analysis tools
- DISTINCT clause in SQL Server
- Google Keyword tool
- Namespace in C #
- How to do keyword research for SEO
- 6 most effective keyword research (Keyword Research)
- How to do and research on Long Tail Keyword in SEO - Part 1
- How to search for any keyword on a Mac
- 7 types of keywords to consider when researching SEO keywords
- Keyword typedef in C
- 5 common mistakes when researching keywords for SEO
- Facebook tests the status search feature by keyword