• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

MySQL Tip: Use DISTINCT keyword to return only distinct (unique) values.

December 20, 2023

MySQL Tip: Use DISTINCT keyword to return only distinct (unique) values

When working with databases, it is common to encounter situations where you need to retrieve only unique values from a specific column. In MySQL, the DISTINCT keyword comes in handy for this purpose. By using the DISTINCT keyword in your queries, you can eliminate duplicate values and obtain a result set with only distinct (unique) values.

Understanding the DISTINCT keyword

The DISTINCT keyword is used in conjunction with the SELECT statement to retrieve unique values from a column or a combination of columns in a table. It instructs the database engine to eliminate duplicate rows from the result set, returning only distinct values.

Let’s consider an example to better understand how the DISTINCT keyword works. Suppose we have a table named “customers” with the following data:

+----+----------+
| ID |  Name    |
+----+----------+
| 1  | John     |
| 2  | Mary     |
| 3  | John     |
| 4  | David    |
| 5  | Mary     |
+----+----------+

If we want to retrieve only the distinct names from the “Name” column, we can use the following query:

SELECT DISTINCT Name FROM customers;

The result of this query would be:

+----------+
|  Name    |
+----------+
| John     |
| Mary     |
| David    |
+----------+

As you can see, the duplicate names “John” and “Mary” have been eliminated, and only the distinct values are returned.

Using DISTINCT with multiple columns

The DISTINCT keyword can also be used with multiple columns to retrieve unique combinations of values. Let’s consider another example using a table named “orders” with the following data:

+----+----------+---------+
| ID |  Product |  Price  |
+----+----------+---------+
| 1  | Apple    |  1.99   |
| 2  | Orange   |  0.99   |
| 3  | Apple    |  1.99   |
| 4  | Banana   |  0.50   |
| 5  | Orange   |  0.99   |
+----+----------+---------+

If we want to retrieve only the distinct combinations of “Product” and “Price” from the “orders” table, we can use the following query:

SELECT DISTINCT Product, Price FROM orders;

The result of this query would be:

+----------+---------+
|  Product |  Price  |
+----------+---------+
| Apple    |  1.99   |
| Orange   |  0.99   |
| Banana   |  0.50   |
+----------+---------+

Again, the duplicate combinations of “Product” and “Price” have been eliminated, and only the distinct values are returned.

Conclusion

The DISTINCT keyword in MySQL is a powerful tool for retrieving only distinct (unique) values from one or more columns in a table. By using the DISTINCT keyword in your queries, you can eliminate duplicate rows and obtain a result set with only the unique values you need.

For more information on MySQL and VPS hosting solutions, visit Server.HK.

Recent Posts

  • Hong Kong VPS Security Checklist: 10 Steps to Harden Your Server in 2026
  • NVMe SSD vs SATA SSD for VPS Hosting: Does Storage Type Really Matter?
  • Hong Kong VPS Docker Setup: Run Containers with Full Root Access
  • How to Set Up a Game Server on Hong Kong VPS: Low-Latency Gaming for Asia
  • How to Deploy a Node.js Application on Hong Kong VPS: Complete Guide

Recent Comments

  1. dapoxetine in usa on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  2. tadalafil tablets on Hong Kong VPS vs Singapore VPS: Which Is Better for Your Asia Business in 2026?
  3. ivermectina tabletas on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  4. hello world on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot