๐๏ธ The Ultimate SQL Cheat Sheet (With Examples)
Structured Query Language (SQL) is the backbone of working with relational databases. Whether youโre a beginner or an advanced developerโฆ
๐๏ธ The Ultimate SQL Cheat Sheet (With Examples)
Structured Query Language (SQL) is the backbone of working with relational databases. Whether youโre a beginner or an advanced developer, having a cheat sheet helps you quickly recall commands, patterns, and functions when writing queries.
In this article, weโll walk through a categorized SQL Cheat Sheet with definitions and examples to strengthen your understanding.

๐น 1. Basic SQL Commands
These are the building blocks of SQL.
- SELECT โ fetches data
SELECT * FROM employees;
- INSERT INTO โ inserts data
INSERT INTO employees (name, role) VALUES ('John Doe', 'Developer');
- UPDATE โ modifies existing data
UPDATE employees SET role = 'Manager' WHERE id = 1;
- DELETE โ removes data
DELETE FROM employees WHERE id = 1;
๐น 2. Filtering Data
Use conditions to narrow results.
SELECT * FROM employees WHERE role = 'Developer';
๐น 3. Sorting and Limiting Data
ORDER BYโ sort resultsLIMITโ restrict row countDISTINCTโ remove duplicates
SELECT DISTINCT role FROM employees ORDER BY role LIMIT 5;
๐น 4. Aggregating Data
Perform calculations on sets of rows.
SELECT COUNT(*) AS total, AVG(salary) AS avg_salary
FROM employees
GROUP BY role
HAVING AVG(salary) > 50000;
๐น 5. Joins
Combine data across tables.
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;
Types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN, SELF JOIN
๐น 6. Subqueries
Queries inside other queries.
SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
๐น 7. Set Operations
Combine multiple queries.
SELECT name FROM employees
UNION
SELECT name FROM contractors;
๐น 8. Indexing
Speed up queries.
CREATE INDEX idx_name ON employees(name);
DROP INDEX idx_name;

Leave a clap if this article is helpful to you
๐น 9. Transactions
Ensure data consistency.
BEGIN TRANSACTION;
UPDATE employees SET salary = salary * 1.1 WHERE dept_id = 2;
COMMIT;
Transaction with Rollback :
START TRANSACTION;
-- Step 1: Insert a new product
INSERT INTO products (product_id, product_name, price)
VALUES (101, 'Smartphone', 599.99);
-- Step 2: Update the price of an existing product
UPDATE products
SET price = 15.00
WHERE product_name = 'T-Shirt';
-- At this point, the changes are not yet permanent.
-- You can now decide to either COMMIT or ROLLBACK.
-- Example of a successful transaction:
-- COMMIT; -- This would make the insert and update permanent.
-- Example of rolling back:
ROLLBACK; -- This discards all changes made since START TRANSACTION.
๐น 10. Views
Save reusable queries as virtual tables.
CREATE VIEW high_paid AS
SELECT name, salary FROM employees WHERE salary > 60000;
๐น 11. Data Modification (Conditions)
Filter with logic.
SELECT * FROM employees
WHERE role LIKE 'Dev%' AND salary BETWEEN 40000 AND 80000;
๐น 12. Common Table Expressions (CTE)
Reusable query blocks.
WITH DeptAvg AS (
SELECT dept_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY dept_id
)
SELECT * FROM employees e
JOIN DeptAvg d ON e.dept_id = d.dept_id
WHERE e.salary > d.avg_salary;
๐น 13. Window Functions
Powerful analytics.
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
๐น 14. Date and Time Functions
Work with timestamps.
SELECT GETDATE(); -- Current date
SELECT DATEDIFF(year, hire_date, GETDATE()) AS years_worked FROM employees;
๐น 15. Triggers
Automate actions.
CREATE TRIGGER trg_salary_update
AFTER UPDATE ON employees
FOR EACH ROW
INSERT INTO audit_log (emp_id, action, updated_at)
VALUES (NEW.id, 'Salary updated', NOW());
๐น 16. Conditional Logic
Handle conditional cases.
SELECT name,
CASE
WHEN salary > 70000 THEN 'High'
WHEN salary BETWEEN 40000 AND 70000 THEN 'Medium'
ELSE 'Low'
END AS salary_band
FROM employees;
๐ฏ Key Takeaways
- SQL is versatile: from simple
SELECTto advancedCTEsandWindow Functions. - Always use indexes wisely for performance.
- Transactions and Triggers help in maintaining data integrity.
- Conditional logic + aggregation gives deeper insights into your data.
โ This SQL Cheat Sheet is not just for memorization but also for hands-on practice. The more queries you write, the better youโll get at spotting patterns and using SQL effectively.

If this Article was helpful for you, leave a clap and ๐ Follow me on Medium for more informational articles like this. ๐จโ๐ปConnect with me on LinkedIn & Git
Related Articles โ
๋ฉํ๋ฐ์ดํฐ
- post_id
- e8ffe5f719f7
- slug
- ๏ธ-the-ultimate-sql-cheat-sheet-with-examples-e8ffe5f719f7
- url
- https://medium.com/@rp99452/%EF%B8%8F-the-ultimate-sql-cheat-sheet-with-examples-e8ffe5f719f7
- canonical_url
- https://medium.com/@rp99452/%EF%B8%8F-the-ultimate-sql-cheat-sheet-with-examples-e8ffe5f719f7
- author_url
- https://medium.com/@rp99452
- status
- ok
- fetched_at
- 2026-06-10 21:21:38