SQL Tutorial



OFFSET CLAUSE IN SQL


โญ๏ธ OFFSET Clause in SQL

OFFSET is used to skip a specific number of rows before starting to return rows from a query. It's especially helpful in pagination where you want to display records in chunks (like 10 per page).

๐Ÿ”ง Syntax (MySQL / PostgreSQL):
SELECT column1, column2
FROM table_name
ORDER BY column_name
LIMIT count OFFSET start;

๐Ÿ“˜ Example: Get Records from 6 to 10

If you want to fetch the 6th to 10th records, skip the first 5 using OFFSET:

SELECT name, marks
FROM Students
ORDER BY marks DESC
LIMIT 5 OFFSET 5;
  

โœ… This skips the first 5 rows and shows the next 5.


๐Ÿ–ผ Sample Data โ€“ Students Table

IDNameMarks
1Ravi95
2Neha89
3Aman85
4Sara81
5Ritu80
6Karan78
7Preeti76

๐Ÿ’ก Real-Life Use Case: Pagination

  • Page 1: LIMIT 10 OFFSET 0
  • Page 2: LIMIT 10 OFFSET 10
  • Page 3: LIMIT 10 OFFSET 20

๐Ÿ“– This helps show data in a paginated manner, like search results, product lists, etc.


๐Ÿงช Try It Yourself

๐Ÿ“ Write a query to show the 3rd to 5th highest scoring students (use LIMIT + OFFSET)


๐ŸŽฏ Summary

  • OFFSET skips rows before starting to return data.
  • LIMIT defines how many rows to return.
  • Both are useful for pagination and chunk-based data fetching.

Next โžก๏ธ: Dive into GROUP BY to group and analyze data!


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review