SQL Cheatsheet: The Ultimate Guide That Saved My Time and Effort

SQL used to feel like a headache to me

Iโ€™d spend hours debugging slow queries, wondering where I went wrong. But once I understood how to write efficient SQL queries, everything changed

What used to take hours now gets done in minutes and you can do the same

๐—•๐—”๐—ฆ๐—œ๐—– ๐—–๐—ข๐— ๐— ๐—”๐—ก๐——๐—ฆ โž›๐—ง๐—ต๐—ฒ ๐—™๐—ผ๐˜‚๐—ป๐—ฑ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ฌ๐—ผ๐˜‚ ๐—ก๐—ฒ๐—ฒ๐—ฑ

โ†ณ Retrieve Data with SELECT
The SELECT command is the bread and butter of SQL.

For example, to get the names of all employees, you can write:
โž›SELECT name FROM employees

โ†ณ Filter Results with WHERE
Tired of manually searching through rows? The WHERE clause filters data

For instance, to find all employees who work in the Sales department, use:
โž›SELECT * FROM employees WHERE department = 'Sales'

โ†ณ Insert Records with INSERT
Adding new data to a table used to feel complicated, but INSERT made it straightforward

To add a new employee named John, aged 30, write:
โž›INSERT INTO employees (name, age) VALUES ('John', 30)

โ†ณ Modify Data with UPDATE
Made a mistake in your data? You can fix it without starting over

If you want to change Johnโ€™s age to 31, use:
โž›UPDATE employees SET age = 31 WHERE name = 'John'

โ†ณ Remove Records with DELETE
Need to clean up your database? DELETE gets rid of unnecessary records quickly

For example, to remove John from the employee list, write:
โž›DELETE FROM employees WHERE name = 'John'


๐—”๐——๐—ฉ๐—”๐—ก๐—–๐—˜๐—— ๐—–๐—ข๐— ๐— ๐—”๐—ก๐——๐—ฆ โž›๐—ง๐—ฎ๐—ธ๐—ฒ ๐—ฌ๐—ผ๐˜‚๐—ฟ ๐—ค๐˜‚๐—ฒ๐—ฟ๐—ถ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—ง๐—ต๐—ฒ ๐—ก๐—ฒ๐˜…๐˜ ๐—Ÿ๐—ฒ๐˜ƒ๐—ฒ๐—น
โ†ณ Combine Data with JOIN
JOIN is a lifesaver when you need to work with multiple tables

For example, to link customer orders with their details, you can write:
โž›SELECT orders. id, customers. name FROM orders INNER JOIN customers ON orders.customer_id = customers. id

โ†ณ Group Data with GROUP BY
GROUP BY helps you analyze data by categories

To count the number of employees in each department, write:
โž›SELECT department, COUNT(*) FROM employees GROUP BY department

โ†ณ Sort with ORDER BY
Sorting data is a breeze with ORDER BY

To find the highest earners by sorting salaries in descending order, use:
โž›SELECT name, salary FROM employees ORDER BY salary DESC

โ†ณ Filter Groups with HAVING
When GROUP BY isnโ€™t enough, HAVING steps in to filter groups.

For instance, to identify departments with more than 5 employees, write:
โž›SELECT depart, COUNT() FROM employees GROUP BY department HAVING COUNT() > 5

โ†ณ Use CTEs
Break complex queries into manageable parts with CTEs

โ†ณ Leverage Window Functions
Perform calculations across rows while retaining the original data


๐—ง๐—›๐—˜ ๐—•๐—œ๐—š ๐—ฃ๐—œ๐—–๐—ง๐—จ๐—ฅ๐—˜ โž›๐—›๐—ข๐—ช ๐—œ๐—ง ๐—–๐—›๐—”๐—ก๐—š๐—˜๐—— ๐— ๐—ฌ ๐—ช๐—ข๐—ฅ๐—ž
One of the key insights I learned was the SQL ๐Ž๐ซ๐๐ž๐ซ ๐จ๐Ÿ ๐„๐ฑ๐ž๐œ๐ฎ๐ญ๐ข๐จ๐ง, which completely changed how I approached query writing

Understanding how SQL processes commands step by step allowed me to write better and faster queries.

Save ๐Ÿ’พ โžž React ๐Ÿ‘ โžž Share โ™ป๏ธ

credit : author