WEBVTT 1 00:00:00.290 --> 00:00:01.380 Hi, welcome back. 2 00:00:01.380 --> 00:00:05.130 In this video we're talking about ordering and limiting. 3 00:00:05.130 --> 00:00:06.820 These are two things you can do with Sequel 4 00:00:06.820 --> 00:00:10.180 that can be pretty handy in a number of scenarios. 5 00:00:10.180 --> 00:00:12.660 Let's have this purchases table here 6 00:00:12.660 --> 00:00:15.520 that has three columns and four rows. 7 00:00:15.520 --> 00:00:18.150 Notice how there's an id, an amount, 8 00:00:18.150 --> 00:00:19.830 the money that was paid for this purchase, 9 00:00:19.830 --> 00:00:23.580 and also a date which is a Unix timestamp. 10 00:00:23.580 --> 00:00:25.930 We could do something like this: 11 00:00:25.930 --> 00:00:28.060 Select star from purchases, 12 00:00:28.060 --> 00:00:30.380 and now instead of where, or indeed, 13 00:00:30.380 --> 00:00:35.380 as well as where, we can use order by amount. 14 00:00:35.490 --> 00:00:38.400 This is going to order in ascending fashion, 15 00:00:38.400 --> 00:00:40.800 giving us something like this: 16 00:00:40.800 --> 00:00:45.800 9.99 at the top, followed by 14.99, 15.99 and 150. 17 00:00:47.210 --> 00:00:51.440 Of course we could order by amount descending instead 18 00:00:51.440 --> 00:00:55.820 by just putting DESC on the end for descending. 19 00:00:55.820 --> 00:00:59.630 And that gives us 150 at the top going down to 9.99, 20 00:00:59.630 --> 00:01:01.630 and if for some reason you wanted to make it explicit, 21 00:01:01.630 --> 00:01:03.670 that you wanted ascending, 22 00:01:03.670 --> 00:01:07.160 you can use ASC instead of DESC. 23 00:01:07.160 --> 00:01:08.550 Ascending is the default so you don't 24 00:01:08.550 --> 00:01:10.480 have to include it in Sequel Light, 25 00:01:10.480 --> 00:01:12.450 but it's possible that other database systems 26 00:01:12.450 --> 00:01:13.410 may be different. 27 00:01:15.410 --> 00:01:17.750 You can order by amount descending, 28 00:01:17.750 --> 00:01:19.980 that's the largest at the top, 29 00:01:19.980 --> 00:01:23.220 and limit one, and that's gonna give you only the top 30 00:01:23.220 --> 00:01:25.500 most row of your database. 31 00:01:25.500 --> 00:01:28.230 That's the most expensive purchase that anybody has made, 32 00:01:28.230 --> 00:01:31.190 and potentially you could do then things like, 33 00:01:31.190 --> 00:01:33.070 notify the user that has made this purchase 34 00:01:33.070 --> 00:01:34.670 and tell them they've won a voucher or something 35 00:01:34.670 --> 00:01:36.570 for spending the most money in your store. 36 00:01:36.570 --> 00:01:39.599 I don't know, but you see the, you see the deal there. 37 00:01:39.599 --> 00:01:42.320 You can get the top most of something, 38 00:01:42.320 --> 00:01:43.630 and indeed instead of limit one, 39 00:01:43.630 --> 00:01:45.130 you can do limit two, or limit three, 40 00:01:45.130 --> 00:01:47.540 if you wanna get the top three of something. 41 00:01:48.560 --> 00:01:51.020 So these two, order by and limit, 42 00:01:51.020 --> 00:01:55.220 are pretty handy to use, and you can use them together. 43 00:01:55.220 --> 00:01:57.770 If you wanted to put a where clause, 44 00:01:57.770 --> 00:02:00.640 it would go before the order by clause. 45 00:02:01.590 --> 00:02:03.730 So you can also put that in there. 46 00:02:03.730 --> 00:02:06.590 Again, this course is not meant to be a in depth 47 00:02:06.590 --> 00:02:08.120 Sequel tutorial. 48 00:02:08.120 --> 00:02:09.570 I just wanted to guide you enough 49 00:02:09.570 --> 00:02:12.140 so that you can sort of interact with tables, 50 00:02:12.140 --> 00:02:13.790 and store and retrieve some data. 51 00:02:14.840 --> 00:02:16.810 If you want to learn much more about Sequel, 52 00:02:16.810 --> 00:02:19.840 then I'd encourage you to go and investigate, 53 00:02:19.840 --> 00:02:21.300 read stuff online, 54 00:02:21.300 --> 00:02:22.940 implement your own projects that use them, 55 00:02:22.940 --> 00:02:24.280 and learn through that, 56 00:02:24.280 --> 00:02:25.890 and of course you can also take some 57 00:02:25.890 --> 00:02:28.414 other courses if you are so inclined. 58 00:02:28.414 --> 00:02:29.610 But that's it for this video. 59 00:02:29.610 --> 00:02:32.890 I just wanted to guide you through ordering and limiting, 60 00:02:32.890 --> 00:02:34.540 and I'll see you on the next one.