5. ORDER BY, DISTINCT and Top-N Results
5.3 How to Remove Duplicate Values with DISTINCT
Why. Duplicate city names clutter a “list of cities” answer.
What. DISTINCT removes duplicate result rows for the selected columns.
How. SELECT DISTINCT city FROM orders ORDER BY city;
Steps
- List cities without DISTINCT — see repeats.
- Add DISTINCT.
- Count how many unique cities.
Examples
Example 1 · dataset: raja_coffee
SELECT DISTINCT city FROM orders ORDER BY city;Output
| city |
|---|
| Goa |
| Kolhapur |
| Mumbai |
| Nashik |
| Pune |
| Solapur |
Meaning. Each city once.
Ravindra Bagale's Tip
DISTINCT applies to the whole selected row, not one column in isolation when you select many columns.
Ravindra Bagale's Tip – मराठी
अनेक कॉलम SELECT केल्यावर DISTINCT संपूर्ण निवडलेल्या रो वर लागू होतो.
Ravindra Bagale's Tip – हिंदी
कई कॉलम SELECT पर DISTINCT पूरी चुनी पंक्ति पर लगता है।