1 00:00:00,660 --> 00:00:04,080 Databases can store massive amounts of data, and 2 00:00:04,080 --> 00:00:07,040 often you don't want to bring back all of the results. 3 00:00:07,040 --> 00:00:10,200 It's slow to do something like that, it affects the performance for 4 00:00:10,200 --> 00:00:14,350 other users, and in most cases you only want a subset of rows. 5 00:00:14,350 --> 00:00:15,430 Not all of them are needed. 6 00:00:16,590 --> 00:00:17,750 Imagine your company, 7 00:00:17,750 --> 00:00:22,410 an e-commerce site, launched nine different marketing campaigns using email, 8 00:00:22,410 --> 00:00:28,020 television, banner ads, Google ads, and a variety of other marketing channels. 9 00:00:28,020 --> 00:00:33,070 The marketing team wants to know which of these campaigns produced the most revenue. 10 00:00:33,070 --> 00:00:37,550 They want to find out the top three effective marketing campaigns. 11 00:00:37,550 --> 00:00:41,080 Fortunately your website tracks all of this information and 12 00:00:41,080 --> 00:00:43,080 stores it in a database. 13 00:00:43,080 --> 00:00:46,385 Let's take a look in our database to see how we can answer this question. 14 00:00:47,920 --> 00:00:50,190 Let's have a look at the campaigns table. 15 00:00:50,190 --> 00:00:56,270 We have an ID column, the name of the campaign and the number of sales. 16 00:00:56,270 --> 00:01:00,400 Let's start with a general select staff from campaign statement. 17 00:01:01,760 --> 00:01:05,250 We want to order the results by the number of sales but 18 00:01:05,250 --> 00:01:10,310 in a descending order, meaning from the largest to the smallest number of sales. 19 00:01:12,130 --> 00:01:16,340 Remember the marketing team only wants the top three results. 20 00:01:16,340 --> 00:01:20,540 We could retrieve all the results and then manually pick off the top three or 21 00:01:20,540 --> 00:01:23,950 just give the full list to the person in the marketing department, 22 00:01:23,950 --> 00:01:28,420 which may be a bit distracting to whoever this report is intended for. 23 00:01:28,420 --> 00:01:33,600 Luckily SQL provides a way to limit the amount of results returned. 24 00:01:33,600 --> 00:01:36,510 To retrieve just a handful of the total of results, 25 00:01:36,510 --> 00:01:42,100 we use the LIMIT keyword followed by the number of rows we want retrieved. 26 00:01:43,940 --> 00:01:49,840 When we run this query now, we get the top three campaigns that generate sales. 27 00:01:49,840 --> 00:01:53,250 To recap, you can start any query you'd like and 28 00:01:53,250 --> 00:01:57,970 end it with the LIMIT keyword with the number of rows you'd like returned. 29 00:01:57,970 --> 00:02:03,120 You can also use it with queries with no conditions and with any number 30 00:02:03,120 --> 00:02:08,060 of conditions and including other SQL keywords like ORDER BY. 31 00:02:09,110 --> 00:02:12,540 The LIMIT keyword with the number of rows you want returning 32 00:02:12,540 --> 00:02:15,110 must be at the end of each query. 33 00:02:15,110 --> 00:02:20,170 Putting LIMIT anywhere but at the end of the statement generates a syntax error. 34 00:02:21,590 --> 00:02:25,100 You've already limited results with the WHERE clause, but 35 00:02:25,100 --> 00:02:29,190 now you can limit results with the LIMIT keyword. 36 00:02:29,190 --> 00:02:32,410 With WHERE clauses you need to know specifically the conditions you want to 37 00:02:32,410 --> 00:02:34,910 filter by, whereas with LIMIT, 38 00:02:34,910 --> 00:02:37,580 you're just concerned with the number of records returned. 39 00:02:38,740 --> 00:02:41,830 LIMIT isn't available in all SQL databases. 40 00:02:41,830 --> 00:02:45,920 Other databases can use a different keyword and syntax. 41 00:02:45,920 --> 00:02:48,700 I will include some code examples in the teacher's notes below.