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
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