0% found this document useful (0 votes)
33 views6 pages

MySQL Functions Overview by Category

The document lists various MySQL functions categorized into Aggregate, String, Date & Time, Math, Control Flow, NULL Handling, Type Conversion, Information, and JSON Functions. Each function includes its syntax and a brief description of its purpose. This serves as a reference guide for using MySQL functions effectively.

Uploaded by

Harsha.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

MySQL Functions Overview by Category

The document lists various MySQL functions categorized into Aggregate, String, Date & Time, Math, Control Flow, NULL Handling, Type Conversion, Information, and JSON Functions. Each function includes its syntax and a brief description of its purpose. This serves as a reference guide for using MySQL functions effectively.

Uploaded by

Harsha.S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MySQL Functions by Category

Aggregate Functions

COUNT()
Syntax: COUNT(*) or COUNT(column)
Description: Count rows

SUM()
Syntax: SUM(column)
Description: Total sum

AVG()
Syntax: AVG(column)
Description: Average

MIN()
Syntax: MIN(column)
Description: Minimum value

MAX()
Syntax: MAX(column)
Description: Maximum value

GROUP_CONCAT()
Syntax: GROUP_CONCAT(column)
Description: Join values in a group

String Functions

CONCAT()
Syntax: CONCAT(str1, str2)
Description: Joins strings

SUBSTRING()
Syntax: SUBSTRING(str, pos, len)
Description: Extracts substring

LEFT()
Syntax: LEFT(str, len)
Description: Leftmost characters

RIGHT()
Syntax: RIGHT(str, len)
MySQL Functions by Category

Description: Rightmost characters

LENGTH()
Syntax: LENGTH(str)
Description: Byte length

CHAR_LENGTH()
Syntax: CHAR_LENGTH(str)
Description: Character count

REPLACE()
Syntax: REPLACE(str, from, to)
Description: Replace substring

INSTR()
Syntax: INSTR(str, substr)
Description: Find position

TRIM()
Syntax: TRIM(str)
Description: Remove spaces

LOWER()
Syntax: LOWER(str)
Description: Lowercase

UPPER()
Syntax: UPPER(str)
Description: Uppercase

Date & Time Functions

NOW()
Syntax: NOW()
Description: Current datetime

CURDATE()
Syntax: CURDATE()
Description: Current date

YEAR()
Syntax: YEAR(date)
MySQL Functions by Category

Description: Extract year

MONTH()
Syntax: MONTH(date)
Description: Extract month

DAY()
Syntax: DAY(date)
Description: Extract day

DATEDIFF()
Syntax: DATEDIFF(date1, date2)
Description: Date difference

ADDDATE()
Syntax: ADDDATE(date, INTERVAL n DAY)
Description: Add days

DATE_FORMAT()
Syntax: DATE_FORMAT(date, format)
Description: Format date

Math Functions

ABS()
Syntax: ABS(number)
Description: Absolute value

CEIL()
Syntax: CEIL(number)
Description: Round up

FLOOR()
Syntax: FLOOR(number)
Description: Round down

ROUND()
Syntax: ROUND(number, decimals)
Description: Round to decimal

TRUNCATE()
Syntax: TRUNCATE(number, decimals)
MySQL Functions by Category

Description: Cut without rounding

MOD()
Syntax: MOD(a, b)
Description: Remainder

POWER()
Syntax: POWER(a, b)
Description: Exponent

SQRT()
Syntax: SQRT(n)
Description: Square root

Control Flow Functions

IF()
Syntax: IF(condition, true_val, false_val)
Description: Conditional logic

IFNULL()
Syntax: IFNULL(expr, default)
Description: Replace NULL

NULLIF()
Syntax: NULLIF(expr1, expr2)
Description: NULL if equal

CASE
Syntax: CASE WHEN cond THEN val ... ELSE val END
Description: Multi-condition logic

NULL Handling

ISNULL()
Syntax: ISNULL(expr)
Description: Is value NULL?

COALESCE()
Syntax: COALESCE(a, b, c)
Description: First non-null
MySQL Functions by Category

Type Conversion

CAST()
Syntax: CAST(expr AS type)
Description: Convert type

CONVERT()
Syntax: CONVERT(expr, type)
Description: Same as CAST

Information Functions

