SQL - Useful Functions
SQL - Useful Functions
SQL has many built-in functions for performing processing on string or numeric data. Following
is the list of all useful SQL built-in functions:
SQL COUNT Function - The SQL COUNT aggregate function is used to count the
number of rows in a database table.
SQL MAX Function - The SQL MAX aggregate function allows us to select the highest
(maximum) value for a certain column.
SQL MIN Function - The SQL MIN aggregate function allows us to select the lowest
(minimum) value for a certain column.
SQL AVG Function - The SQL AVG aggregate function selects the average value for
certain table column.
SQL SUM Function - The SQL SUM aggregate function allows selecting the total for a
numeric column.
SQL SQRT Functions - This is used to generate a square root of a given number.
SQL RAND Function - This is used to generate a random number using SQL
command.
SQL CONCAT Function - This is used to concatenate any string inside any SQL
command.
SQL String Functions - Complete list of SQL functions required to manipulate strings
in SQL.
1 | John | 2007-01-24 |
250 |
2 | Ram | 2007-05-27 |
3 | Jack | 2007-05-06 |
170 |
3 | Jack | 2007-04-06 |
100 |
4 | Jill | 2007-04-06 |
5 | Zara | 2007-06-06 |
300 |
5 | Zara | 2007-02-06 |
350 |
220 |
220 |
+------+------+------------+--------------------+
7 rows in set (0.00 sec)
Now suppose based on the above table you want to count total number of rows in this table,
and then you can do it as follows:
SQL>SELECT COUNT(*) FROM employee_tbl ;
+----------+
| COUNT(*) |
+----------+
|
7|
+----------+
1 row in set (0.01 sec)
Similarly, if you want to count the number of records for Zara, then it can be done as follows:
SQL>SELECT COUNT(*) FROM employee_tbl
-> WHERE name="Zara";
+----------+
| COUNT(*) |
+----------+
|
2|
+----------+
1 row in set (0.04 sec)
NOTE: All the SQL queries are case insensitive, so it does not make any difference if you give
ZARA or Zara in WHERE CONDITION.
1 | John | 2007-01-24 |
250 |
2 | Ram | 2007-05-27 |
3 | Jack | 2007-05-06 |
170 |
3 | Jack | 2007-04-06 |
100 |
4 | Jill | 2007-04-06 |
5 | Zara | 2007-06-06 |
300 |
5 | Zara | 2007-02-06 |
350 |
220 |
220 |
+------+------+------------+--------------------+
7 rows in set (0.00 sec)
Now
suppose
based
on
the
above
table
you
want
to
fetch
maximum
value
of
350 |
+-------------------------+
1 row in set (0.00 sec)
You can find all the records with maxmimum value for each name using GROUP BY clause as
follows:
SQL> SELECT id, name, MAX(daily_typing_pages)
-> FROM employee_tbl GROUP BY name;
+------+------+-------------------------+
| id | name | MAX(daily_typing_pages) |
+------+------+-------------------------+
|
3 | Jack |
170 |
4 | Jill |
1 | John |
2 | Ram |
220 |
5 | Zara |
350 |
220 |
250 |
+------+------+-------------------------+
5 rows in set (0.00 sec)
You can use MIN Function along with MAX function to find out minimum value as well. Try out
the following example:
SQL> SELECT MIN(daily_typing_pages) least, MAX(daily_typing_pages) max
-> FROM employee_tbl;
+-------+------+
| least | max |
+-------+------+
| 100 | 350 |
+-------+------+
1 row in set (0.01 sec)