SQLite Functions
SQLite Functions
SQLite functions
In this part of the SQLite tutorial, we will cover SQLite built-in functions. There are three types of
functions in SQLite database. Core, aggregate and date & time functions.
Core functions
In this group we have various diverse functios. Some are numerical functions, some work with text.
SQLite Version
--------------
3.5.9
Random
-------------------
1056892254869386643
The random() function returns a pseudo-random integer between -9223372036854775808 and
+9223372036854775807.
max(cost)
----------
350000
min(cost)
----------
9000
In our example, the max() and min() functions return the most and the least expensive cars from the
Cars table.
Names in Capitals
-----------------
JANE
THOMAS
FRANKLIN
ELISABETH
MARY
LUCY
JACK
length('ZetCode')
-----------------
Total changes
-------------
3
The total_changes() function returns the number of row changes caused by INSERT, UPDATE or
DELETE statements since the current database connection was opened. In the current database
connection, I have done three INSERT statements, so the total changes is equal to three.
Aggregate funcions
Id Name Cost
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
5 Bentley 350000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600
Notice, that there are no duplicate records.
# of cars
----------
The count(*) function returns the number of rows in the table. In our table, we have eight cars.
Id OrderPrice Customer
1 1200 Williamson
2 200 Robertson
3 40 Robertson
4 1640 Smith
5 100 Robertson
6 50 Williamson
7 150 Smith
8 250 Smith
9 840 Brown
10 440 Black
11 20 Brown
Logically, each customer can make multiple orders. How do we count the number of orders and how do
# of orders
-----------
11
This SQL statement returns the number of orders. To calculate the number of unique customers, we
# of customers
--------------
5
Next we are going to demonstrate the difference between the count(*) and count(ColumnName).
These function usages differ in the way, how they handle NULL values.
First, we change how sqlite3 shows NULL values. By default, the NULL value is shown as empty string.
id
----------
1
2
NULL
NULL
# of rows
----------
The count(*) returns the number of rows in the table. It takes NULL values into account.
--------------------
----------------
88528.1666666667
The avg() function returns the average value of all non NULL records. In our example, we show the
Finally, we mention the sum() function. It does a summation of all non NULL values.
Sum
----------
4930
SQLite has functions for working with date and time. With these functions we can use various time
2009-11-05
The date() function with the now string return the current date.
05-11-2009
We can use the the strftime() function to return a date in a different format.
Current day 05
This SQL statement returns the current day of the month. We used the strftime() function.
Days to XMas:49
Here we have computed the number of days till Christmas. The %j modifier gives the day of the year
2009-11-05
This SQL statement returns the first Thursday of the November for the current year. In this example, we
used three modifiers. start of year, +x months and weekday x. The now time string gives the
current date. The start of year shifts the date backwards to the beginning of the year. The 10
months adds 10 months to the current month (January). Finally, the weekday 4 modifier advances
In this part of the SQLite tutorial, we worked with the built-in SQLite functions.