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

DISTINCT, Alias and ORDER BY

Build a Clean, Predictable Report

DISTINCT controls duplicate result rows, aliases improve labels and ORDER BY controls presentation order. Together they make a query easier to consume, but each solves a different problem.

Common learning data: students contains Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) and 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 and Table Aliases

AS names an output column or gives a table a short local name. It does not change storage.

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

Use clear aliases in reports and short table aliases in joins. A select-list alias can be used in ORDER BY, but generally not in WHERE because filtering is resolved before the select list.

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

Deterministic Ties and NULL

If two students share marks, ORDER BY marks DESC does not define which tied row comes first. Add student_id ASC. In ascending MySQL order, NULL sorts before non-NULL; in descending order it sorts after. To place recorded marks before missing marks explicitly:

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

The boolean expression is 0 for recorded values and 1 for NULL, so recorded marks appear first.

DISTINCT with ORDER BY

SELECT DISTINCT class_name
FROM students
ORDER BY class_name ASC;

Keep ordering expressions compatible with the DISTINCT result. When a query becomes complex, select the needed result columns in a derived table or CTE and order the outer result clearly.

Mistakes and Practice

  • Believing aliases change the schema.
  • Relying on default or observed row order.
  • Using column positions such as ORDER BY 2 in maintainable code.
  • Forgetting a tie-breaker in paginated results.
  • Adding DISTINCT to hide a bad join.
  1. Sort active students by marks high-to-low and ID low-to-high.
  2. Create a readable alias for a calculated five-point score.
  3. List classes uniquely and alphabetically.
  4. Place NULL marks last without relying on defaults.

Quick Summary

  • Aliases label query output and shorten table references.
  • ORDER BY defines result order; ASC is default and DESC reverses it.
  • A unique tie-breaker makes output deterministic.
  • DISTINCT and ORDER BY solve uniqueness and ordering separately.

Official References

References reviewed 14 August 2026.

Frequently Asked Questions

Does an alias rename the actual column?
No. A SELECT alias labels the result for that query only. Use ALTER TABLE to rename stored schema objects.
Can ORDER BY use a SELECT alias?
Yes. MySQL allows an output alias in ORDER BY, which is useful for calculated expressions.
What is the default ORDER BY direction?
ASC is the default, but writing ASC explicitly can improve clarity. DESC reverses the ordering.
Why add a unique tie-breaker?
If primary sort values tie, their relative order is otherwise not deterministic. Add a stable unique column such as student_id.
Does DISTINCT automatically sort results?
No. Any observed order without ORDER BY is not guaranteed. Add ORDER BY explicitly.
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.