Locked lesson.
About this lesson
We will look at some built in functions for performing calculations on data
Exercise files
Download this lesson’s related exercise files.
Functions.docx58.9 KB Functions - Solution.docx
59.5 KB
Quick reference
Functions
SQL has lots of built in functions that let us calculate data.
When to use
We'll look at Count, Average, and Sum in this video.
Instructions
To Count:
SELECT COUNT([Age])
FROM [Customer].[dbo].[Current_Customers]
To Average:
SELECT AVG([Age])
FROM [Customer].[dbo].[Current_Customers]
To Sum:
SELECT SUM([Age])
FROM [Customer].[dbo].[Current_Customers]
Hints & tips
- You can do basic math in a select statement (SELECT 2 + 3)
- COUNT(Age)
- AVG(Age)
- SUM(Age)
- 00:00 In this video I wanna to look at some of the built-in
- 00:06 functions we can use to calculate data.
- 00:10 So we've already looked at one of these when we looked at the count function
- 00:13 earlier, but we're gonna drill down and look at a few more in this video.
- 00:17 First off, I should mention that,
- 00:19 just gonna copy this, you can do basic math with the select statement.
- 00:22 So 2 + 3, if we execute this, we get 5.
- 00:27 And you can use all of the basic math you think.
- 00:30 Multiplication, 2 times 3, 2 divided by 3, all of your basic math sort of things.
- 00:37 Let me change this to 6.
- 00:39 We can do, so that's kind of cool and that's sort of useful, but
- 00:43 there are also built-in functions that we can play around with.
- 00:46 And in this video I wanna just look at three of them, we're gonna look at count,
- 00:49 average, and sum.
- 00:51 And like I said, we've already used count in the past.
- 00:54 And whenever you do a function, you just name the function and then you use these
- 00:58 little parentheses and then inside of here you pass whatever you want to pass.
- 01:01 So if we wanna count the number of rows we have in the age column,
- 01:06 we have six rows just like that.
- 01:09 We can also do a sum, so
- 01:11 if we wanna add up all of the values in the age column, comes to 197.
- 01:17 We can also do average, AVG.
- 01:21 And we get 32.
- 01:22 So these things, there's a lot of little built-in functions like this.
- 01:25 You can do a little research to learn some of the other ones.
- 01:28 We don't have time to go into all of them in this course, but
- 01:30 they're all pretty much this easy to use.
- 01:32 You just name the function, put them in these parenthesis, and
- 01:35 then you pass inside whatever you wanna do.
- 01:38 Now obviously, the data type is important here.
- 01:41 If we tried to average, for instance, First Name.
- 01:48 We're gonna get an error, because you can't add letters, right?
- 01:51 It just doesn't work.
- 01:52 And also I should mention I'm typing in Age, as in the past,
- 01:56 we should probably be putting these in brackets, but it works either way.
- 01:59 So I mentioned at the beginning of this video, you can do basic math.
- 02:03 You can also use these different things to do basic math.
- 02:06 For instance, if we do sum of Age, we have 197.
- 02:10 Now if we wanted to count how many rows there are,
- 02:16 we could do COUNT Age, and so we get 6 columns.
- 02:22 The total sum of all of the values in those columns is 197.
- 02:26 Well, to get an average, you take the total sum divided by the number of things.
- 02:31 So we could easily just divide, put the little division sign right there,
- 02:36 run this, and get 32.
- 02:37 Which is the same as when we ran our average earlier, when we get 32.
- 02:44 So lots of different ways you can play around with these different functions.
- 02:48 Lots of different things you could use them for.
- 02:50 Just use your imagination and I'm sure you can think of a lot of different things.
- 02:53 And like I said, there's a lot of different functions you can learn.
- 02:55 I'm just giving you a taste in this video to show you some of the most basic
- 02:58 ones for calculations.
- 03:00 So up until now, we've been pulling stuff out of our database and displaying it.
- 03:04 In the next section we're gonna look at actually putting data in using SQL.
- 03:09 And we're gonna look at the insert statement.
Lesson notes are only available for subscribers.