π οΈ SQL Cheat Sheet: Your Go-To Quick Reference Guide
π Introduction
QL Cheat Sheet: SQL (Structured Query Language) is the backbone of database management. Whether you are new to SQL or an experienced database professional, having a handy reference can make working with databases easier and more efficient. This cheat sheet provides a detailed yet easy-to-understand overview of essential SQL commands, helping you retrieve, manipulate, and manage data effectively.

π Retrieving Data with SQL (SELECT Statements)
π SELECT β Fetch Specific Data
The SELECT
statement is used to fetch data from a table. You can specify which columns you want to retrieve.
SELECT column1, column2 FROM table_name;
β This will return only the specified columns from the given table.
π SELECT * β Retrieve Everything
If you want to see all the data from a table, use SELECT *
.
SELECT * FROM table_name;
β Be cautious with this! Fetching all columns can slow down queries if the table is large.
π DISTINCT β Remove Duplicate Entries
To return only unique values, use DISTINCT
.
SELECT DISTINCT column_name FROM table_name;
β This ensures that duplicate values in the selected column are not included.
π WHERE β Filter Data Based on Conditions
To refine your query results, use the WHERE
clause.
SELECT column_name FROM table_name WHERE condition;
β
Example: WHERE age > 30
will return only rows where the age is greater than 30.
π Sorting and Limiting Results
π ORDER BY β Sort Your Data
To sort your results, use ORDER BY
with ASC
(ascending) or DESC
(descending).
SELECT column_name FROM table_name ORDER BY column_name ASC|DESC;
β
Example: ORDER BY price DESC
will show the most expensive items first.
π LIMIT β Control How Many Rows Are Displayed
If you only need a certain number of records, use LIMIT
.
SELECT column_name FROM table_name LIMIT number;
β
Example: LIMIT 5
will show only the top 5 results.
π Joining Tables: Merging Data from Multiple Sources
π INNER JOIN β Get Matching Records from Two Tables
The INNER JOIN
retrieves only the rows that have matching values in both tables.
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
β Use this when you only need records that exist in both tables.
π LEFT JOIN β Keep All Left Table Records
A LEFT JOIN
returns all rows from the first (left) table and only matching rows from the second (right) table.
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
β Use this if you want all records from the left table, even if there’s no match in the right table.
π RIGHT JOIN β Keep All Right Table Records
A RIGHT JOIN
is the oppositeβit keeps all rows from the right table and only matching rows from the left table.
SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
β This ensures no records from the right table are omitted.
βοΈ Modifying Data (DML – Data Manipulation Language)
π INSERT β Add New Data
To add a new row into a table, use INSERT
.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
β
Example: INSERT INTO students (name, age) VALUES ('John Doe', 25);
π UPDATE β Change Existing Data
To modify data in a table, use UPDATE
.
UPDATE table_name SET column1 = value1 WHERE condition;
β
Example: UPDATE employees SET salary = 60000 WHERE id = 3;
π DELETE β Remove Records
To remove specific records, use DELETE
.
DELETE FROM table_name WHERE condition;
β
Example: DELETE FROM customers WHERE last_purchase < '2020-01-01';
π Summarizing Data with Aggregate Functions
π’ COUNT β Count the Number of Records
To count how many rows exist in a table:
SELECT COUNT(column_name) FROM table_name;
β
Example: SELECT COUNT(*) FROM orders;
returns the total number of orders.
β SUM β Get the Total Value
To get the sum of a numeric column:
SELECT SUM(column_name) FROM table_name;
β
Example: SELECT SUM(sales) FROM revenue;
gives the total sales amount.
π AVG β Calculate the Average
To find the average of a numeric column:
SELECT AVG(column_name) FROM table_name;
β
Example: SELECT AVG(price) FROM products;
gives the average product price.
πΌ MAX & π½ MIN β Find Highest and Lowest Values
SELECT MAX(column_name) FROM table_name;
SELECT MIN(column_name) FROM table_name;
β
Example: SELECT MAX(salary) FROM employees;
returns the highest salary.
π οΈ Advanced SQL Techniques
π GROUP BY β Group Data for Aggregation
To group data and perform calculations on each group:
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
β
Example: SELECT department, COUNT(*) FROM employees GROUP BY department;
π HAVING β Filter Aggregate Results
To filter results after aggregation:
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING COUNT(*) > 1;
β
Example: HAVING COUNT(*) > 10
ensures only groups with more than 10 entries appear.
π Subqueries β Queries Inside Queries
To use one query inside another:
SELECT column_name FROM table_name WHERE column_name = (SELECT column_name FROM another_table);
β Example: Find employees earning more than the company average salary.
π CASE β Apply Conditional Logic in Queries
To add conditional logic within a query:
SELECT column_name,
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE result3
END
FROM table_name;
β Example: Categorize ages as ‘Young’, ‘Middle-Aged’, or ‘Senior’.
π SQL Performance Best Practices
- βοΈ Use Indexing β Speeds up searches significantly.
- βοΈ Avoid
SELECT *
β Fetch only the necessary columns to improve performance. - βοΈ Normalize Data β Organize data efficiently to reduce redundancy.
- βοΈ Use Joins Instead of Subqueries β Joins often run faster than nested queries.
- βοΈ Filter Early β Apply conditions as soon as possible in your query.
π Final Thoughts
Mastering SQL is a game-changer in handling databases effectively. With this cheat sheet, you have a solid reference for writing powerful queries. Keep practicing, experiment with different queries, and soon, you’ll be an SQL expert!
π Learn More:
π¬ Have questions? Drop them in the comments below!