MySQL Select Distinct Clause


The SELECT DISTINCT statement in MySQL is used to return only distinct (unique) values from a column or a combination of columns in a table. This is particularly useful when you want to eliminate duplicate rows from the result set.

Syntax
SELECT DISTINCT column1, column2 FROM table_name ;
Demo Database:
  • Table Name : products
product_id product_name product_code price unit category_id
1 The Psychology 7550 250.00 Piece 1
2 Stories for Children 3098 350.00 Piece 1
3 Harry Potter 8472 275.00 Piece 1
4 Tecno Spark 9468 8000.00 Nos 2
5 Samsung Galaxy 7188 10000.00 Nos 2
6 Panasonic Eluga 3433 7000.00 Nos 2
7 Lenovo IdeaPad 6708 45000.00 Nos 3
8 ASUS Celeron Dual Core 3583 43000.00 Nos 3

1.Distinct Clause with Single Column

To get the list of unique category id.

SELECT DISTINCT category_id FROM products;
Try it Yourself

2.Distinct Clause with multiple columns with GROUP BY

To get the list of unique muliple columns with group by.

SELECT DISTINCT order_no,product_id FROM order_details GROUP BY order_no;
Try it Yourself