SQL Advanced Cheatsheet
SQL Advanced Cheatsheet
String Functions
FROM table1
FULL OUTER JOIN table2 ON table1.common_column = table2.common_column; specified conditions
SELECT CASE
WHEN condition THEN result
ELSE default_result
String functions help manipulate and retrieve information from text data
. CONCAT - Combines two or more strings into a single string Window Functions END AS alias_name
FROM table_name;
SELECT CONCAT (string1, string2); . NULLIF - Returns `NULL` if two expressions are equal; otherwise, it returns
. SUBSTRING - Extracts a specific portion of a string based on starting position and length Window functions perform calculations across a defined range of rows that are the first expression
SELECT SUBSTRING (column_name, start_position, length) FROM table_name; related to the current row SELECT NULLIF (column1, column2) FROM table_name;
. LENGTH - Returns the total number of characters in a string, including spaces . COALESCE - Returns the first non-null value from a list of expressions or columns
. ROW_NUMBER - Assigns a unique sequential number to each row within
SELECT LENGTH (column_name) FROM table_name; SELECT COALESCE (column1, column2, default_value) FROM table_name;
a partition of the result set
. TRIM - Removes leading and trailing spaces or specific characters from a string
SELECT ROW_NUMBER () OVER (PARTITION BY column_name ORDER BY column_name)
SELECT TRIM ( ' ' FROM column_name ) FROM table_name; AS row_num FROM table_name;
. RANK - Assigns a rank to each row within a partition, allowing for gaps in Set Operations
the ranking when there are ties
Date and Time Functions SELECT RANK () OVER (PARTITION BY column_name ORDER BY column_name)
AS rank_num FROM table_name;
Set operations combine results from two or more queries into a single dataset,
with or without duplicate rows.
. LEAD - Retrieves the value of the next row in the result set relative to the
Date and time functions allow operations on and extraction of date/time values current row . UNION - Combines the results of two queries into one, excluding duplicates
. NOW - Returns the current date and time of the system or database. SELECT LEAD (column_name) OVER (ORDER BY column_name) FROM table_name; SELECT column_name FROM table_name1
UNION
SELECT NOW (); . LAG - Retrieves the value of the previous row in the result set relative to
SELECT column_name FROM table_name2;
. DATEDIFF - Calculates the difference in days between two dates the current row
SELECT DATEDIFF (date1, date2);
. UNION ALL - Combines the results of two queries into one, including duplicates
SELECT LAG (column_name) OVER (ORDER BY column_name) FROM table_name;
. YEAR - Extracts the year part of a date value SELECT column_name FROM table_name1
UNION ALL
SELECT YEAR (column_name) FROM table_name;
Common Table Expressions (CTEs)
SELECT column_name FROM table_name2;
. EXTRACT - Retrieves a specific component, such as year or month, from a date or time
SELECT EXTRACT (part FROM date_column) FROM table_name;
. INTERSECT - Returns rows that are common in the results of both queries
SELECT column_name FROM table_name1
CTEs define temporary result sets that are more reusable within a query INTERSECT
SELECT column_name FROM table_name2;
JSON Functions . BASIC CTE - Simplifies complex queries by defining a temporary result set with a name
WITH cte_name AS (SELECT column_name FROM table_name WHERE condition)
. EXCEPT / MINUS - Returns rows that are present in the first query but not in the second
SELECT * FROM cte_name; SELECT column_name FROM table_name1
. RECURSIVE CTE- Allows hierarchical queries by iteratively processing data until EXCEPT
JSON functions enable querying and manipulation of JSON data stored in db columns SELECT column_name FROM table_name2;
a specified condition is met
. JSON_OBJECT - Creates a JSON object using key-value pairs provided in the query WITH RECURSIVE cte_name AS (
SELECT JSON_OBJECT ('key', value); SELECT column_name
SAI RAAM
. JSON_EXTRACT - Extracts specific values from a JSON object based on a given path FROM table_name WHERE condition
UNION ALLSELECT column_name FROM cte_name )
SELECT JSON_EXTRACT (column_name, '$.path') FROM table_name; SELECT * FROM cte_name;
YOUR FRIENDLY NEIGBORHOOD DATA GUY Follow for more such content
srinrealyf.in