Docsity

ALX_SQL-functions-and-CFC-commands, Cheat Sheet of Computer science

Partial preview of the text

Download ALX_SQL-functions-and-CFC-commands and more Cheat Sheet Computer science in PDF only on Docsity!

SQL functions and CFC commands

Reference card

Returns the current date without the time component. The function is specific to MySQL.

CURRENT_DATE()

SELECT

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()

SELECT

CURRENT_TIMESTAMP() AS Current_timestamp;

Returns the day of the month for a specified date.

SELECT DAY(date_expression) AS Alias; FROM Table_name;

SELECT

MONTH(date_expression) AS Alias; FROM Table_name;

Returns the month for a specified date.

SELECT

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()

SELECT

DATE_ADD(date, INTERVAL value INTERVAL_UNIT) AS Alias; FROM Table_name;

DATEDIFF()

MONTH()

NOW()

DAY()

YEAR()

Miscellaneous functions are supporting functions in SQL that can be used with various data types.

Miscellaneous functions

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;

ISNULL() COALESCE()

IFNULL() NULLIF()

CAST() CONVERT()

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.

SELECT

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;

REPLACE()

SUBSTRING() CONCAT()

LEFT() RIGHT()

LENGTH() POSITION()

LTRIM() RTRIM()

UPPER() LOWER()

String functions are used to manipulate and format string data types to ensure consistency.

String functions

SELECT

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.

IF()

END

Condition

False

START

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.

START

END

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

START

END

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.

Datetime functions

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.

Control flow functions