SQL_Functions
SQL_Functions
==================
AGGREGATE FUNCTIONS
-------------------
AVG() – Returns the average value
COUNT() – Returns the number of rows
MAX() – Returns the maximum value
MIN() – Returns the minimum value
SUM() – Returns the total sum
GROUP_CONCAT() – Concatenates values from multiple rows into a single string
(MySQL)
STDEV(), VAR() – Statistical functions
STRING FUNCTIONS
----------------
CONCAT() – Combines two or more strings
SUBSTRING() – Extracts part of a string
LENGTH() – Returns the length of a string
UPPER() – Converts string to upper case
LOWER() – Converts string to lower case
TRIM() – Removes leading and trailing spaces
REPLACE() – Replaces part of a string
LEFT() – Extracts characters from the left
RIGHT() – Extracts characters from the right
INSTR() – Returns the position of a substring
LTRIM() – Trims left spaces
RTRIM() – Trims right spaces
REVERSE() – Reverses a string
FORMAT() – Formats a value
DATE/TIME FUNCTIONS
-------------------
NOW() – Current date and time
CURDATE() – Current date
CURTIME() – Current time
CURRENT_TIMESTAMP – Current timestamp
CURRENT_DATE – Current date
CURRENT_TIME – Current time
DATE() – Extracts the date part
TIME() – Extracts the time part
YEAR(), MONTH(), DAY(), HOUR(), MINUTE(), SECOND() – Extract parts of a date/time
DATEADD() – Adds a date/time interval
DATEDIFF() – Difference between dates
DATE_FORMAT() – Formats a date
GETDATE() – Current date/time (SQL Server)
MATHEMATICAL FUNCTIONS
----------------------
ABS() – Absolute value
ROUND() – Rounds to the nearest value
FLOOR() – Rounds down
CEILING() – Rounds up
POWER() – Raises a number to a power
SQRT() – Square root
MOD() – Modulus
RAND() – Random number
EXP() – Exponential
LOG() – Logarithm
LN() – Natural log
PI() – π constant
SIN(), COS(), TAN() – Trigonometric functions
CONVERSION FUNCTIONS
--------------------
CAST() – Converts a value to another data type
CONVERT() – Converts a value (SQL Server)
PARSE() – Parses a string into date/number (SQL Server)
TRY_CAST() – Tries to cast, returns NULL if failed
TRY_CONVERT() – Tries to convert, returns NULL if failed
SYSTEM FUNCTIONS
----------------
DATABASE() – Returns current database
USER() – Returns current user
VERSION() – Returns DB version
SESSION_USER – Current session user
SYSTEM_USER – System user
@@IDENTITY – Last inserted identity (SQL Server)
SCOPE_IDENTITY()– Last identity in scope (SQL Server)
LAST_INSERT_ID()– Last inserted ID (MySQL)
WINDOW FUNCTIONS
----------------
ROW_NUMBER() – Row number within partition
RANK() – Ranks with gaps
DENSE_RANK() – Ranks without gaps
NTILE(n) – Divides rows into n groups
LEAD() – Next row value
LAG() – Previous row value
FIRST_VALUE() – First value in window
LAST_VALUE() – Last value in window
SUM(), AVG(), etc. – As window functions with OVER()