DATABASE()
Syntax: DATABASE()
Description: Current DB name

USER()
Syntax: USER()
Description: Current user

VERSION()
Syntax: VERSION()
Description: MySQL version

LAST_INSERT_ID()
Syntax: LAST_INSERT_ID()
Description: Last inserted ID

JSON Functions (MySQL 5.7+)

JSON_OBJECT()
Syntax: JSON_OBJECT(key, value, ...)
Description: Create JSON object

JSON_ARRAY()
Syntax: JSON_ARRAY(val1, val2, ...)
Description: JSON array

JSON_EXTRACT()
Syntax: JSON_EXTRACT(json, path)
Description: Get JSON value
MySQL Functions by Category

JSON_SET()
Syntax: JSON_SET(json, path, value)
Description: Set JSON field

Common questions

Powered by AI

The CASE function allows multi-condition logic by evaluating a series of conditions and returning corresponding values or a default, providing flexible, inline conditional handling. The IF() function offers a simpler conditional structure for binary outcomes. Combining CASE and IF() allows fine granularity and complex decision-making directly within SQL queries, such as dynamically classifying or re-categorizing results based on multiple criteria, which reduces the need for additional processing logic outside the database .

SUM() is used to calculate the total sum of a numeric column across all rows, providing insights into the total quantity, sales, or units represented within a dataset. COUNT() is used to determine the number of rows or non-null entries in a column, which is useful for understanding the size or the presence of data points. Choose SUM() when you are interested in quantitative insights and COUNT() when you need to know how many entries exist .

DATE_FORMAT() allows for custom formatting of date outputs, significantly enhancing their readability by enabling the specification of different formats, such as 'YYYY-MM-DD' or 'DD/MM/YYYY'. This function impacts the data presentation by making dates more intuitive and context appropriate for the end-users' or systems' cultural expectations, thus facilitating accurate interpretation and analysis .

LENGTH() calculates the byte length of a string, which can differ from CHAR_LENGTH() that computes the number of characters. This difference arises when using multibyte character sets, such as UTF-8, where some characters may be represented by more than one byte. Therefore, for input strings containing multibyte characters, LENGTH() will return a higher value compared to CHAR_LENGTH().

The GROUP_CONCAT() function is used to concatenate values from multiple rows into a single string with a specified separator, while CONCAT() is used to join multiple strings or values. By leveraging both functions, you can create complex formatted strings. For instance, use GROUP_CONCAT() to combine related values across rows, separated by commas, and CONCAT() to append additional text or characters for more precise formatting of those combined strings .

JSON_OBJECT() creates JSON objects enabling key-value storage directly within MySQL, useful for structured data within a database context. JSON_ARRAY() allows for the storage and retrieval of ordered JSON data. Both functions enhance data handling by facilitating the storage of complex data directly in the table, reducing the need for additional parsing logic and enhancing database-schema flexibility. They provide a streamlined way to handle semi-structured data efficiently within SQL .

ADDDATE() can extend a given date by a specified interval, creating future date estimates, while DATEDIFF() calculates the difference in days between two dates. Together, they can manage and compare dates by first using ADDDATE() to project certain deadlines or future events and then using DATEDIFF() to measure the gap between original and calculated dates, facilitating timeline management and analysis of chronological data .

FLOOR() rounds a number down to the nearest integer, while CEIL() rounds up. The choice affects data analysis by changing how results from decimal calculations are interpreted. Using FLOOR() can result in underestimations, potentially misrepresenting minimum thresholds, whereas CEIL() could lead to overestimations, possibly inflating maximum thresholds. These functions, therefore, impact data summarization and decision-making where exact cut-offs are crucial .

The IFNULL() function replaces a NULL value with a specified default expression, ensuring a result is always returned in place of NULL. ISNULL(), on the other hand, is used to check if a given expression is NULL, returning a boolean indicator (1 if NULL, 0 otherwise). Thus, IFNULL() actively replaces NULL values, while ISNULL() serves as a conditional check .

CAST() and CONVERT() perform similar type conversions in SQL; however, one key consideration is syntactic preference as CONVERT() can vary across SQL dialects with potential for more options. Another consideration is compatibility; CAST() adheres more closely to the SQL standard, which may improve cross-platform consistency. Understanding specific database requirements and context will drive the choice between these functions .

You might also like