W3Schools

home HOME

SQL Basic
SQL HOME
SQL Intro
SQL Select
SQL Where
SQL Insert
SQL Update
SQL Delete

SQL Demo
SQL Try It

SQL Advanced
SQL Order By
SQL and & or
SQL In
SQL Between
SQL Aliases
SQL Join
SQL Union
SQL Create
SQL Drop
SQL Alter
SQL Functions
SQL Group By
SQL Select Into
SQL Create View
SQL Server

SQL Quick Ref
SQL Summary

SQL Quiz
SQL Quiz

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Forum

Helping W3Schools

SQL Functions

previous next

SQL has a lot of built-in functions for counting and calculations.


Function Syntax

The syntax for built-in SQL functions is:

SELECT function(column) FROM table


Types of Functions

There are several basic types and categories of functions in SQL. The basic types of functions are:

  • Aggregate Functions
  • Scalar functions

Aggregate functions

Aggregate functions operate against a collection of values, but return a single value.

Note: If used among many other expressions in the item list of a SELECT statement, the SELECT must have a GROUP BY clause!!

"Persons" table (used in most examples)

Name Age
Hansen, Ola 34
Svendson, Tove 45
Pettersen, Kari 19

Aggregate functions in MS Access

Function Description
AVG(column) Returns the average value of a column
COUNT(column) Returns the number of rows (without a NULL value) of a column
COUNT(*) Returns the number of selected rows
FIRST(column) Returns the value of the first record in a specified field
LAST(column) Returns the value of the last record in a specified field
MAX(column) Returns the highest value of a column
MIN(column) Returns the lowest value of a column
STDEV(column)  
STDEVP(column)  
SUM(column) Returns the total sum of a column
VAR(column)  
VARP(column)  

Aggregate functions in SQL Server

Function Description
AVG(column) Returns the average value of a column
BINARY_CHECKSUM  
CHECKSUM  
CHECKSUM_AGG  
COUNT(column) Returns the number of rows (without a NULL value) of a column
COUNT(*) Returns the number of selected rows
COUNT(DISTINCT column) Returns the number of distinct results
FIRST(column) Returns the value of the first record in a specified field (not supported in SQLServer2K)
LAST(column) Returns the value of the last record in a specified field (not supported in SQLServer2K)
MAX(column) Returns the highest value of a column
MIN(column) Returns the lowest value of a column
STDEV(column)  
STDEVP(column)  
SUM(column) Returns the total sum of a column
VAR(column)  
VARP(column)  


Scalar functions

Scalar functions operate against a single value, and return a single value based on the input value.

Useful Scalar Functions in MS Access

Function Description
UCASE(c) Converts a field to upper case
LCASE(c) Converts a field to lower case
MID(c,start[,end]) Extract characters from a text field
LEN(c) Returns the length of a text field
INSTR(c,char) Returns the numeric position of a named character within a text field
LEFT(c,number_of_char) Return the left part of a text field requested
RIGHT(c,number_of_char) Return the right part of a text field requested
ROUND(c,decimals) Rounds a numeric field to the number of decimals specified
MOD(x,y) Returns the remainder of a division operation
NOW() Returns the current system date
FORMAT(c,format) Changes the way a field is displayed
DATEDIFF(d,date1,date2) Used to perform date calculations


previous next


Learn XML with Stylus Studio XML Editor – Free Download

Stylus Studio provides tools for editing and debugging XML, XSLT, XML Schema, DTD, XQuery, Web Services,and more!

Using Stylus Studio XML Tools You Can:

See why millions use Stylus Studio as their preferred XML tool. Download a FREE TRIAL or watch an online VIDEO DEMO today!




Jump to: Top of Page or HOME or Printer Friendly Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its contents. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2008 by Refsnes Data. All Rights Reserved.

Validate Validate W3C-WAI level A conformance icon W3Schools was converted to XHTML in December 1999