1 00:00:00,460 --> 00:00:04,040 We're now going to use the Count Function to count groups of rows that have 2 00:00:04,040 --> 00:00:05,210 things in common. 3 00:00:05,210 --> 00:00:08,060 For example, how many products of an each category. 4 00:00:09,900 --> 00:00:13,890 Well, we could start out with a query where we select the category and 5 00:00:13,890 --> 00:00:15,290 order it by the category. 6 00:00:16,730 --> 00:00:19,070 We could count all of these up manually and 7 00:00:19,070 --> 00:00:21,980 keep track of a tally of each category. 8 00:00:21,980 --> 00:00:23,990 But there is no need for that. 9 00:00:23,990 --> 00:00:28,290 I've shown you how to use the Count function to count all the rows in a table. 10 00:00:28,290 --> 00:00:32,590 I've also shown you how to use Count to count the results from a query. 11 00:00:32,590 --> 00:00:38,830 But for this example, we need to count the groups of rows by their category. 12 00:00:38,830 --> 00:00:44,980 For example, all of the clothing products, all of the electronics products and so on. 13 00:00:44,980 --> 00:00:48,910 We can do that by using the group by keywords. 14 00:00:50,430 --> 00:00:53,860 At first glance, this looks like the key word distinct. 15 00:00:53,860 --> 00:00:56,100 But there's a key difference. 16 00:00:56,100 --> 00:00:58,822 Distinct discards any duplicates. 17 00:00:58,822 --> 00:01:04,820 Where as group by, groups rows together so you can use functions like count. 18 00:01:04,820 --> 00:01:08,157 Let's update this query to include the product counts. 19 00:01:12,007 --> 00:01:16,430 Now, that shows us how many products are in each category. 20 00:01:16,430 --> 00:01:18,330 Let's recap what we've just learned. 21 00:01:19,920 --> 00:01:21,570 Let's start with keywords. 22 00:01:22,740 --> 00:01:26,800 DISTINCT is used to get the unique values in a column. 23 00:01:26,800 --> 00:01:29,780 It's used in the select portion of a query. 24 00:01:29,780 --> 00:01:35,780 This will use the full table as a source of data to obtain distinct values. 25 00:01:35,780 --> 00:01:39,700 You can narrow down the dates and get a distinct values from that. 26 00:01:39,700 --> 00:01:41,100 You can use a WHERE cause. 27 00:01:42,180 --> 00:01:46,440 The next sets of key words you have learned were GROUP BY, 28 00:01:46,440 --> 00:01:49,470 GROUP BY appears at the end of the statement. 29 00:01:49,470 --> 00:01:52,370 This allows you to run functions like counts 30 00:01:52,370 --> 00:01:56,540 on collections of rows grouped by a particular value in a column. 31 00:01:57,630 --> 00:02:02,125 Then you can use the function like COUNT to count all rows that are in each group. 32 00:02:02,125 --> 00:02:09,058 [SOUND] Finally, you've seen COUNT counting all rows and the subsets of rows. 33 00:02:09,058 --> 00:02:13,090 You've seen me build up my queries, starting with simple queries first, 34 00:02:13,090 --> 00:02:16,190 then progressively getting more complex. 35 00:02:16,190 --> 00:02:17,960 This is a technique you can use. 36 00:02:17,960 --> 00:02:20,610 You don't have to write out your whole query in one go.