SQL Cheat Sheet With Tips
SQL Cheat Sheet With Tips
1. SELECT Statement
2. WHERE Clause
- Tip: Use WHERE to reduce the result set for better performance.
3. AND/OR Operators
- Example: SELECT * FROM table WHERE column1 = 'value1' AND column2 = 'value2';
- Tip: Be careful when combining AND/OR to ensure proper logic in the query.
4. ORDER BY Clause
- Tip: Always use ORDER BY when displaying data to control the order of rows.
5. GROUP BY Clause
- Tip: GROUP BY is often used with aggregate functions like SUM(), COUNT(), AVG().
6. HAVING Clause
- Example: SELECT column1, COUNT(*) FROM table GROUP BY column1 HAVING COUNT(*) >
10;
7. JOIN Operations
- INNER JOIN: SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
- LEFT JOIN: SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
8. DISTINCT Keyword
9. Aggregate Functions
- Tip: Aggregate functions work on grouped data and require a GROUP BY clause.
10. Subqueries
employees);
- Tip: Use subqueries for complex filtering that cannot be handled with a JOIN.
- Tip: Use LIMIT to control large result sets and avoid performance issues.