SQL Cheat Sheet: The Ultimate Quick Reference Guide

πŸ› οΈ 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.

SQL Cheat Sheet

πŸ”Ž 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:

Incident Management

Linux

SQL

πŸ’¬ Have questions? Drop them in the comments below!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top