Cheat Sheet for SQL Functions
SQL supports a variety of built-in functions that can be categorized into several groups based on their purposes. Here’s a list of commonly used SQL functions, organized by category:
Aggregate Functions
SUM(): Calculates the sum of values in a column.AVG(): Calculates the average value in a column.COUNT(): Counts the number of rows or non-null values in a column.MAX(): Returns the maximum value in a column.MIN(): Returns the minimum value in a column.GROUP_CONCAT()(MySQL, SQLite) orSTRING_AGG()(SQL Server): Concatenates values into a single string within a group.ARRAY_AGG()(PostgreSQL): Aggregates values into an array within a group.
Math Functions
+,-,*,/,%: Arithmetic operators.DIV: Integer division5 DIV 2.ABS(): Returns the absolute value of a number.ROUND(): Rounds a number to a specified number of decimal places.CEIL()orCEILING(): Rounds a number up to the nearest integer.FLOOR(): Rounds a number down to the nearest integer.POWER(): Raises a number to a specified power.SQRT(): Calculates the square root of a number.LOG()orLN(): Calculates the natural logarithm of a number.EXP(): Calculates the exponential value of a number.PI()(MySQL and PostgreSQL): Returns the mathematical constant (pi).
Conditional Functions
CASE WHEN ... THEN ... ELSE ... END: Performs conditional logic.COALESCE()(Database-Specific): Returns the first non-null value in a list.NULLIF()(Database-Specific): Compares two expressions and returns null if they are equal.
String Functions
CONCAT(): Concatenates two or more strings.CONCAT_WS(): Concatenates values with a specified separator.LENGTH()orLEN(): Returns the length (number of characters) of a string.SUBSTRING()orSUBSTR(): Extracts a substring from a string.REPLACE(): Replaces all occurrences of a substring within a string.UPPER(): Converts a string to uppercase.LOWER(): Converts a string to lowercase.TRIM(): Removes leading and trailing spaces from a string.LEFT(): Returns a specified number of characters from the left of a string.RIGHT(): Returns a specified number of characters from the right of a string.POSITION()orCHARINDEX(): Finds the position of a substring within a string.INITCAP()(PostgreSQL): Capitalizes the first letter of each word in a string.- Full-Text Search Functions (Database-Specific) (e.g.,
CONTAINS,FREETEXT,MATCH()) for performing advanced text search and ranking in text-based columns.
Date and Time Functions
CURRENT_DATE: Returns the current date.CURRENT_TIME: Returns the current time.CURRENT_TIMESTAMP: Returns the current timestamp (date and time).DATE(): Extracts the date portion from a timestamp.TIME(): Extracts the time portion from a timestamp.YEAR(): Returns the year from a date or timestamp.MONTH(): Returns the month from a date or timestamp.DAY(): Returns the day of the month from a date or timestamp.HOUR(): Returns the hour from a time or timestamp.MINUTE(): Returns the minute from a time or timestamp.SECOND(): Returns the second from a time or timestamp.DATEADD()(SQL Server) orINTERVAL(PostgreSQL): Adds a specified interval to a date or timestamp.DATEDIFF()(SQL Server) orDATE_PART()(PostgreSQL): Calculates the difference between two dates or timestamps.EXTRACT()(PostgreSQL): Extracts components (e.g., year, month, day) from a date or timestamp.TIMESTAMPDIFF()(MySQL) orEXTRACT()(Oracle): Calculates the difference between two dates or timestamps in a specific unit (e.g., days, hours).
Conversion Functions
TO_CHAR(): Converts a value to a character string with a specified format (typically used for date and time formatting).TO_DATE(): Converts a character string to a date.TO_NUMBER(): Converts a character string to a number.CAST(): Converts one data type to another.CONVERT(): Converts one data type to another (database-specific).
Other Functions
IFNULL()orISNULL(): Checks if a value is null and returns an alternative value.
The availability of these functions may vary depending on the specific database system you are using (e.g., MySQL, PostgreSQL, SQL Server, Oracle, etc.). Additionally, some databases provide additional custom functions and extensions. Be sure to refer to your database system’s documentation for a comprehensive list of functions and their usage.
Comments