SQLite is a popular database management system that provides various built-in functions to manipulate and retrieve data from the database. These functions can be classified into different categories, including aggregate functions, date and time functions, window functions, string functions, math functions, and JSON functions.
Aggregate functions are used to perform calculations on a set of values and return a single result. SQLite provides a variety of aggregate functions such as COUNT, SUM, AVG, MAX, and MIN. These functions are commonly used in queries to retrieve statistics about the data in a table. For example, you can use the COUNT function to count the number of rows in a table, or the SUM function to calculate the total value of a column.
|Calculates the sum of all values in a given column of a table.|
|Calculates the average value of a given column of a table.|
|Returns the maximum value in a given column of a table.|
|Returns the minimum value in a given column of a table.|
|Returns the number of rows that match a specified condition or all rows in a table.|
|Concatenates the values of a given column of a table into a single string, separated by a specified delimiter.|
|Calculates the sum of all values in a given column of a table, including NULL values.|
Date and Time functions
SQLite provides various
Date and Time functions to manipulate dates and times. Some of the common date and time functions include DATE, TIME, DATETIME, STRFTIME, and JULIANDAY. These functions allow you to perform operations such as converting dates and times to different formats, extracting components such as the day or month from a date, and calculating the difference between two dates.
|Extracts the date portion of a date/time expression, returning a string in the format “YYYY-MM-DD”.|
|Extracts the time portion of a date/time expression, returning a string in the format “HH:MM:SS”.|
|This function combines a date and time expression into a single string in the format “YYYY-MM-DD HH:MM:SS”.|
|This function formats a date/time expression according to a specified format string.|
|This function returns the Julian day number for a given date/time expression.|
Window functions are used to perform calculations on a specific set of rows in a result set. SQLite supports several window functions, including ROW_NUMBER, RANK, DENSE_RANK, and NTILE. These functions are commonly used in analytical queries to perform calculations such as running totals, moving averages, and rank order.
|Assigns a unique sequential number to each row within a result set.|
|Assigns a rank to each row within a result set based on the value of a specified column. Rows with the same value in the specified column receive the same rank.|
|This function is similar to RANK() but does not leave gaps in the ranking sequence when there are ties.|
|Divides the result set into a specified number of groups, assigning each row to one of the groups based on the value of a specified column.|
|Returns the value of a specified column from the previous row in the result set.|
|Returns the value of a specified column from the next row in the result set.|
|Returns the value of a specified column from the first row in the result set.|
|Returns the value of a specified column from the last row in the result set.|
String functions are used to manipulate strings in the database. SQLite provides several built-in string functions such as SUBSTR, LENGTH, UPPER, LOWER, and REPLACE. These functions allow you to perform operations such as extracting a substring from a string, converting a string to upper or lower case, and replacing a substring with another string.
|Returns the length of a string in characters.|
|Converts a string to uppercase.|
|Converts a string to lowercase.|
|Extracts a substring from a string based on a specified starting position and length.|
|Replaces all occurrences of a specified string with another string in a given string.|
|Removes leading and trailing whitespace characters from a string.|
Math functions are used to perform mathematical calculations on numeric data. SQLite provides various built-in math functions, such as ABS, ROUND, CEIL, FLOOR, and RANDOM. These functions allow you to perform operations such as finding the absolute value of a number, rounding a number to a specific decimal place, and generating a random number.
|Returns the absolute value of a number.|
|Rounds a number to a specified number of decimal places.|
|Returns the smallest integer greater than or equal to a given number.|
|Returns the largest integer less than or equal to a given number.|
|Returns the exponential value of a given number.|
|Returns the natural logarithm of a given number.|
|Returns the result of raising a given number to a specified power.|
|Returns the square root of a given number.|
|Returns the sine value of a given angle.|
|Returns the cosine value of a given angle.|
|Returns the tangent value of a given angle.|
|Returns the arc sine (inverse sine) of a given number.|
|Returns the arc cosine (inverse cosine) of a given number.|
|Returns the arc tangent (inverse tangent) of a given number.|
SQLite provides several built-in
JSON functions to work with JSON data. Some of the common JSON functions include JSON(), JSON_ARRAY(), JSON_OBJECT(), and JSON_EXTRACT(). These functions allow you to create and manipulate JSON objects and arrays, and extract data from JSON strings.
|Converts a JSON-formatted string into a SQLite value.|
|Extracts a value from a JSON object or array.|
|Creates a new JSON array from a list of values.|
|Creates a new JSON object from a list of key-value pairs.|
|Returns the type of a JSON value.|
In conclusion, SQLite provides a comprehensive set of built-in functions that enable you to perform various data manipulation and retrieval tasks. Whether you need to perform calculations on numeric data, manipulate strings, or work with date and time data, SQLite’s built-in functions can help you accomplish your goals efficiently and effectively.