Standard SQL Functions Cheat Sheet Letter
Standard SQL Functions Cheat Sheet Letter
Fetch all names that start with any letter followed by USEFUL FUNCTIONS NULL NULL
SELECT
Get the remainder of a division: CASE
'atherine':
SELECT MOD(13, 2); vertabelo.com 13 WHEN score >= 90 THEN 'A'
SELECT name
-- result: 1 WHEN score > 60 THEN 'B'
FROM names
ELSE 'F'
WHERE name LIKE '_atherine'; Round a number to its nearest integer: USEFUL FUNCTIONS END AS grade
Fetch all names that end with 'a': SELECT ROUND(1234.56789); COALESCE(x, y, ...) FROM test_results;
SELECT name -- result: 1235 To replace NULL in a query with something meaningful: Here, all students who scored at least 90 will get an A, those
FROM names SELECT with the score above 60 (and below 90) will get a B, and the
Round a number to three decimal places: domain,
WHERE name LIKE '%a'; rest will receive an F.
SELECT ROUND(1234.56789, 3); COALESCE(domain, 'domain missing')
USEFUL FUNCTIONS -- result: 1234.568
PostgreSQL requires the first argument to be of the type
FROM contacts; TROUBLESHOOTING
Get the count of characters in a string: Integer division
numeric – cast the number when needed. domain coalesce
SELECT LENGTH('LearnSQL.com'); When you don't see the decimal places you expect, it means
-- result: 12 LearnSQL.com LearnSQL.com
To round the number up: that you are dividing between two integers. Cast one to
Convert all letters to lowercase: SELECT CEIL(13.1); -- result: 14 NULL domain missing decimal:
SELECT LOWER('LEARNSQL.COM'); SELECT CEIL(-13.9); -- result: -13 CAST(123 AS decimal) / 2
The COALESCE() function takes any number of arguments
-- result: learnsql.com The CEIL(x) function returns the smallest integer not less Division by 0
and returns the value of the first argument that isn't NULL.
Convert all letters to uppercase: than x. In SQL Server, the function is called CEILING(). To avoid this error, make sure that the denominator is not
SELECT UPPER('LearnSQL.com'); equal to 0. You can use the NULLIF() function to replace 0
To round the number down: NULLIF(x, y)
-- result: LEARNSQL.COM with a NULL, which will result in a NULL for the whole
SELECT FLOOR(13.8); -- result: 13 To save yourself from division by 0 errors:
expression:
Convert all letters to lowercase and all first letters to SELECT FLOOR(-13.2); -- result: -14 SELECT
count / NULLIF(count_all, 0)
uppercase (not implemented in MySQL and SQL Server): The FLOOR(x) function returns the greatest integer not last_month,
greater than x. this_month, Inexact calculations
SELECT INITCAP('edgar frank ted cODD');
this_month * 100.0 If you do calculations using real (floating point) numbers,
-- result: Edgar Frank Ted Codd
To round towards 0 irrespective of the sign of a number: / NULLIF(last_month, 0) you'll end up with some inaccuracies. This is because this
Get just a part of a string: SELECT TRUNC(13.5); -- result: 13 type is meant for scientific calculations such as calculating
AS better_by_percent
SELECT SUBSTRING('LearnSQL.com', 9); SELECT TRUNC(-13.5); -- result: -13 the velocity. Whenever you need accuracy (such as dealing
FROM video_views;
-- result: .com TRUNC(x) works the same way as CAST(x AS with monetary values), use the decimal / numeric type (or
SELECT SUBSTRING('LearnSQL.com', 0, 6); integer). In MySQL, the function is called TRUNCATE(). last_month this_month better_by_percent money if available).
-- result: Learn Errors when rounding with a specified precision
To get the absolute value of a number: 723786 1085679 150.0
Replace part of a string: Most databases won't complain, but do check the
SELECT ABS(-12); -- result: 12 0 178123 NULL
SELECT REPLACE('LearnSQL.com', 'SQL', documentation if they do. For example, if you want to specify
'Python'); To get the square root of a number: The NULLIF(x, y) function will return NULL if x is the the rounding precision in PostgreSQL, the value must be of
-- result: LearnPython.com SELECT SQRT(9); -- result: 3 same as y, else it will return the x value. the numeric type.