SQL cheat sheet with explanations for each command:
1. SELECT: Retrieve data from a database table.
Example: `SELECT column1, column2 FROM table_name;`
2. DISTINCT: Remove duplicate rows from the result set.
Example: `SELECT DISTINCT column FROM table_name;`
3. WHERE: Filter rows based on specific conditions.
Example: `SELECT * FROM table_name WHERE column = value;`
4. AND / OR: Combine multiple conditions in a WHERE clause.
Example: `SELECT * FROM table_name WHERE condition1 AND condition2;`
5. ORDER BY: Sort the result set based on one or more columns.
Example: `SELECT * FROM table_name ORDER BY column ASC;`
6. GROUP BY: Group rows based on specified columns (usually used with aggregate functions).
Example: `SELECT column, COUNT(*) FROM table_name GROUP BY column;`
7. HAVING: Filter groups of rows based on aggregate function results.
Example: `SELECT column, COUNT(*) FROM table_name GROUP BY column HAVING COUNT(*) > 5;`
8. JOIN: Combine data from multiple tables based on a condition.
Example: `SELECT * FROM table1 JOIN table2 ON table1.column = table2.column;`
9. INNER JOIN: Return only the matched rows from both tables.
Example: `SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;`
10. LEFT JOIN: Return all rows from the left table and matched rows from the right table.
Example: `SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column;`
11. RIGHT JOIN: Return all rows from the right table and matched rows from the left table.
Example: `SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;`
12. FULL OUTER JOIN: Return all rows when there is a match in either table.
Example: `SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.column;`
13. UNION: Combine the result of two or more SELECT statements (removes duplicates).
Example: `SELECT column1 FROM table1 UNION SELECT column1 FROM table2;`
14. UNION ALL: Combine the result of two or more SELECT statements (includes duplicates).
Example: `SELECT column1 FROM table1 UNION ALL SELECT column1 FROM table2;`
15. INSERT INTO: Insert new records into a table.
Example: `INSERT INTO table_name (column1, column2) VALUES (value1, value2);`
16. UPDATE: Modify existing records in a table.
Example: `UPDATE table_name SET column = new_value WHERE condition;`
17. DELETE: Remove records from a table.
Example: `DELETE FROM table_name WHERE condition;`
18. CREATE TABLE: Create a new table with specified columns and data types.
Example:
```sql
CREATE TABLE table_name (
column1 datatype1,
column2 datatype2,
...
);
```
19. ALTER TABLE: Modify an existing table by adding or altering columns.
Example: `ALTER TABLE table_name ADD column datatype;`
20. DROP TABLE: Delete an existing table.
Example: `DROP TABLE table_name;`
Please note that the examples provided are simplified, and you may need to adjust them based on your specific database structure and data. SQL is a powerful language for working with relational databases, and these commands cover the most common operations.
Comments
Post a Comment