SQL KEY POINTS
🧠 SQL Cheat Sheet (For Data Science & Analytics)
🏁 1️⃣ Basic SQL Commands
| Command | Description |
|---|---|
SELECT | Choose columns to display |
FROM | Choose table |
WHERE | Filter rows |
GROUP BY | Aggregate rows |
HAVING | Filter after aggregation |
ORDER BY | Sort results |
LIMIT / TOP | Restrict number of rows |
✅ Example:
📋 2️⃣ Data Definition Language (DDL)
| Command | Purpose |
|---|---|
CREATE TABLE | Create new table |
DROP TABLE | Delete table |
ALTER TABLE | Modify table structure |
TRUNCATE TABLE | Delete all rows (keep structure) |
Example:
🧩 3️⃣ Data Manipulation Language (DML)
| Command | Purpose |
|---|---|
INSERT INTO | Add new record |
UPDATE | Modify existing record |
DELETE | Remove record |
Example:
🔍 4️⃣ Filtering Data
| Operator | Meaning |
|---|---|
= | Equal |
<> / != | Not equal |
> < >= <= | Comparisons |
BETWEEN a AND b | Range |
IN (...) | Match a list |
LIKE | Pattern matching |
IS NULL / IS NOT NULL | Null check |
Example:
📊 5️⃣ Aggregate Functions
| Function | Description |
|---|---|
COUNT() | Number of rows |
SUM() | Sum of values |
AVG() | Average |
MIN() | Minimum |
MAX() | Maximum |
Example:
🔗 6️⃣ SQL Joins
| Type | Description |
|---|---|
INNER JOIN | Matching rows only |
LEFT JOIN | All from left + matches |
RIGHT JOIN | All from right + matches |
FULL JOIN | All rows from both sides |
CROSS JOIN | Cartesian product |
Example:
🧮 7️⃣ Set Operations
| Operation | Description |
|---|---|
UNION | Combine unique rows |
UNION ALL | Combine all rows (duplicates allowed) |
INTERSECT | Common rows |
EXCEPT / MINUS | Rows in first not in second |
🧠 8️⃣ Subqueries
Subquery in WHERE:
Subquery in FROM:
🧱 9️⃣ CASE WHEN (Conditional Logic)
📆 🔟 Date & Time Functions
| Function | Description |
|---|---|
NOW() / CURRENT_TIMESTAMP | Current date & time |
DATE() | Extract date |
YEAR(date) | Year |
MONTH(date) | Month |
DATEDIFF(d1, d2) | Difference in days |
DATE_ADD() / DATE_SUB() | Add/Subtract days |
Example:
📈 11️⃣ Window (Analytic) Functions
Powerful for ranking, running totals, and partitioned calculations.
🧩 Syntax:
Examples:
a) Row Number:
b) Rank & Dense Rank:
c) Running Total:
d) Percent Rank:
🧩 12️⃣ Common Table Expressions (CTEs)
🧮 13️⃣ String Functions
| Function | Example | Output |
|---|---|---|
CONCAT(a,b) | 'Data', 'Science' | DataScience |
UPPER(col) | 'john' | JOHN |
LOWER(col) | 'JOHN' | john |
SUBSTRING(col, start, len) | 'Analytics', 1, 4 | Anal |
LENGTH(col) | 'Data' | 4 |
TRIM(col) | ' Data ' | 'Data' |
REPLACE(col, 'a', 'x') | 'data' | 'dxtx' |
🧩 14️⃣ Numeric Functions
| Function | Description |
|---|---|
ROUND(col, n) | Round value |
CEIL(col) / FLOOR(col) | Round up/down |
ABS(col) | Absolute value |
POWER(col, n) | Exponent |
MOD(a,b) | Remainder |
🧠 15️⃣ Advanced Analytics SQL
🔹 Correlation:
🔹 Top N per Group:
🔹 Year-over-Year (YoY) Growth:
💾 16️⃣ Table Constraints
| Constraint | Description |
|---|---|
PRIMARY KEY | Unique & not null |
FOREIGN KEY | Reference another table |
UNIQUE | All values unique |
CHECK | Must satisfy condition |
DEFAULT | Assign default value |
NOT NULL | Cannot be null |
📂 17️⃣ Indexes
→ Speeds up queries, but slows down inserts/updates.
🔐 18️⃣ Views
Use views to simplify complex queries.
🧩 19️⃣ Data Export / Import
| Command | Description |
|---|---|
SELECT INTO new_table FROM ... | Copy data |
INSERT INTO ... SELECT ... | Copy data to existing table |
LOAD DATA INFILE | Import CSV |
COPY FROM (Postgres) | Import data file |
🧠 20️⃣ Interview Key Points
✅ SQL is declarative — you tell what you want, not how.
✅ Use CTEs + Window Functions for cleaner analytics queries.
✅ For Data Science, focus on:
-
Aggregations
-
Joins
-
Subqueries
-
Window functions
-
CASE logic
-
Date handling
-
Ranking / Percentiles
Comments
Post a Comment