Locked lesson.
About this lesson
We will look at how we can sort returned data using the ORDER BY clause
Exercise files
Download this lesson’s related exercise files.
ORDER BY Clause.docx58.8 KB ORDER BY Clause - Solution.docx
58.9 KB
Quick reference
ORDER BY Clause
Order By allows us to arrange our results.
When to use
Use it whenever you'd like to arrange your results in a certain way.
Instructions
To use order by, just type:
ORDER BY [Column]
Where [Column] is the thing you'd like to order by.
You can order in Ascending (low to high) or Descending (high to low) order.
ORDER BY [Column] DESC
or
ORDER BY [Column] ASC
Hints & tips
- Order By allows you to arrange your output.
- Ascending means low to high (ASC)
- Descending means high to low (DESC)
- 00:05 In this video, I want to talk about order buy.
- 00:07 So, we're getting data from our table here, but
- 00:10 we can actually tell the system how we want to show the results.
- 00:14 And we can order them by different things, and we can do it in ascending or
- 00:18 descending order.
- 00:20 Ascending means from low to high, descending means from high to low.
- 00:24 For instance, we could have everybody listed by age ascending.
- 00:29 So, the youngest people are listed first, and the oldest people are listed last.
- 00:33 Or we could do the opposite, we could do descending where the oldest people
- 00:36 are listed first and the youngest people are listed last.
- 00:39 And that sort of thing, or alphabetically.
- 00:41 Ascending alphabetically would be A, B, C, D, E, F, G.
- 00:45 Descending would be Z, Y, X, whatever the alphabet is backwards, right?
- 00:49 So let's go ahead and try this.
- 00:51 Let's get rid of this wire clause.
- 00:53 And let's just come up here to our select statement and let's just give it a star.
- 00:57 And I'm gonna execute this.
- 00:58 And so, here we have six records.
- 01:00 And the first one is John Elder, and the last one is Julia Styles.
- 01:03 We have city, we have age, all the different things we can order by.
- 01:07 So let's go, this is the format.
- 01:10 We just type order by.
- 01:13 And let's see, what do we want?
- 01:16 Well, we can do age.
- 01:17 And if we execute, the default is ascending.
- 01:22 18, 27, 29, 32.
- 01:23 Remember, John Elder was first and Julia Styles was last.
- 01:26 In this new one here, Julia Styles is second and John Elder is second to last.
- 01:31 So it's ordering these by age in ascending order.
- 01:35 So we can go DESC for descending.
- 01:41 And now we have Mark Robertson, who is 52, and John Smith, who is the youngest at 18.
- 01:46 So DESC for descending, ASC for ascending.
- 01:51 So execute, and now we have youngest to oldest ascending order.
- 01:56 Very interesting, very useful a lot of times you want a certain age range or
- 02:00 whatever, and you wanna ascending or descending order that's how you do it.
- 02:05 So we've done age, we can try city.
- 02:08 What does that do?
- 02:09 Well, if we execute,
- 02:10 we're gonna get an error cuz text can't be sorted that way, right?
- 02:14 Come up to our current customer's table and click on Design,
- 02:19 and we can come on here and look.
- 02:20 So our e-mail address is for our varChar, so I think we can sort by that.
- 02:24 So let's go back here.
- 02:25 I'll go ahead and close this, and bring this back up.
- 02:28 Order by e-mail address, and let's give this a try.
- 02:33 And so here we have e-mail address, and this is ascending, H, I, J, K, L, M, N,
- 02:38 O, P, Q, R, S, T.
- 02:39 So it's alphabetically ascending.
- 02:42 And that's another point I wanna make about the type of columns.
- 02:45 The data type you select is gonna determine things like this in the future.
- 02:49 So maybe knowing this,
- 02:51 you might not wanna use text as a field if you want to sort by it later on.
- 02:55 Instead, use varChar or something like that.
- 02:57 So, something to keep in mind.
- 02:59 We can change this to descending, execute, boom.
- 03:02 Now it's T, S, M, N, J.
- 03:05 So it's backwards alphabetical.
- 03:07 So that's order by.
- 03:08 Very, very useful.
- 03:09 You'll use this all the time.
- 03:11 And just remember, it's ascending and descending.
- 03:13 This is the basic format, very straightforward.
- 03:15 So in the next video I want to talk about group by.
Lesson notes are only available for subscribers.