SQLite classic ...login
SQLite classic tutorial
author:php.cn  update time:2022-04-13 17:05:02

SQLite common functions


SQLite has many built-in functions for working with string or numeric data. Listed below are some useful SQLite built-in functions, and all functions are case-insensitive, which means you can use lowercase or uppercase or mixed forms of these functions. For more details, please check the official documentation of SQLite:

Serial NumberFunction & Description
1SQLite COUNT function
SQLite COUNT aggregate function is used to count the number of rows in a database table.
2SQLite MAX function
SQLite MAX aggregate function allows us to select the maximum value of a column.
3SQLite MIN function
SQLite MIN aggregate function allows us to select the minimum value of a column.
4SQLite AVG function
SQLite AVG aggregate function calculates the average of a column.
5SQLite SUM function
SQLite SUM aggregate function allows calculating the sum for a numeric column.
6SQLite RANDOM function
The SQLite RANDOM function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807.
7SQLite ABS function
SQLite ABS function returns the absolute value of a numeric argument.
8SQLite UPPER function
The SQLite UPPER function converts a string to uppercase letters.
9SQLite LOWER function
The SQLite LOWER function converts a string to lowercase letters.
10SQLite LENGTH function
SQLite LENGTH function returns the length of a string.
11SQLite sqlite_version function
SQLite sqlite_version function returns the version of the SQLite library.

Before we start explaining these function examples, assume that the COMPANY table has the following records:

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

SQLite COUNT function

SQLite COUNT aggregate function is used to count rows in a database table number. Here is an example:

sqlite> SELECT count(*) FROM COMPANY;

The above SQLite SQL statement will produce the following results:

count(*)
----------
7

SQLite MAX function

SQLite MAX aggregate function allows us to select the maximum value of a column. Here is an example:

sqlite> SELECT max(salary) FROM COMPANY;

The above SQLite SQL statement will produce the following results:

max(salary)
-----------
85000.0

SQLite MIN function

SQLite MIN aggregate function allows us to select the minimum value of a column. Here is an example:

sqlite> SELECT min(salary) FROM COMPANY;

The above SQLite SQL statement will produce the following results:

min(salary)
-----------
10000.0

SQLite AVG Function

SQLite AVG aggregate function calculates the average of a column. Here is an example:

sqlite> SELECT avg(salary) FROM COMPANY;

The above SQLite SQL statement will produce the following results:

avg(salary)
----------------
37142.8571428572

SQLite SUM function

SQLite SUM aggregate function allows calculating the sum for a numeric column. The following is an example:

sqlite> SELECT sum(salary) FROM COMPANY;

The above SQLite SQL statement will produce the following results:

sum(salary)
-----------
260000.0

SQLite RANDOM function

The SQLite RANDOM function returns a value between -9223372036854775808 and +9223372036854775807 pseudo-random integers between. The following is an example:

sqlite> SELECT random() AS Random;

The above SQLite SQL statement will produce the following results:

Random
-------------------
5876796417670984050

SQLite ABS function

The SQLite ABS function returns the absolute value of a numeric argument. The following is an example:

sqlite> SELECT abs(5), abs(-15), abs(NULL), abs(0), abs("ABC");

The above SQLite SQL statement will produce the following results:

abs(5)      abs(-15)    abs(NULL)   abs(0)      abs("ABC")
----------  ----------  ----------  ----------  ----------
5           15                      0           0.0

SQLite UPPER function

The SQLite UPPER function converts a string to uppercase letters. The following is an example:

sqlite> SELECT upper(name) FROM COMPANY;

The above SQLite SQL statement will produce the following results:

upper(name)
-----------
PAUL
ALLEN
TEDDY
MARK
DAVID
KIM
JAMES

SQLite LOWER function

The SQLite LOWER function converts a string to lowercase letters. The following is an example:

sqlite> SELECT lower(name) FROM COMPANY;

The above SQLite SQL statement will produce the following results:

lower(name)
-----------
paul
allen
teddy
mark
david
kim
james

SQLite LENGTH function

SQLite LENGTH function returns the length of a string. Here is an example:

sqlite> SELECT name, length(name) FROM COMPANY;

The above SQLite SQL statement will produce the following results:

NAME        length(name)
----------  ------------
Paul        4
Allen       5
Teddy       5
Mark        4
David       5
Kim         3
James       5

SQLite sqlite_version function

SQLite sqlite_version function returns the version of the SQLite library. The following is an example:

sqlite> SELECT sqlite_version() AS 'SQLite Version';

The above SQLite SQL statement will produce the following results:

SQLite Version
--------------
3.6.20

php.cn