Hi Guys!
Its your favourite SQL teacher, Islamabaddataguy!
Hope you all are good and ready to dive into further concepts of SQL.
So, today we’ll be looking at LIMIT and OFFSET statements in SQL. I hope you guys have you IDEs open while looking at this post, ready to query.
But first.
Switching To Dbeaver
I have switched from SSMS to Dbeaver because I recently came to know that SSMS uses t-sql and not MySQL. There’s not much difference but I wanted to put out content for MySQL so I switched to Dbeaver. If you want to do that, just download Dbeaver community from their website and connect your MySQL server. If you’re confused at any point, you can ask chatgpt to help you.
LIMIT In SQL
LIMIT as obvious by its name is used to limit the data we want to see. It helps us put up a dam to control the flow of data coming towards you. For example, there are 64k rows in a table but you only need to see 10, so you’ll limit your data using LIMIT.
So, lets get started
We used a limit of 5 to see results limited to 5 rows only. So we can see the the highest number of sales range from 14-20.
If you have any queries, do let me know in the comments.
OFFSET In SQL
OFFSET just like LIMIT helps us to limit data as well. Just not how LIMIT does it. Offset skips the number of rows you tell it to skip before starting to fetch data. I’ll show what I mean with the same query I wrote above but using OFFSET this time.
So, lets get started.
So, what really happened in this query?
It is now showing us data from 13.94 till 13.1. However, in the limit query it was showing the data from 14-20. So what OFFSET 5 did was it omitted the first 5 rows so that data after the 5th row is fetched.
Note: OFFSET will not work without LIMIT. You have to declare both in order for your query to work.
Exercise
Today’s exercise will be a little bit specific.
Here’s what you need to do:
View all data using Select *
View how many transactions were done at a specific stations where payment method was credit card but show only 10 records
Now skip those 10 records and see another 5 records.
Comment your query below
Note: Only 1 person commented on my last post with their query. Honestly guys, I’m putting out this content for you guys. If you’re experts, you don’t need to comment but if you’re beginners and looking to get into data roles I would suggest following my exercises.
If you need help you can always ask for guidance in the comments. However, if you need personal guidance I would recommend going for a paid subscription as I have other commitments during the day and I have to make time for my precious people.
I am a BI Engineer working at a Data And AI Firm. I’m trying to help newbies in Data land their first data job and impact as many fellow data professionals as I can. Here’s my contact info:
ajmal7809@gmail.com
bideveloper_ (Discord)
Great work Jake
SELECT * FROM Railway
WHERE
[Payment Method] = 'Credit Card'
AND [Departure Station] = 'London Kings Cross'
ORDER BY [Transaction ID]
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;
It's show 5 rows skipping first 10.