Big SQL Energy Beginner Course: Lesson 4 Limit & Top
Got BDE?⚡️
Hi BDEs! Welcome to Lesson 4 of the free Big SQL Energy Beginner Course⚡️ Today we’re learning how to limit datasets with LIMIT and TOP!
Return to Big SQL Energy Beginner homepage here to access the other lessons:
If you’re new here:
💁🏽♀️ Who Am I?
I’m Jess Ramos, the founder of Big Data Energy and the creator of the BEST SQL course and community: Big SQL Energy⚡️. Check me out on socials: 🔗LinkedIn, 🔗Instagram, 🔗YouTube, and 🔗TikTok. And of course subscribe to my 🔗newsletter here for all my upcoming lessons and updates— all for free!
⚡️ What is Big SQL Energy?
🔗Big SQL Energy Intermediate is an intermediate SQL course designed to solve real-world business problems hands-on using realistic data and a modern tech stack. You’ll walk away with 2 SQL portfolio projects, portfolio building guidance, access to the Big Data Energy Discord community, and lots of confidence for you upcoming coding interviews! You also get access to all monthly events including masterclasses, office hours, and guest speakers. This course is all of the most important things I’ve learned on the job as a Senior Data Analyst in tech who grew from a $72K salary to over a $150K salary. And now I’m sharing it with YOU! It’s more than just another course— it’s a challenging program designed to upskill your SQL to the intermediate level, get ready to ace your live coding interviews, and build connections in the data community through the Discord and events.
Lesson 4: Limit & Top
Very often when working with data, we’re working with A LOT of data. In a course or sandbox environment, you’re likely only working with a few rows, or hundreds if you’re lucky! In the real world, tables will have millions of rows which can take a long time to process… and be very expensive! Many of the new cloud platforms (like Snowflake and other data warehouses) bill based on usage (either on the amount of data pulled or computation time), so it’s essential as a data analyst to work as efficiently as possible. Not only do you not want to wait 20 minutes for your query to load, but it’ll likely be more expensive for your company.
Of course I have a ton of tips for query optimization when writing queries in my 🔗intermediate course, but I’m going to show you a quick tip that is great for beginners. Let’s start with:
select
*
from
orders
Based on what I told you about there being lots of data in the real world and the cost of computing data, what do you think about this query? What would it look like in the real world, and are there any issues you can think of? If you’re thinking that pulling in ALL the columns and ALL the rows from a table is probably not a great idea, YOU ARE RIGHT!
So yes, a great start would be to list the actual columns we need instead of just using *. You might not see a huge need in this dataset, but I’ve seen tables in the real world from CRMs (like HubSpot and Salesforce) with HUNDREDs of columns, so be specific instead of being lazy with * can make a big difference there.
The main focus of this lesson will actually be about LIMIT. Limit is the method we can use to reduce the rows of our output and get a sneak peek of our data which is a lot more optimized than pulling in the entire table. (Note that limit is at the very end of the order of execution, so it’s good to think about optimization higher up in the order of execution as well. I talk a lot more about this in my 🔗intermediate course, but don’t worry too much about this now if you’re a beginner.) You can add LIMIT to the end of any query and follow with how many rows you want in the output:
select
*
from
orders
limit
10;
select
*
from
orders
limit
100;
select
*
from
orders
limit
1000;
select
*
from
orders
limit
678;
Sometimes a quick 10 rows is enough to see a brief sample of your data, but other times you might need a larger sample to see more values so 100 or 1000 rows is better suited. I know 1000 seems like a lot of rows, but remember tables in the real world have MILLIONS!
Note that LIMIT is not supported in all SQL dialects and platforms, but it is supported in most of the newer ones. If it’s not working in the IDE you’re working in, check out TOP. You can always ChatGPT the syntax for limiting rows in the platform you’re working in if you have trouble!
Now that we’ve learned some super basics, we’re going to head into a bunch of functions and operations in the next few lessons!
If you’ve enjoyed the free BEGINNER Big SQL Energy course, grab my intermediate course. There are no deadlines or timelines for this course— you start and take as long as you want. It will take your beginner skills to the next level and get you ready to ace your interviews.
DM me or email me at courses@bdeanalytics.com if you have questions on the intermediate course!
Return to Big SQL Energy Beginner homepage here to access the other lessons: