SAL Cheat Sheet for Data Analysts
| “SELECT statement: Retrieve Data
used +o cetrieve data from one or more tables
“Specifies columns to be retrieved from a table
Basic SELECT statement
SELECT columni, column2
FROM table_name;
Select all columns
SELECT *
FROM +able_name;
Alias for column names
SELECT columni AS alias_name
FROM table_name;
DISTINCT : Retrieve unique values
SELECT DISTINCT column
FROM table_name;
-used to filter records based on Specified conditions
- allows se_of toni (AND, ORD. §
multiple conditions-IN: Filter by o list of values
LIKE: Pattern matching,
SELECT column
WHERE columni LIKE ‘pattern%*;
NULL values
SELECT column
FROM +able_name
WHERE column TS NULL}“sorts the result set in ascending or descending order
ified_columns
Ascending order
SELECT columni, column2
FROM +able_name
ORDER BY columni ASC;
Descending order
SELECT columni, column2
FROM table_name
ORDER BY columni DESC;
— Qroups rows based on specified columns for aggregate
functions like COUNT, SUM, AVG, MAX, MIN,
~ HAVING clause filters after grouping,
Bosic aggregation (COUNT, SUM, AVG, MAX, MIN)
SELECT COUNT (column i), AVG(column2)
FROM +able-name
GROUP BY column3;
HAVING: Filter after grouping,
SELECT column, AVGCcolumn2) AS ava valueFROM +able_name
GROUP BY column
HAVING AVG (column2) > threshold;
JOIN: Combining Tables _
“Combines rows from two or more tables based on a related
column
“INNER JOLN ceturns matching rows; LEFT JOIN ceturns all
Tows From the left table and motching rewS From the cignt
INNER JOIN
SELECT tablel.columni, table2.column2
FROM tablet
INNER JOIN +table2 ON table. common column = toble2.common—
column;
LEFT JOIN
SELECT tablei.columni, table2. colurmn2
FROM tablet
LEFT JOIN +table2 ON tablel.common—column = toble2.common—
column;
SUBQUERIES : Nested Queri
“allows embedding one query inside another
~Suloqueries can be used in SELECT, FROM, WHERE, andother clauses
Subquery, in SELECT statement
SELECT cotumni, (SELECT AVG Ccolumn2) FROM table2) AS
ava,-value
FROM tablet;
Suloquery in WHERE clause
SELECT columnt
FROM tabled
WHERE column2 > (SELECT AVG Ccotumn2? FROM tablet);
“restricts the number of cows returned in the result set
Limit the number of rows
SELECT columni, column2
FROM table name
LIMIT 10;
“Single-line comments (--) and multi-line comments (/#*/)
Provide documentation and explanations in SQL code
Single - line commentMulti-line comment
Z* This is a
Multi-line comment */