This lesson on LIMIT AND ALIASING is hands-on and example-driven. You will learn how to restrict the number of rows returned using LIMIT, including applying offsets. You will also master column aliasing using the AS keyword to rename aggregate function outputs, enabling cleaner use in clauses like HAVING.
What You'll Be Able To Do
- Restrict the number of rows returned by a query using LIMIT.
- Identify the N largest or smallest values using LIMIT combined with ORDER BY.
- Apply an offset parameter to skip a specified number of initial rows.
- Rename a calculated column output using the AS keyword.
- Reference an aliased aggregate column within a HAVING clause.
Topics Covered in LIMIT AND ALIASING
- LIMIT basic function (0:00 - 0:15) — The LIMIT clause specifies how many rows should be returned in the output.
- Basic LIMIT demonstration (0:15 - 0:30) — Running a query with LIMIT 3 returns only the first three rows of the result set.
- LIMIT with ORDER BY (0:30 - 0:55) — Combining LIMIT with ORDER BY DESC allows finding the top N values, such as the three oldest employees.
- LIMIT offset parameter (0:55 - 1:40) — The syntax LIMIT offset, count allows skipping the first 'offset' rows before returning 'count' rows.
- Introduction to Aliasing (1:40 - 2:00) — Aliasing is a way to temporarily change the name of a column in the output.
- Aliasing necessity (2:00 - 2:45) — When using aggregate functions, the resulting column name is often complex, making it difficult to reference in clauses like HAVING.
- Using AS for Aliasing (2:45 - 3:30) — The AS keyword is used to rename an aggregate column, allowing the new name to be used cleanly in the HAVING clause.
- AS keyword optionality (3:30 - 3:50) — The AS keyword is not strictly necessary for aliasing, as it is often implied by the SQL engine.
SQL Cheat Sheet
-
LIMIT N— Restricts the result set to the first N rowsSELECT * FROM employee_demographics LIMIT 3; -
LIMIT offset, count— Skips 'offset' rows, then returns 'count' rowsSELECT * FROM employee_demographics LIMIT 2, 1; -
ORDER BY <col> DESC LIMIT N— Finds the top N values based on the columnSELECT * FROM employee_demographics ORDER BY age DESC LIMIT 3; -
AVG(col) AS alias_name— Assigns a temporary name to a calculated columnSELECT gender, AVG(age) AS average_age FROM employee_demographics GROUP BY gender; -
HAVING alias_name > value— Filters grouped results using the aliased nameSELECT gender, AVG(age) AS average_age FROM employee_demographics GROUP BY gender HAVING average_age > 40;
Comparison Table
| LIMIT Usage | Syntax Pattern | Effect |
|---|---|---|
| Basic Restriction | LIMIT N | Returns the first N rows. |
| Offset Restriction | LIMIT offset, count | Skips 'offset' rows, returns 'count' rows. |
| Top N Filtering | ORDER BY col DESC LIMIT N | Returns N rows with highest values. |
Common Pitfalls
- Mistake: Forgetting LIMIT is applied after ORDER BY. Avoid: Always place ORDER BY before LIMIT to ensure you get the correct top/bottom N rows.
- Mistake: Assuming the LIMIT offset parameter starts counting at 1. Avoid: Remember the offset parameter is zero-indexed (0 is the first row).
- Mistake: Trying to use the alias in the WHERE clause. Avoid: Aliases are defined after WHERE; use the original function or column name.
FAQs
- Is the AS keyword required for aliasing? No, AS is optional and often implied by the SQL engine. However, using AS is recommended for improved query readability and clarity.
- Can I use LIMIT without ORDER BY? Yes, but the rows returned are arbitrary unless the table has a defined primary key order. Always use ORDER BY if you need specific rows (e.g., oldest, newest).
- Where does LIMIT fit into the standard SQL query structure? LIMIT is almost always the very last clause executed in a SELECT statement, after filtering (WHERE), grouping (GROUP BY), and ordering (ORDER BY).