โ† Back to list

๐Ÿ—„๏ธ 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โ€ฆ

raw-hitt ยท 2026-02-01 20:06 ยท 94 claps ยท 3.2 min read
#sql #mysql #dml #postgresql #transactions
Open on Medium โ†—

๐Ÿ—„๏ธ 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 results
  • LIMIT โ†’ restrict row count
  • DISTINCT โ†’ 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

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 SELECT to advanced CTEs and Window 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

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 โ€”

[embed]Ace Your Web Dev Interview: 5 Essential SQL Query Types You Need to Master In the world of web development, particularly for backend and full-stack roles, proficiency in SQL is oftenโ€ฆmedium.com

[embed]Nail Your Next Interview: 20 Must-Know .NET Core and SQL Questions Here are top 20 questions asked in .Net and SQL interviews. 1- What is the difference between authorization and authenโ€ฆmedium.com


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
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