๐ Phase 3: SQL for Data Science
๐ Topic 4 โ LIMIT
LIMIT is used to control how many rows a SQL query returns.
It is especially useful when you want to see only the first few records, such as:
โข Top 5 highest salaries
โข Top 10 products by sales
โข First 20 customers
โข Latest 10 transactions
๐น Basic Syntax
SELECT *
FROM employees
LIMIT 5;
This returns only 5 rows from the result.
๐น LIMIT with ORDER BY
This is where LIMIT becomes especially powerful.
Suppose you want the 5 highest-paid employees:
SELECT employee_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5;
The logical idea is:
Filter โ Sort โ Take required number of rows
For example:
Salary
120000
110000
95000
90000
85000
Only the top 5 rows are returned.
๐น LIMIT Without ORDER BY
You can write:
SELECT *
FROM employees
LIMIT 5;
But without ORDER BY, you should not assume these are the "first" or "top" 5 meaningful records.
The database does not guarantee a particular order unless you explicitly specify one.
So for analysis, this is usually better:
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 5;
๐น LIMIT with WHERE
You can combine WHERE, ORDER BY, and LIMIT.
Example:
SELECT employee_name, salary
FROM employees
WHERE department = 'IT'
ORDER BY salary DESC
LIMIT 3;
This finds the 3 highest-paid employees in the IT department.
๐น LIMIT for Top-N Analysis
Top-N queries are extremely common in data analysis.
Top 10 products by sales
SELECT product_name, sales
FROM products
ORDER BY sales DESC
LIMIT 10;
5 most recent transactions
SELECT transaction_id, transaction_date, amount
FROM transactions
ORDER BY transaction_date DESC
LIMIT 5;
3 cheapest products
SELECT product_name, price
FROM products
ORDER BY price ASC
LIMIT 3;
๐น LIMIT with OFFSET
OFFSET allows you to skip a certain number of rows before returning results.
Example:
SELECT *
FROM employees
LIMIT 5 OFFSET 10;
This means:
Skip 10 rows
โ
Return the next 5 rows
This is commonly used for pagination.
For example:
โข Page 1 โ
LIMIT 10 OFFSET 0โข Page 2 โ
LIMIT 10 OFFSET 10โข Page 3 โ
LIMIT 10 OFFSET 20Syntax can vary between SQL databases, so always check the specific database you are using.
๐น LIMIT vs TOP vs FETCH
Different SQL databases may use different syntax.
MySQL/PostgreSQL/SQLite commonly use:
LIMIT 5;SQL Server commonly uses:
SELECT TOP 5 *
FROM employees;
Standard SQL also supports:
FETCH FIRST 5 ROWS ONLY;The concept is the same: restrict the number of returned rows.
๐น Real-World Data Science Example
Suppose an analyst wants to investigate the 10 largest customer transactions.
SELECT customer_id,
transaction_id,
amount
FROM transactions
ORDER BY amount DESC
LIMIT 10;
This can help identify:
โข High-value transactions
โข Potential anomalies
โข Important customers
โข Transactions requiring further investigation
๐น Common Mistakes
Mistake 1 โ Using LIMIT without sorting
SELECT *
FROM products
LIMIT 10;
This does not necessarily mean "10 best products."
For that:
ORDER BY sales DESC
LIMIT 10;
Mistake 2 โ Using ASC when you need the highest values
Wrong:
ORDER BY salary ASC
LIMIT 5;
This returns the lowest salaries.
For the highest salaries: