MOCKSTACKS
EN
Questions And Answers

More Tutorials









MYSQL SELECT

SELECT with DISTINCT


The DISTINCT clause after SELECT eliminates duplicate rows from the result set.

CREATE TABLE `car`
( `car_id` INT UNSIGNED NOT NULL PRIMARY KEY,
 `name` VARCHAR(20),
 `price` DECIMAL(8,2)
);
INSERT INTO CAR (`car_id`, `name`, `price`) VALUES (1, 'Audi A1', '20000');
INSERT INTO CAR (`car_id`, `name`, `price`) VALUES (2, 'Audi A1', '15000');
INSERT INTO CAR (`car_id`, `name`, `price`) VALUES (3, 'Audi A2', '40000');
INSERT INTO CAR (`car_id`, `name`, `price`) VALUES (4, 'Audi A2', '40000');
SELECT DISTINCT `name`, `price` FROM CAR;
+---------+----------+
| name | price |
+---------+----------+
| Audi A1 | 20000.00 |
| Audi A1 | 15000.00 |
| Audi A2 | 40000.00 |
+---------+----------+

DISTINCT works across all columns to deliver the results, not individual columns. The latter is often a misconception of new SQL developers. In short, it is the distinctness at the row-level of the result set that matters, not distinctness at the column-level. To visualize this, look at "Audi A1" in the above result set.

For later versions of MySQL, DISTINCT has implications with its use alongside ORDER BY. The setting for ONLY_FULL_GROUP_BY comes into play as seen in the following MySQL Manual Page entitled MySQL Handling of GROUP BY.

Conclusion

In this page (written and validated by ) you learned about MYSQL SELECT . What's Next? If you are interested in completing MYSQL tutorial, your next topic will be learning about: MYSQL Backticks.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.