Okay, next up is a fairly easy one. Let's say I have got a straightforward SQL statement here, just selecting star from SalesLT.Address, and I scan some of this information and see that I've got a lot of information such as different states and different countries and regions. And what I might be interested in this is this. I don't really care about the individual pieces of data, but I'd like to know, for example, what the different countries are that we ship to. Well, I could start off by just filtering this data down.
Rather than SELECT *, I'll just say well, SELECT CountryRegion FROM SalesLT.Address and execute that. It still returns 450 rows that I could scan through, and it certainly doesn't look like there are too many countries, but just to be sure how could I start to ask for those individual values. I really just want to know it is Canada, the US, and whatever counties there are. Well, this is what I do. I use the word DISTINCT in the SELECT statement just before the actual column that I'm interested in, which really means only bring me back an individual value, just bring it back once for each value, even if it's repeated. I execute that.
We get now the distinct values for this column, which is Canada, United Kingdom, and United States. Now, if you find it useful, you can even go a little deeper than that. If I, for example, wanted to find both the distinct country regions and within that, we're actually storing StateProvince too. That's one of the columns in this table. I don't need to use the word DISTINCT again, but if I execute this line, what I'll get is the distinct countries and the distinct state provinces, whether they're repeated or not.
DISTINCT can be a very useful keyword when you find yourself getting too much data back and you really just want to filter it down, and in fact you'll find that using this DISTINCT keyword can actually be very useful even if you feed it into one of the other functions. For example, I could surround that phrase with the COUNT function, execute that, and I just get the answer. How many countries do we ship to? Three. I'm counting the distinct ones and just totaling them up.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 73773 Viewers
80 Video lessons · 129368 Viewers
52 Video lessons · 63716 Viewers
59 Video lessons · 49489 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.