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
May be interested
- How to do and research on Long Tail Keyword in SEO - Part 1it can be said that this long tail keyword - long tail keyword has few users and attention in seo, but has very unexpected results if you search specifically in the search engine.
- How to search for any keyword on a Macthis article will show you how to search for any word or keyword phrase in almost any program or application on your mac.
- 7 types of keywords to consider when researching SEO keywordskeywords in seo are an important factor for any search engine. choosing the right keywords for optimization is essential and ensures the success of every seo strategy.
- Keyword typedef in Cthe c program language provides a typedef keyword, which you can use to provide the type for a new name. here is an example to define a byte entry for 1-byte numbers (like unsigned char).
- 5 common mistakes when researching keywords for SEOavoiding these errors can save you time, rethink your marketing strategy and bring the right audience to your website.
- Facebook tests the status search feature by keywordwith this feature you can quickly find the phone number or restaurant name that friends posted a few months ago on facebook with a simple search keyword.
- What is Search volume? Why care about search volume?search volume (keyword search volume) refers to the number of searches for a certain keyword within a selected time frame (usually 1 month). in most keyword research tools, the number of searches is an average value based on the past 12 months.
- 47 steps to increase keyword rankings and website traffic effectively and sustainably over timethe problem most people have is not about how they can set up a website or even start a blog, it's about how they can actually drive traffic to their website.
- Basic C # syntaxc # is an object-oriented programming language. in an object-oriented programming method, a program of diverse objects that interacts with the ways of action. actions that an object can receive are called methods.
- 8 ways to do SEO to increase website trafficwith seo people, when the number of visitors to the website is increasing, it will have a big impact on whether it can sustain the keyword ranking in search results. therefore, how to increase website traffic is essential.