DISTINCT keyword in SQL
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
You should read it
- DISTINCT clause in SQL Server
- How to Get Inauguration Tickets
- Namespace in C #
- How to Identify Pure Sandalwood Powder
- Installing Exchange 2007 (Part I)
- How to Close Windows in Windows 8
- How to Create a Partition
- Elements of the universe
- 3 types of hybrids like myths exist in reality
- Ways to Power Off Nintendo Switch
- What is Apple's Assistive Access feature? How to use?
- Group YouTube channel managers
Maybe you are interested
How to block apps from accessing messages on Windows 11 Why use dynamic DNS instead of static IP address? Age 35 becomes a worry for Chinese technology engineers How to Determine Coordinates on Minecraft GOM Player: Free Media Player for Windows How to remove the Ease of Access button from the login screen in Windows 10