MySQL reference – functions and statements
An overview of MySQL and MariaDB functions and statements with examples on a sample database. The results of the examples are real outputs, verified in MySQL 8.0 and MariaDB; the differences between them are described in the entries.
162 entries
Text 40 entries
- ASCII Returns the numeric value of the first byte of a string (0–255).
- CHAR Returns a string built from the given codes.
- CHAR_LENGTH Returns the number of characters in a string.
- CONCAT Joins the given values into a single string.
- CONCAT_WS Joins the values and puts the separator between each two of them (WS = with separator).
- ELT Returns the nth value from the list str1, str2, … If n is less than 1 or greater than the number of values, it returns NULL.
- FIELD Returns the position of the value in the list val1, val2, … (from 1).
- FIND_IN_SET Returns the position of the value in a string where the items are separated by commas (for example 'coffee,tea,sugar').
- FORMAT Returns the number as a string with thousands separators, rounded to the given number of decimal places.
- FROM_BASE64 Decodes a Base64 string back to the original bytes – the opposite of TO_BASE64().
- HEX For a string, it returns each byte as two hexadecimal digits; for a number, its notation in base 16.
- INSERT (function) Returns the string in which len characters from pos are replaced by the new string.
- INSTR Returns the position (from 1) of the first occurrence of the substring in the string, or 0.
- LEFT Returns the first len characters of a string.
- LENGTH Returns the length of a string in bytes, not in characters.
- LIKE Returns 1 if the string matches the pattern.
- LOCATE Returns the position (from 1) where the substring first starts in the string.
- LOWER Converts all letters of a string to lowercase according to the rules of the character set, accented letters included.
- LPAD Pads the string on the left so that it has exactly len characters.
- LTRIM Removes spaces from the beginning of a string (from the left).
- MD5 Returns the MD5 checksum of the given string as 32 hexadecimal characters.
- REGEXP Returns 1 if the string contains a match of the regular expression.
- REGEXP_REPLACE Returns the string with all matches of the regular expression replaced.
- REGEXP_SUBSTR Returns the first part of the string that matches the regular expression.
- REPEAT Returns the string repeated count times.
- REPLACE Returns the string with all occurrences of from_str replaced by to_str.
- REVERSE Returns the string with the order of characters reversed.
- RIGHT Returns the last len characters of a string.
- RPAD Pads the string on the right to exactly len characters.
- RTRIM Removes spaces from the end of a string (from the right).
- SHA2 Returns a hash from the SHA-2 family as a hexadecimal string.
- SPACE Returns a string of n spaces.
- STRCMP Compares two strings according to the collation.
- SUBSTRING Returns part of a string from the given position, optionally only the given number of characters.
- SUBSTRING_INDEX Returns the part of the string before the countth occurrence of the delimiter.
- TO_BASE64 Encodes a string or binary data to Base64 (only letters, digits, +, / and =).
- TRIM Removes spaces, or the given string, from the beginning and the end of a string.
- UNHEX Converts pairs of hexadecimal digits to bytes – the opposite of HEX().
- UPPER Converts all letters of a string to uppercase according to the rules of the character set – accented letters too.
- UUID Returns a universally unique identifier (UUID version 1) in the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
Numbers 15 entries
- ABS Returns the absolute value of a number – a negative number becomes positive, positive numbers and zero stay.
- CAST Converts a value to the given type: SIGNED, UNSIGNED (integers), DECIMAL(m,d), CHAR, DATE, DATETIME, TIME, BINARY and others.
- CEIL Returns the smallest integer that is greater than or equal to the given number – it always rounds up.
- CONV Converts a number written in the base from_base to the base to_base (2 to 36).
- CONVERT It has two forms.
- FLOOR Returns the largest integer that is less than or equal to the given number – it always rounds down.
- GREATEST Returns the largest of the given values – it compares values in one row, not across rows (MAX() does that).
- LEAST Returns the smallest of the given values in one row (MIN() compares across rows).
- MOD Returns the remainder of a division.
- POW Returns x raised to the power of y.
- RAND Returns a random decimal number in the range 0 ≤ x < 1.
- ROUND Rounds a number to the given number of decimal places (0 by default).
- SIGN Returns -1 for a negative number, 0 for zero and 1 for a positive number.
- SQRT Returns the square root of a non-negative number.
- TRUNCATE Cuts a number to the given number of decimal places – it discards the rest and rounds nothing.
Date and time 37 entries
- ADDTIME Adds a duration written as a time ('01:30:00') to a time or a date with a time.
- CONVERT_TZ Converts a date and time from one time zone to another.
- CURDATE Returns the current date (the DATE type) in the YYYY-MM-DD format according to the session time zone.
- CURTIME Returns the current time (the TIME type) in the HH:MM:SS format according to the session time zone.
- DATE Returns the date part of a DATETIME value or of a string with a date and time.
- DATE_ADD Adds the given interval to a date.
- DATE_FORMAT Returns a date and time as a string according to the format.
- DATE_SUB Subtracts the given interval from a date.
- DATEDIFF Returns the number of days date1 − date2.
- DAY Returns the day of the month as a number from 1 to 31.
- DAYNAME Returns the name of the day of the week in the language set by lc_time_names.
- DAYOFWEEK Returns the day of the week as a number according to the ODBC standard: 1 = Sunday, 2 = Monday … 7 = Saturday.
- DAYOFYEAR Returns the day of the year: 1 January is 1, 31 December 365 (366 in a leap year).
- EXTRACT Returns the given part of a date as a number.
- FROM_UNIXTIME Converts a Unix timestamp to DATETIME in the session time zone.
- HOUR Returns the hour of a time or of a date and time.
- LAST_DAY Returns the date of the last day of the month that the given date belongs to.
- MAKEDATE Returns a date from a year and the day of the year (1 = 1 January).
- MINUTE Returns the minutes of a time or of a date and time as a number from 0 to 59.
- MONTH Returns the month of a date as a number from 1 (January) to 12 (December).
- MONTHNAME Returns the name of the month in the language set by lc_time_names – in English by default (March), in German with de_DE (März).
- NOW Returns the current date and time (the DATETIME type) in the session time zone.
- QUARTER Returns the quarter the date belongs to: 1 (January–March) to 4 (October–December).
- SEC_TO_TIME Converts a number of seconds to a TIME value in the form hours:minutes:seconds.
- SECOND Returns the seconds of a time or of a date and time as a number from 0 to 59.
- STR_TO_DATE Converts a string to a date or a date and time according to the format – the opposite of DATE_FORMAT().
- TIME Returns the time part of a DATETIME value or of a string.
- TIME_FORMAT Returns a time as a string according to the format.
- TIME_TO_SEC Returns the number of seconds that corresponds to the time – for a time of day the seconds since midnight, for a duration its total length in seconds.
- TIMEDIFF Returns the difference expr1 − expr2 as a TIME value (it can be negative).
- TIMESTAMPADD Adds interval units (SECOND to YEAR) to a date.
- TIMESTAMPDIFF Returns the difference end − start in the given unit (SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR), whole units only – the rest is discarded.
- UNIX_TIMESTAMP Returns the number of seconds since 1 January 1970 00:00:00 UTC.
- UTC_TIMESTAMP Returns the current date and time in Coordinated Universal Time (UTC), independently of the time_zone setting.
- WEEK Returns the week number of the year.
- WEEKDAY Returns the day of the week as a number from 0 = Monday to 6 = Sunday – it matches the ISO week, which starts on Monday.
- YEAR Returns the year of a date as a number (1000–9999).
Conditions 5 entries
- CASE It tries the WHEN conditions one by one and returns the value of the first true one.
- COALESCE Returns the first of the given values that is not NULL.
- IF If the condition is true (not zero and not NULL), it returns the second parameter, otherwise the third.
- IFNULL Returns the value, and if it is NULL, it returns the fallback.
- NULLIF If expr1 equals expr2, it returns NULL, otherwise it returns expr1.
Aggregates and operators 10 entries
- AVG Returns the arithmetic mean of the values in a group.
- BETWEEN Returns 1 if the value is between min and max including both limits – the same as expr >= min AND expr <= max.
- COUNT Counts rows.
- GROUP_CONCAT Joins the values from all rows of a group into one string.
- IN Returns 1 if the value equals any value of the list or the subquery.
- IS NULL Returns 1 if the value is NULL (missing).
- MAX Returns the largest value of an expression in a group of rows.
- MIN Returns the smallest value of an expression in a group of rows – the lowest number, the oldest date or the first string in alphabetical order.
- STDDEV Returns the standard deviation of the values in a group – how much the values differ from the average.
- SUM Adds up the values of an expression in the rows of a group.
JSON 14 entries
- JSON_ARRAY Creates a JSON array from the given values.
- JSON_CONTAINS Returns 1 if the document (or its part at the path) contains the searched value.
- JSON_EXTRACT Returns the value from a JSON document at the given path.
- JSON_INSERT Adds a value at a path where there is nothing yet.
- JSON_KEYS Returns a JSON array with the key names of the top-level object, or of the object at the given path.
- JSON_LENGTH Returns the number of elements of an array, the number of keys of an object, and 1 for a simple value.
- JSON_OBJECT Creates a JSON object from the given key – value pairs.
- JSON_REMOVE Returns the document without the values at the given paths.
- JSON_REPLACE Overwrites the value at a path only if it is already there.
- JSON_SEARCH Returns the path to the string value that matches the searched string (with % and _ as in LIKE).
- JSON_SET Returns the document with the new value set at the path: it overwrites an existing key and adds a missing one.
- JSON_UNQUOTE Removes the quotes around a JSON string value and converts escaped characters (\n, \") back.
- JSON_VALID Returns 1 for a valid JSON document, 0 for an invalid one and NULL for NULL.
- JSON_VALUE Returns the value (a string, a number) at the given path without quotes.
Window functions 9 entries
- DENSE_RANK Like RANK() – equal values have the same rank – but without gaps: 1, 2, 2, 3.
- FIRST_VALUE Returns the value of an expression from the first row of the window according to ORDER BY in OVER.
- LAG Returns the value of an expression from the row N rows up (1 by default) according to the order in OVER.
- LAST_VALUE Returns the value of an expression from the last row of the window.
- LEAD Like LAG(), but it reads the value from the row N rows down.
- NTILE Splits the sorted rows into n groups as evenly as possible and returns the group number (1 to n).
- OVER Defines the window of rows over which the function is calculated.
- RANK Returns the rank of a row as in a sports table: equal values get the same place and the next place is skipped (1, 2, 2, 4).
- ROW_NUMBER Assigns the sequence numbers 1, 2, 3… to the rows according to ORDER BY in the OVER clause.
Database information 4 entries
- DATABASE Returns the name of the current database (selected when connecting or with the USE statement).
- LAST_INSERT_ID Returns the AUTO_INCREMENT value generated by the last INSERT statement in the current connection.
- ROW_COUNT Returns the number of rows that the previous INSERT, UPDATE or DELETE statement really changed.
- VERSION Returns the server version as a string, such as 8.0.18 or 10.11.6-MariaDB.
Statements – queries 12 entries
- DISTINCT Returns each combination of the values of the selected columns only once.
- EXISTS Returns true if the subquery finds at least one row.
- GROUP BY Merges rows with the same values of the given expressions into one group.
- HAVING Keeps only the groups that meet the condition.
- JOIN An inner join returns the combinations of rows of both tables that meet the ON condition.
- LEFT JOIN Returns all rows of the left table.
- LIMIT Returns at most row_count rows; with OFFSET after skipping the given number.
- ORDER BY Sorts the rows of the result by the given columns or expressions – ascending (ASC, the default) or descending (DESC).
- SELECT Selects columns or calculated values from one or more tables.
- UNION Joins the rows of several SELECT queries into one result.
- WHERE Keeps only the rows for which the condition is true.
- WITH Names the result of a subquery (a Common Table Expression), and the main query works with it as with a table.
Statements – changing data 8 entries
- DELETE Deletes the rows that meet the condition.
- INSERT Inserts one or more rows into a table.
- INSERT IGNORE Inserts rows and silently leaves out those that would violate the primary key or a unique index.
- INSERT … SELECT Inserts into a table all the rows returned by a SELECT query.
- ON DUPLICATE KEY UPDATE When an insert would violate the primary key or a unique index, the existing row is updated according to the UPDATE part instead of an error.
- REPLACE (statement) It works like INSERT, but it first deletes the row with the same primary key or unique value.
- TRUNCATE TABLE Deletes all rows of a table at once – it actually drops the table and creates it again empty.
- UPDATE Sets new column values in all rows that meet the WHERE condition.
Statements – structure and transactions 8 entries
- ALTER TABLE Changes the structure of a table: adds, changes or removes columns, indexes and keys, renames the table or changes the encoding.
- CREATE INDEX Creates an index – a helper structure by which the database quickly finds rows without going through the whole table.
- CREATE TABLE Creates a table with the given columns, data types, keys and constraints.
- CREATE VIEW Saves a SELECT query under a name.
- DROP TABLE Deletes a table – the structure and all data.
- EXPLAIN Lists the execution plan of a query: the order of tables, the access type (type), the index used (key) and the estimated number of rows read (rows).
- SHOW CREATE TABLE Returns the complete CREATE TABLE statement matching the current structure of the table – columns, default values, indexes, foreign keys, the storage engine…
- START TRANSACTION Starts a transaction.
No entry matches your search.