SQL_Cheat_Sheet_Updated
SQL_Cheat_Sheet_Updated
Basic Queries
SELECT column_name FROM table_name WHERE condition - Retrieves specific data.
Joins
INNER JOIN - Returns only matching records from both tables.
LEFT JOIN - Returns all records from the left table and matched records from the right.
RIGHT JOIN - Returns all records from the right table and matched records from the left.
FULL OUTER JOIN - Returns all records when there is a match in either table.
Aggregations
COUNT(column) - Counts the number of rows.
SUM(column) - Sums up all values in a column.
AVG(column) - Returns the average value of a column.
MIN(column) - Returns the smallest value in a column.
MAX(column) - Returns the largest value in a column.
Subqueries
SELECT * FROM table1 WHERE column IN (SELECT column FROM table2 WHERE condition) - Retrieves
data using a nested query.
Window Functions
ROW_NUMBER() OVER (PARTITION BY column ORDER BY column) - Assigns a unique number to rows.
RANK() - Assigns a rank to rows within a partition.
DENSE_RANK() - Assigns a rank to rows without gaps.
LEAD() - Accesses data from the next row.
LAG() - Accesses data from the previous row.