Back to AGGREGATIONS & GROUP BY, ORDER BY

LIMIT AND ALIASING

Understand LIMIT Clause and aliasing

4 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. Basic LIMIT Usage — The `LIMIT` clause restricts the total number of rows returned by the query.
  2. LIMIT with ORDER BY — Combining `LIMIT` with `ORDER BY` allows retrieval of ranked results, such as the top N records.
  3. LIMIT Offset Syntax — Using two arguments in `LIMIT` allows skipping a number of rows before selecting the specified count.
  4. Column Renaming — Aliasing provides temporary, descriptive names to columns in the output for better readability.
  5. Aliasing in HAVING — Aliases are necessary when referencing the output of an aggregate function within the `HAVING` clause.
PDF notes

Frequently asked questions

Is the `AS` keyword required for aliasing?

No, MySQL allows you to omit `AS`, but including it is strongly recommended for clarity and adherence to SQL standards.

Why can I use the alias in `HAVING` but not `WHERE`?

`WHERE` is processed before the `SELECT` list where the alias is defined, while `HAVING` is processed afterward, allowing alias recognition.

What happens if I use `LIMIT` without `ORDER BY`?

The results will be arbitrary, based on the physical storage order, which is unreliable for ranked data like "Top 10."

How was this lesson?

Your feedback helps us refine explanations and catch bugs.