0% found this document useful (0 votes)
5 views

SQL Function CheatSheet 1746267574

This document is a comprehensive cheat sheet for SQL functions, categorized into string functions, aggregate functions, date and time functions, mathematical functions, conditional functions, conversion functions, and window functions. Each category includes various functions, their purposes, and examples of usage. It serves as a quick reference for SQL users to understand and apply these functions effectively.

Uploaded by

ntambatka97
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)
5 views

SQL Function CheatSheet 1746267574

This document is a comprehensive cheat sheet for SQL functions, categorized into string functions, aggregate functions, date and time functions, mathematical functions, conditional functions, conversion functions, and window functions. Each category includes various functions, their purposes, and examples of usage. It serves as a quick reference for SQL users to understand and apply these functions effectively.

Uploaded by

ntambatka97
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
You are on page 1/ 5

SQL Functions Cheat Sheet

String Functions

Function Purpose Example

UPPER() Convert text to uppercase UPPER('hello') → 'HELLO'

LOWER() Convert text to lowercase LOWER('HELLO') → 'hello'

TRIM() Remove spaces from TRIM(' hello ') → 'hello'


start/end
LENGTH() Find length of string LENGTH('SQL') → 3

SUBSTRING() / SUBSTR() Extract part of string SUBSTRING('SQLWorld', 1, 3) → 'SQL'

LEFT() Get leftmost characters LEFT('SQLWorld', 3) → 'SQL'

RIGHT() Get rightmost characters RIGHT('SQLWorld', 5) → 'World'

INSTR() Find position of substring INSTR('SQLWorld', 'World') → 4

POSITION() Find position of substring POSITION('World' IN 'SQLWorld') → 4

CONCAT() Join two strings CONCAT('SQL', 'World') → 'SQLWorld'

REPLACE() Replace part of string REPLACE('SQLWorld', 'SQL', 'Data') →


'DataWorld'

FIND_IN_SET() Find the position of a value FIND_IN_SET('b', 'a,b,c') → 2


inside a comma-separated
string
LPAD() Pad string from the left side LPAD('SQL', 5, '*') → '**SQL'
to a certain length

RPAD() Pad string from the right RPAD('SQL', 5, '*') → 'SQL**'


side to a certain length

FORMAT() Format a number with FORMAT(1234567.89, 2) → '1,234,567.89'


commas and decimal places
(MySQL)
Aggregate & Statistical Functions

Function Purpose Example

COUNT() Number of rows COUNT(*)

SUM() Total sum SUM(salary)

AVG() Average value AVG(salary)

MIN() Minimum value MIN(age)

MAX() Maximum value MAX(age)

VAR_POP() or Population variance VAR_POP(salary) or


VARIANCE() (measure of spread) VARIANCE(salary)

STDDEV_POP() or Population standard STDDEV_POP(salary) or


STDDEV() deviation STDDEV(salary)

CORR() Correlation coefficient CORR(salary, age)


between two columns

COVAR_POP() Population covariance COVAR_POP(salary, age)


between two columns

PERCENTILE_CONT() Continuous percentile PERCENTILE_CONT(0.5)


calculation (for medians, WITHIN GROUP (ORDER BY
etc.) salary)
Date and Time Functions

Function Purpose Example

CURRENT_DATE Today's date CURRENT_DATE

CURRENT_TIMESTAMP Current date + time CURRENT_TIMESTAMP

DATE_PART() Extract part of date DATE_PART('year', CURRENT_DATE)

EXTRACT() Extract year, month, etc. EXTRACT(MONTH FROM CURRENT_DATE)

AGE() Difference between dates AGE('2025-04-01', '2024-04-01') → 1 year

NOW() Current date-time NOW()

TO_CHAR() Format date/time TO_CHAR(NOW(), 'YYYY-MM-DD')

DATE() Extract date part only (no DATE(NOW()) → '2025-04-28'


time)

DATEDIFF() Difference between two DATEDIFF('2025-05-01', '2025-04-28') →


dates (in days)
3

DATE_ADD() Add interval to date DATE_ADD('2025-04-28', INTERVAL 5


DAY) → '2025-05-03'

Mathematical Functions

Function Purpose Example

ROUND() Round number ROUND(3.1415, 2) → 3.14

CEIL() / CEILING() Round up CEIL(3.2) → 4

FLOOR() Round down FLOOR(3.8) → 3

ABS() Absolute value ABS(-5) → 5

POWER() Exponentiation POWER(2,3) → 8

MOD() Remainder MOD(10,3) → 1

SQRT() Square root SQRT(25) → 5


Conditional Functions

Function Purpose Example

CASE WHEN THEN ELSE END If-else logic CASE WHEN salary > 5000
THEN 'High' ELSE 'Low' END

COALESCE() Return first non-null value COALESCE(NULL, NULL,


'default') → 'default'

NULLIF() Returns NULL if values match NULLIF(5,5) → NULL

IFNULL() replaces a NULL with a value IFNULL(NULL, 'default') →


'default'

ISNULL() checks if something is NULL ISNULL(NULL) → 1 and


ISNULL('text') → 0

Conversion and Other Useful Functions

Function Purpose Example

CAST() Change data type CAST('123' AS INT)

CONVERT() Similar to CAST CONVERT(INT, '123')

DISTINCT Remove duplicates SELECT DISTINCT department


FROM employees

GROUP_CONCAT() / Merge values STRING_AGG(name, ',')


STRING_AGG()

JSON_VALUE() Extract value from JSON JSON_VALUE('{"a":1}', '$.a') → 1


Window Functions

Function Purpose Example

Unique row number per ROW_NUMBER() OVER


partition (PARTITION BY dept ORDER
ROW_NUMBER() BY salary DESC)

Ranking (gaps allowed) RANK() OVER (ORDER BY


marks DESC)
RANK()
Ranking (no gaps) DENSE_RANK() OVER (ORDER
BY marks DESC)
DENSE_RANK()
Value from next row LEAD(salary) OVER (ORDER
BY emp_id)
LEAD()
Value from previous row LAG(salary) OVER (ORDER BY
emp_id)
LAG()
Distribute rows into n buckets NTILE(4) OVER (ORDER BY
salary)
NTILE(n)

You might also like