Back to Blog

Beginning SQL

SQL Functions

Explore essential SQL Server functions for data manipulation. Learn string, date, numeric, and aggregate functions with practical examples and best practices.

4 min read

Last updated on

Today I want to talk about SQL Functions and how they are used in the SQL language.

Let-s start by looking at a simple employee table.

select * from employee;


![SQL Functions Result](../../assets/blog/2012/06/image_thumb14.png)

This employee table has 14 rows.

Now, there are some built-in SQL Functions that allow you to average, sum, get the max and minimum and count. (There are others too, but I am only mentioning a few.)

These functions will look at a group of rows and perform the required action.

So, what if I want to get the **average salary** of all 14 of my employees?

Then I would do the following:

select AVG(salary) from employee;

The average function is actually just **AVG** and you put the column in between the parenthesis.

Here are the results:

![SQL Avg Result](../../assets/blog/2012/06/image_thumb15.png)

So the average salary is 2073.21-

That was pretty easy. But, what if you want the column to have a name?

select AVG(salary) as average_salary from employee;

You do what is called a **column alias**. After you put the column in (in this case AVG(salary) ) you put in the -as- keyword, then you put whatever you want to name the column. (In this case I called the column average_salary)

And here is the result:

![SQL Alias Result](../../assets/blog/2012/06/image_thumb16.png)

And now your column has a name.

So now let me go ahead and use the rest of the functions I mentioned.

select

AVG(salary) as average_salary

,sum(salary) as sum_salary

,min(salary) as min_salary

,max(salary) as max_salary

,count(salary) as count_salary

from employee;

And here are the results:

![SQL Functions Result](../../assets/blog/2012/06/image_thumb17.png)

Not too difficult- But, I want to try one more thing.

I want to use the **SUM** function and the **COUNT** function to create my own average function.

If you remember from Algebra, the average is calculated by summing all of the values and dividing by the number of values.

So let-s give it a try:

select sum(salary)/count(salary) as my_new_average

from employee;

Notice how I took the Sum/Count.

Here is the result:

SQL Average Result

Notice that the value is the same as when we used the built in average function. That is what we would expect.

So that is an introduction into SQL Functions.

Leave any question, comments, or topic suggestions bellow.

About Joey Blue

Joey Blue teaches practical data skills that companies actually use. With 25+ years of experience solving real data problems for Fortune 500 companies, he's helped 152,000+ students learn SQL, Power BI, reporting, and modern analytics—cutting straight to what works.