Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 51

DISTINCT, Alias और ORDER BY

Clean, Predictable Report बनाएँ

DISTINCT duplicate result rows, aliases labels और ORDER BY presentation order control करते हैं। तीनों अलग problem solve करते हैं।

Common learning data: students में Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) और Sana (X-B, 88.50, Active) हैं।
SELECT
  s.full_name AS student_name,
  s.marks AS percentage
FROM students AS s
WHERE s.status = 'Active'
ORDER BY percentage DESC, s.student_id ASC;
Meera | 91.00 Sana | 88.50 Aarav | 86.50

Column और Table Aliases

AS output column को नाम या table को short local name देता है। Storage नहीं बदलता।

SELECT s.full_name AS student_name,
       ROUND(s.marks / 100 * 5, 2) AS score_out_of_5
FROM students AS s;

Reports में clear और joins में short table aliases लें। Select alias ORDER BY में चल सकता, पर WHERE में सामान्यतः नहीं क्योंकि filtering पहले resolve होती है।

ORDER BY with ASC and DESC

SELECT full_name, class_name, marks
FROM students
ORDER BY class_name ASC,
         marks DESC,
         student_id ASC;
Meera | X-A | 91.00 Aarav | X-A | 86.50 Sana | X-B | 88.50 Kabir | X-B | 74.00

पहले class ASC, फिर same class में marks DESC और exact tie में student_id ASC apply होता है।

Deterministic Ties और NULL

Same marks पर केवल ORDER BY marks DESC tied order define नहीं करता। student_id ASC जोड़ें। MySQL ascending में NULL पहले और descending में बाद आता है। Recorded marks explicitly पहले:

ORDER BY marks IS NULL ASC,
         marks DESC,
         student_id ASC;

Boolean expression recorded के लिए 0 और NULL के लिए 1 देता है।

DISTINCT के साथ ORDER BY

SELECT DISTINCT class_name
FROM students
ORDER BY class_name ASC;

Ordering expressions को DISTINCT result से compatible रखें। Complex query में derived table/CTE से needed result select करके outer order साफ लिखें।

गलतियाँ और अभ्यास

  • Alias को schema rename मानना।
  • Observed order पर depend करना।
  • Maintainable code में ORDER BY 2 लिखना।
  • Pagination में tie-breaker भूलना।
  • Bad join छिपाने के लिए DISTINCT जोड़ना।
  1. Active students को marks DESC, ID ASC sort करें।
  2. Five-point score का alias बनाएँ।
  3. Classes uniquely और alphabetically list करें।
  4. Defaults पर depend किए बिना NULL last रखें।

त्वरित सारांश

  • Aliases output label और table reference short करते हैं।
  • ORDER BY order define करता; ASC default और DESC reverse है।
  • Unique tie-breaker deterministic output देता है।
  • DISTINCT और ORDER BY अलग problems solve करते हैं।

Official संदर्भ

References 14 August 2026 को review किए गए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या alias actual column rename करता है?
नहीं। SELECT alias केवल उस query के result को label करता है। Stored schema object rename करने के लिए ALTER TABLE चाहिए।
क्या ORDER BY SELECT alias उपयोग कर सकता है?
हाँ। MySQL output alias को ORDER BY में allow करता है, जो calculated expressions में useful है।
ORDER BY की default direction क्या है?
ASC default है, पर clarity के लिए explicit ASC लिख सकते हैं। DESC order reverse करता है।
Unique tie-breaker क्यों जोड़ें?
Primary sort values tie हों तो relative order deterministic नहीं रहता। student_id जैसा stable unique column जोड़ें।
क्या DISTINCT automatically sort करता है?
नहीं। ORDER BY बिना observed order guaranteed नहीं है। Explicit ORDER BY जोड़ें।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.