SQL function
SQL has many built-in functions for counting and calculations.
SQL Aggregate function
The SQL Aggregate function calculates the values obtained from columns, returning a single value.
Useful Aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the value of the first record
LAST() - Returns the value of the last record
MAX() - Returns the maximum value
MIN() - returns the minimum value
SUM() - returns the sum
SQL Scalar function
The SQL Scalar function returns a single value based on input values.
Useful Scalar functions:
UCASE() - Convert a field to uppercase
LCASE() - Convert a field to lowercase
MID() - Extract characters from a text field, MySql uses
SubString(field, 1, end) - Extract characters from a text field
LEN() - Return the length of a text field
ROUND() - Round a numeric field to a specified number of decimal places
NOW() - Return the current system date and time
FORMAT() - Format how a field is displayed
SQL AVG() function
AVG() function
The AVG() function returns the average value of a numeric column.
SQL AVG() Syntax
SELECT AVG(column_name) FROM table_name
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data selected from the "access_log" table:
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
SQL AVG() instance
The following SQL statement gets the average value from the "count" column of the "access_log" table:
SELECT AVG(count) AS CountAverage FROM access_log;
Executing the above SQL output results are as follows:
The following SQL statement selects the "site_id" and "count" whose visits are higher than the average visits:
SELECT site_id, count FROM access_log
WHERE count > (SELECT AVG(count) FROM access_log);
Executing the above SQL output results are as follows:
SQL COUNT() function
The COUNT() function returns the number of rows matching the specified criteria.
SQL COUNT(column_name) syntax
The COUNT(column_name) function returns the number of values for the specified column (NULLs are not counted):
SELECT COUNT(column_name) FROM table_name;
SQL COUNT(*) syntax
The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name;
SQL COUNT(DISTINCT column_name) syntax
The COUNT(DISTINCT column_name) function returns the number of distinct values for the specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name;
NOTE: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data selected from the "access_log" table:
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
SQL COUNT(column_name) instance
The following SQL statement counts the total visits of "site_id"=3 in the "access_log" table:
SELECT COUNT(count) AS nums FROM access_log
WHERE site_id=3;
SQL COUNT(*) instance
The following SQL statement counts the total number of records in the "access_log" table:
SELECT COUNT(*) AS nums FROM access_log;
Executing the above SQL output results are as follows:
SQL COUNT(DISTINCT column_name) instance
The following SQL statement counts the number of records with different site_ids in the "access_log" table:
SELECT COUNT(DISTINCT site_id) AS nums FROM access_log;
Executing the above SQL output results are as follows:
SQL FIRST() function
FIRST() function
The FIRST() function returns the value of the first record in the specified column.
SQL FIRST() Syntax
SELECT FIRST(column_name) FROM table_name;
Note: Only MS Access supports the FIRST() function.
####SQL FIRST() Workspace SQL
Server Syntax in SQL Server, MySQL, and Oracle
SELECT TOP 1 column_name FROM table_name
ORDER BY column_name ASC;
example
SELECT TOP 1 name FROM Websites
ORDER BY id ASC;
MySQL syntax
SELECT column_name FROM table_name
ORDER BY column_name ASC
LIMIT 1;
example
SELECT name FROM Websites
ORDER BY id ASC
LIMIT 1;
Oracle syntax
SELECT column_name FROM table_name
ORDER BY column_name ASC
WHERE ROWNUM <=1;
example
SELECT name FROM Websites
ORDER BY id ASC
WHERE ROWNUM <=1;
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 6 | 百度 | https://www.baidu.com/ | 4 | CN |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
SQL FIRST() instance
The following SQL statement selects the value of the first record in the "name" column of the "Websites" table:
SELECT name AS FirstSite FROM Websites LIMIT 1;
The result of executing the above SQL is as follows:
SQL LAST() function
LAST() function
The LAST() function returns the value of the last record in the specified column.
SQL LAST() Syntax
SELECT LAST(column_name) FROM table_name;
Note: The LAST() function is only supported by MS Access.
SQL LAST() workspace in SQL Server, MySQL, and Oracle
SQL Server syntax
SELECT TOP 1 column_name FROM table_name
ORDER BY column_name DESC;
example
SELECT TOP 1 name FROM Websites
ORDER BY id DESC;
MySQL syntax
SELECT column_name FROM table_name
ORDER BY column_name DESC
LIMIT 1;
example
SELECT name FROM Websites
ORDER BY id DESC
LIMIT 1;
Oracle syntax
SELECT column_name FROM table_name
ORDER BY column_name DESC
WHERE ROWNUM <=1;
example
SELECT name FROM Websites
ORDER BY id DESC
WHERE ROWNUM <=1;
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 6 | 百度 | https://www.baidu.com/ | 4 | CN |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
SQL LAST() instance
The following SQL statement selects the value of the last record in the "name" column of the "Websites" table:
SELECT name FROM Websites
ORDER BY id DESC
LIMIT 1;
The result of executing the above SQL is as follows: