TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4652 208
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“ 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 20

Syntax 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:
More from @datasciencefun
  1. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 โ€” Forgetting that LIMIT applies after sorting Forโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Oct 2, 2026Data Visualisation tips for beginners
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 28, 2026๐Ÿ”น Real-World Data Science Example Suppose you are analyzing sales data. You want to identโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’