
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

Reference card
Returns the current date without the time component. The function is specific to MySQL.
CURRENT_DATE() AS Current_date;
Returns the current date and time from the system. The function is specific to MySQL.
SELECT NOW() AS Current_date;
Returns the current date and time from the system. The function is specific to MySQL.
CURRENT_TIMESTAMP() AS Current_timestamp;
Returns the day of the month for a specified date.
SELECT DAY(date_expression) AS Alias; FROM Table_name;
MONTH(date_expression) AS Alias; FROM Table_name;
Returns the month for a specified date.
DATEDIFF(date_part, start_date, end_date) AS Alias; FROM Table_name;
Calculates the difference between two dates.
Returns the year for a specified date.
SELECT YEAR(date_expression) AS Alias; FROM Table_name;
Adds a specified interval to a date or datetime value.
DATE_ADD(date, INTERVAL value INTERVAL_UNIT) AS Alias; FROM Table_name;
Miscellaneous functions are supporting functions in SQL that can be used with various data types.
Converts a value from its current data type into a specified data type.
SELECT CAST(expression AS datatype) FROM Table_name;
Another function that converts a value from its current data type into a specified data type.
SELECT CONVERT(value, datatype) FROM Table_name;
Returns a specified value if the given expression is null. Otherwise, it returns the value of the expression itself.
SELECT IFNULL(expression, alternative_value) FROM Table_name;
Compares two expressions and returns NULL if they are equal. Otherwise, the first expression is returned.
SELECT NULLIF(expression1, expression2) FROM Table_name;
Determines if an expression is NULL or not. If the expression is NULL, it returns 1. Otherwise , it returns 0.
SELECT ISNULL(expression) FROM Table_name;
Evaluates expressions from left to right, returning the first non- NULL value or NULL if all expressions are NULL.
SELECT COALESCE(value1, value1, ...) FROM Table_name;
Converts a string to uppercase.
SELECT UPPER(string) AS Alias FROM Table_name;
Converts a string to lowercase.
SELECT LOWER(string) AS Alias FROM Table_name;
Removes leading spaces from the left end of a string.
SELECT LTRIM(string) AS Alias FROM Table_name;
Removes trailing spaces from the right end of a string.
SELECT RTRIM(string) AS Alias FROM Table_name;
Extracts a substring from a string.
SELECT SUBSTRING(string, start_position, length) AS Alias FROM Table_name;
Replaces all occurrences of a specified substring within a string with a new substring.
SELECT REPLACE(string, search_string, replacement_string) AS Alias FROM Table_name;
Concatenates or joins multiple strings together.
SELECT CONCAT(string1, string2, ...) AS Alias FROM Table_name;
Determines the length (number of characters) of a string.
LENGTH(string) AS Alias FROM Table_name;
Returns the position (index) of the first occurrence of a substring within a string.
SELECT POSITION(string IN string) AS Alias FROM Table_name;
Extracts a specified number of characters from the leftmost side of a string.
SELECT LEFT(string, length) AS Alias FROM Table_name;
Extracts a specified number of characters from the rightmost side of a string.
SELECT RIGHT(string, length) AS Alias FROM Table_name;
String functions are used to manipulate and format string data types to ensure consistency.
IF(condition, value_if_true, value_if_false) FROM Table_name;
Evaluates a condition and returns a particular value if a condition is TRUE, or another value if a condition is FALSE.
Condition
False
value_if_false value_if_true
True
CASE Case_Expression WHEN value_1 THEN result_ WHEN value_2 THEN result_ WHEN value_N THEN result_N ELSE result END AS Alias_name
A list of values are compared to a given CASE expression.
Simple CASE statement
One or more conditional statements, like the IF and CASE control flow functions, within another conditional statement.
Nested conditional statements
The use of an IF function Nested IF statement: inside of another IF function.
The use of an IF function inside Nested IF and CASE statement: of another CASE function.
Case_Expression result_ = value_
True
False
False
False
Case_Expression result_ = value_
True
result_N
ELSE result
Case_Expression = value_N
True CASE Case_Expression WHEN condition_1 THEN result_ WHEN condition_2 THEN result_ WHEN condition_N THEN result_N ELSE result END AS Alias_name
A list of conditions are evaluated to either TRUE or FALSE.
Searched CASE statement
condition_1 result_
condition_
condition_N
True
False
False
False
result_
True
result_N
ELSE result
True
Datetime functions allow manipulation and calculation of date and time values.
Control flow functions are used to implement conditional logic and control the flow of execution within SQL queries. They allow us to perform different actions or return different values based on specific conditions.