0% found this document useful (0 votes)
9 views4 pages

SQL Master Key

The document provides a comprehensive overview of various SQL functions, detailing their purposes and use cases along with examples. It covers string manipulation functions like LOWER(), UPPER(), and TRIM(), as well as aggregate functions such as COUNT(), SUM(), and AVG(). Additionally, it includes mathematical functions and date-related functions, illustrating their usage with SQL queries and expected outputs.

Uploaded by

rilojij814
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)
9 views4 pages

SQL Master Key

The document provides a comprehensive overview of various SQL functions, detailing their purposes and use cases along with examples. It covers string manipulation functions like LOWER(), UPPER(), and TRIM(), as well as aggregate functions such as COUNT(), SUM(), and AVG(). Additionally, it includes mathematical functions and date-related functions, illustrating their usage with SQL queries and expected outputs.

Uploaded by

rilojij814
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

SQL

Function Purpose / Use Case Example (SQL/Python)

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

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

SUBSTRING() Extracts part of string SUBSTRING('VIVEK', 1, 3) → 'VIV'

LENGTH() Returns number of characters LENGTH('hello') → 5

Removes whitespace from both


TRIM() TRIM(' hello ') → 'hello'
ends

Removes whitespace from the


LTRIM() LTRIM(' hello') → 'hello'
left

Removes whitespace from the


RTRIM() RTRIM('hello ') → 'hello'
right

REPLACE() Replaces part of the string REPLACE('abcabc', 'a', 'x') → 'xbcxbc'

CONCAT() Joins two or more strings CONCAT('Hello', 'World') → 'HelloWorld'

CHARINDEX() / INSTR('OpenAI', 'AI') → 5 (1-based index


Finds index of substring
INSTR() in SQL)

LEFT() Gets leftmost characters LEFT('HELLO', 2) → 'HE'

RIGHT() Gets rightmost characters RIGHT('HELLO', 2) → 'LO'

Formats string (esp.


FORMAT() FORMAT(12345, '###,###') → '12,345'
dates/numbers)

REVERSE() Reverses the string REVERSE('abc') → 'cba'

Replaces NULL with a default


ISNULL() / IFNULL() IFNULL(NULL, 'N/A') → 'N/A'
value

Converts data type (e.g., string


CAST() / CONVERT() CAST('123' AS INT)
to int)
SQL

Function Purpose / Use Case Example (SQL/Python)

Returns ASCII code of first


ASCII() ASCII('A') → 65
character

Function Purpose / Use Case Example (SQL) Output

Counts the number


COUNT() SELECT COUNT(*) FROM Students; Total rows
of rows

Adds all numeric SELECT SUM(salary) FROM Total


SUM()
values Employees; salary

Calculates average of Average


AVG() SELECT AVG(marks) FROM Scores;
numeric values marks

Returns the smallest Youngest


MIN() SELECT MIN(age) FROM Users;
value age

Returns the largest SELECT MAX(salary) FROM Highest


MAX()
value Employees; salary

GROUP_CONCAT() / Concatenates strings SELECT GROUP_CONCAT(name)


Name list
STRING_AGG() in group FROM Students;

Sample variance of SELECT VAR_SAMP(salary) FROM


VAR_SAMP() Variance
numeric values Employees;

Standard deviation of SELECT STDDEV(marks) FROM Std


STDDEV()
values Scores; deviation

Counts unique values SELECT COUNT(DISTINCT Unique


COUNT(DISTINCT)
only department) FROM Employees; depts
SQL

Function Description Example Query Output

Returns the absolute (positive)


ABS(x) SELECT ABS(-15) AS Result; 15
value

CEIL(x) / Returns the smallest integer ≥


SELECT CEIL(15.3) AS Result; 16
CEILING(x) x

FLOOR(x) Returns the largest integer ≤ x SELECT FLOOR(15.7) AS Result; 15

SELECT ROUND(15.678, 2) AS
ROUND(x, d) Rounds x to d decimal places 15.68
Result;

POWER(x, y) Returns x raised to power y SELECT POWER(2, 3) AS Result; 8

SQRT(x) Returns square root of x SELECT SQRT(25) AS Result; 5

EXP(x) Returns e raised to power x SELECT EXP(1) AS Result; 2.718...

LOG(x) Natural log (base e) SELECT LOG(10) AS Result; 2.302...

LOG10(x) Logarithm base 10 SELECT LOG10(100) AS Result; 2

MOD(x, y) Returns remainder (x ÷ y) SELECT MOD(17, 5) AS Result; 2

PI() Returns value of π SELECT PI() AS Result; 3.141593

SIN(x) Returns sine of x (radians) SELECT SIN(PI()/2) AS Result; 1

COS(x) Returns cosine of x (radians) SELECT COS(0) AS Result; 1

TAN(x) Returns tangent of x (radians) SELECT TAN(PI()/4) AS Result; 1

Returns a random number (0– e.g.,


RAND() SELECT RAND() AS Result;
1) 0.3746
SQL

Function Description Example ('1998-05-21') Output

YEAR(date) Gets the year YEAR('1998-05-21') 1998

Gets the month


MONTH(date) MONTH('1998-05-21') 5
number

DAY(date) or Gets the day of


DAY('1998-05-21') 21
DAYOFMONTH(date) month

Gets the day


DAYNAME(date) DAYNAME('1998-05-21') Thursday
name

Gets the month


MONTHNAME(date) MONTHNAME('1998-05-21') May
name

Current date & 2025-08-26


NOW() NOW()
time [Link]

CURDATE() Current date only CURDATE() 2025-08-26

CURTIME() Current time only CURTIME() [Link]

DATEDIFF('2025-08-26','1998-
DATEDIFF(date1, date2) Difference in days 9962
05-21')

ADDDATE(date, INTERVAL n ADDDATE('1998-05-21',


Add days 1998-05-31
DAY) INTERVAL 10 DAY)

SUBDATE(date, INTERVAL n SUBDATE('1998-05-21',


Subtract days 1998-05-16
DAY) INTERVAL 5 DAY)

DATE_FORMAT(date, '%Y- DATE_FORMAT('1998-05-


Format date 21-May-1998
%m-%d') 21','%d-%M-%Y')

You might also like