🗄️ SQL – General Interview Cheat Sheet
Revisão rápida para entrevistas técnicas (Backend / Java Senior). Foque em conceitos + exemplos
simples. SQL costuma ser decisivo.
1️⃣ Basic Concepts
Tables & Rows
● Table: structured data storage
● Row: single record
● Column: attribute of the record
Primary Key (PK)
● Uniquely identifies a row
● Cannot be NULL
● One per table
Foreign Key (FK)
● References a primary key in another table
● Enforces referential integrity
2️⃣ Basic Commands (DDL vs DML)
DDL (Data Definition Language)
● CREATE
● ALTER
● DROP
● TRUNCATE
DML (Data Manipulation Language)
● SELECT
● INSERT
● UPDATE
● DELETE
3️⃣ DELETE vs TRUNCATE vs DROP
● DELETE
○ Removes rows
○ Can use WHERE
○ Can be rolled back (transactional)
● TRUNCATE
○ Removes all rows
○ Faster than DELETE
○ Cannot use WHERE
○ Usually not transactional
● DROP
○ Removes the table structure entirely
4️⃣ SELECT Basics
SELECT column1, column2
FROM table
WHERE condition;
● WHERE filters rows
● SELECT * selects all columns (avoid in production)
5️⃣ WHERE vs HAVING
● WHERE
○ Filters rows before aggregation
● HAVING
○ Filters groups after aggregation
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
6️⃣ GROUP BY & Aggregation
Common aggregate functions:
● COUNT()
● SUM()
● AVG()
● MIN()
● MAX()
Rule:
● Columns in SELECT must appear in GROUP BY or be aggregated
7️⃣ JOIN Types
INNER JOIN
● Returns matching records only
LEFT JOIN
● Returns all rows from left table
● NULL when no match on right
RIGHT JOIN
● Opposite of LEFT JOIN
FULL JOIN
● Returns all rows from both tables
8️⃣ NULL Handling
● NULL means unknown, not zero or empty
Common functions:
● IS NULL / IS NOT NULL
● COALESCE(value, default)
9️⃣ Indexes
● Improve SELECT performance
● Speed up searches and joins
Trade-offs:
● Slow down INSERT/UPDATE/DELETE
● Use only when needed
🔟 ACID vs BASE
ACID (Relational DBs)
● Atomicity
● Consistency
● Isolation
● Durability
BASE (NoSQL)
● Basically Available
● Soft state
● Eventually consistent
1️⃣1️⃣ Transactions
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
● COMMIT saves changes
● ROLLBACK undoes changes
1️⃣2️⃣ Common Interview Queries
Find duplicates
SELECT column, COUNT(*)
FROM table
GROUP BY column
HAVING COUNT(*) > 1;
Top N results
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 5;
Count records per category
SELECT category, COUNT(*)
FROM products
GROUP BY category;
🎯 Interview One-Liners
● "Indexes improve read performance but slow down writes."
● "HAVING filters groups, WHERE filters rows."
● "LEFT JOIN keeps all rows from the left table."
● "NULL means unknown, not empty."
✅ Revise this cheat sheet 10–15 minutes before the interview.