Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

SQL Advanced Cheatsheet

This document is a cheat sheet for advanced SQL operations, covering join operations, aggregate functions, string functions, date and time functions, window functions, set operations, common table expressions (CTEs), and JSON functions. It provides syntax examples for each function and operation, facilitating complex queries and data manipulation. The cheat sheet serves as a quick reference for users looking to enhance their SQL skills.

Uploaded by

Romissa Osama
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

SQL Advanced Cheatsheet

This document is a cheat sheet for advanced SQL operations, covering join operations, aggregate functions, string functions, date and time functions, window functions, set operations, common table expressions (CTEs), and JSON functions. It provides syntax examples for each function and operation, facilitating complex queries and data manipulation. The cheat sheet serves as a quick reference for users looking to enhance their SQL skills.

Uploaded by

Romissa Osama
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

SQL ADVANCED

Join Operations Mathematical Functions


Cheatsheeeeet
Join operations combine rows from two or more tables based on a related column, Mathematical functions are used to perform arithmetic and advanced numeric
allowing complex queries across multiple datasets operations on data
Aggregate Functions . INNER JOIN - Returns rows that have matching values in both tables . ROUND - Rounds a numeric value to a specified number of decimal places
SELECT table1.column_name, table2.column_name SELECT ROUND (column_name, decimal_places) FROM table_name;
A B
Aggregate functions perform calculations on a set of rows and return a single value,
FROM table1 . POWER - Calculates the result of raising a number to a specified power
INNER JOIN table2 ON table1.common_column = table2.common_column;
often used with GROUP BY SELECT POWER (column_name, exponent) FROM table_name;
. LEFT JOIN - Returns all rows from the left table, and the matching rows . SQRT - Returns the square root of a numeric value
. COUNT - Counts the number of rows in a column or table from the right table; unmatched rows from the right table are null
SELECT SQRT (column_name) FROM table_name;
SELECT COUNT (column_name) FROM table_name; SELECT table1.column_name, table2.column_name A B . MOD - Finds the remainder when one number is divided by another
. SUM - Calculates the total sum of numeric values in a column FROM table1
INNER JOIN table2 ON table1.common_column = table2.common_column; SELECT MOD (column_name, divisor) FROM table_name;
SELECT SUM (column_name) FROM table_name;
. AVG - Returns the average of numeric values in a column . RIGHT JOIN - Returns all rows from the right table, and the matching
rows from the left table; unmatched rows from the left table are null
SELECT AVG (column_name) FROM table_name;
. MIN - Finds the smallest value in a column SELECT table1.column_name, table2.column_name
FROM table1
A B Logical Functions
SELECT MIN (column_name) FROM table_name; RIGHT JOIN table2 ON table1.common_column = table2.common_column;
. MAX - Finds the largest value in a column
. FULL OUTER JOIN - Returns rows when there is a match in either table; Logical functions evaluate conditions and provide outputs based on specific criteria
SELECT MAX (column_name) FROM table_name; unmatched rows are filled with nulls. or handle special values like NULL
SELECT table1.column_name, table2.column_name srinrealyf.in A B . CASE - Implements conditional logic to return different values based on

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

You might also like