Big SQL Energy Beginner Course: Lesson 2 Order By
Got BDE?⚡️
Hi BDEs! Welcome to Lesson 2 of the free Big SQL Energy Beginner Course⚡️ Today we’re learning how to sort datasets with Order By!
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 2: Order By
Now that you’ve learned how to pull some data using SELECT and FROM combined with * or column names, I bet you’re wondering how to sort data in SQL. It can be really hard to see any trends or insights with a large amounts of data, so sometimes we have to summarize, rearrange, or reshape data in order to learn more. However, I do want to caution you that sorting an entire dataset can have negative impacts to optimization since it can take a lot of time and power to sort a very large dataset. But let’s ignore that for now since we’re learning the basics— I talk more about optimization in my 🔗intermediate course.
To sort a dataset in SQL, we have to use the ORDER BY clause in SQL. By default, ORDER BY sorts in ascending order, or in other words smallest to largest. We can add it after FROM like this:
select
*
from
table
order by
order_date
There are other SQL clauses that sometimes go between FROM and ORDER BY, but we’ll learn those later. If you want to switch the order to descending order (largest to smallest), you can do it by adding “desc” after the column name:
select
*
from
table
order by
order_date desc
And of course you can order by multiple columns as well too. It’ll order by the first column and then by the second column. Here’s a few examples of that. Notice how including desc and changing the position changes our ordering:
select
*
from
table
order by
customer_id, order_date
;
select
*
from
table
order by
customer_id, order_date desc
;
select
*
from
table
order by
customer_id desc, order_date
;
select
*
from
table
order by
customer_id desc, order_date desc
;
In some SQL dialects (especially in newer cloud platforms like Snowflake for example), you can use a shorthand notation to represent the column name based on the columns selected in the SELECT statement. Order by 1, 2 orders by the first and second column in your select statement for example:
select
*
from
table
order by
1, 2
;
And that’s how we can sort datasets in SQL! See you tomorrow for the next lesson!
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: