Querying into Data(DQL)

Result Control with LIMIT and OFFSET

Learning Outcome

1

Understand the purpose of DISTINCT

2

Retrieve unique values from a table

3

Limit the number of records using LIMIT

4

Use LIMIT with OFFSET for pagination

Let's recall.....

How does Instagram ensure unique hashtags, limit results to the top 5 posts, and display posts page by page during scrolling?

What concepts does it apply here?

When you open Instagram:
 

You see trending unique hashtags
 

Only limited posts load initially
 

More posts load when you scroll

 Instagram does not show everything at once, instead it shows selected and controlled results

These features use DISTINCT and LIMIT concepts internally

Sometimes we need unique data
 

Sometimes we need limited records
 

Sometimes we need pagination

In such cases:

SQL provides special clauses:

DISTINCT
 

LIMIT
 

LIMIT with OFFSET

Lets understand them in detail....

What is Distinct?

DISTINCT keyword is used to remove duplicate values from the result set.

SELECT DISTINCT column_name FROM table_name;

Syntax:

Example: Fetch the unique courses from students table

Select Distinct Course from Students;

DISTINCT on Multiple Columns

SELECT DISTINCT city, username

FROM user_activity;

User_Activity

Result

 It will remove duplicates based on both columns together

Note:  DISTINCT checks combination of columns

Query

Imagine a scenario where you want to fetch only the unique city and usernames from a table

What is LIMIT Clause?

LIMIT clause is used to restrict the number of records returned in SQL query result

Why we use limit?

To show limited data
 

To improve performance
 

For pagination
 

To avoid loading entire table

Syntax: SELECT column_name FROM table_name LIMIT number;

Example:

Imagine you open Instagram

On your home feed, you don’t see all posts at once.

You only see maybe 5–10 posts initially.

That is exactly how LIMIT works

SELECT * FROM posts
LIMIT 5;

Result: This will return only first 5 records

What is OFFSET?

OFFSET is used to skip a specific number of rows before starting to return rows

Why we use Offset?

For pagination
 

For “Load More” functionality
 

To fetch next set of records

Syntax:

SELECT column_name

FROM table_name

LIMIT number OFFSET skip_number;

 

Example:

When you scroll Instagram, posts load in batches

This is exactly how OFFSET works

SELECT post_id, username
FROM posts
LIMIT 10 OFFSET 10;

OFFSET 10 = Skip first 10 posts

LIMIT 10 = Show next 10 posts

 

Result → Shows posts 11–20

OFFSET is used to Skip rows

LIMIT is used to Show rows

Pagination Formula

Formula

Example

Page number = P
Records per page = N

OFFSET = (P − 1) × N

Page = 3
Records per page = 5

OFFSET = (3 − 1) × 5 = 10

SELECT *
FROM employees
LIMIT 5 OFFSET 10;

Pagination is used to fetch records page-by-page

Summary

5

Does not modify actual table data

4

Used mainly for data display optimization

3

OFFSET skips specific rows

2

LIMIT restricts number of records

1

DISTINCT removes duplicate values

Quiz

Which clause is used to retrieve unique values?

A. LIMIT

B. DISTINCT

 C. WHERE

D. ORDER BY

Quiz-Answer

Which clause is used to retrieve unique values?

A. LIMIT

B. DISTINCT

 C. WHERE

D. ORDER BY

Result Control with Limit and Offset

By Content ITV

Result Control with Limit and Offset

  • 8