SQL Training Online

  • Training Courses
  • Beginner Book
  • Blog

June 15, 2012 by Joey Blue Leave a Comment

SQL Functions

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

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

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

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

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.

Related

Filed Under: SQL Training Tagged With: Beginning SQL, SQL Function, SQL Select

Leave a Question, Comment, or Reply. All are welcome! Cancel reply

Recent Posts

  • SQL Database Normalization – 1NF, 2NF, 3NF, and 4NF
  • SQL Joins Tutorial for Beginners – Inner Join, Left Join, Right Join, Full Outer Join – SQL Training Online
  • Zillow House Price Analysis from CSV Archive – #PowerBI 002
  • Learn Basic SQL – 1 Hour Training Course – SQL Training Online
  • Create Table Statement in SQL Server and Inserting Baseball Homerun Leader Dataset – SQL Training Online

Popular Posts

  • SQL Functions
  • SQL Jobs in Oracle and Microsoft SQL Server
  • Troubleshooting a SQL Join
Copyright © 2023 · SQLTrainingOnline.com · Consulting at EmbarkBlue.com