Ultimate SQL Cheat Sheet: Your Go-To Guide for Mastering SQL
SQL (Structured Query Language) is an essential tool for managing and manipulating databases. Whether you're a beginner or an experienced user, having a comprehensive cheat sheet can streamline your SQL queries and improve your efficiency. Here’s an ultimate SQL cheat sheet that covers the basics to advanced functions, ensuring you have the essential commands at your fingertips.
Basic SQL Commands
1. SELECT
- Retrieve data from a database.
SELECT column1, column2
FROM table_name;
2. SELECT DISTINCT
- Retrieve unique records.
SELECT DISTINCT column1, column2
FROM table_name;
3. WHERE
- Filter records based on a condition.
SELECT column1, column2
FROM table_name
WHERE condition;
4. AND, OR, NOT
- Combine multiple conditions.
SELECT column1, column2
FROM table_name
WHERE condition1 AND condition2;
5. ORDER BY
- Sort the result set.
SELECT column1, column2
FROM table_name
ORDER BY column1 ASC | DESC;
6. INSERT INTO
- Insert new data into a database.
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
7. UPDATE
- Update existing data.
UPDATE table_name
SET column1 = value1
WHERE condition;
8. DELETE
- Delete existing data.
DELETE FROM table_name
WHERE condition;
Advanced SQL Commands
1. JOIN
- Combine rows from two or more tables.
SELECT column1, column2
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
2. LEFT JOIN
- Retrieve all rows from the left table and match rows from the right table.
SELECT column1, column2
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
3. RIGHT JOIN
- Retrieve all rows from the right table and match rows from the left table.
SELECT column1, column2
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
4. FULL JOIN
- Retrieve rows when there is a match in one of the tables.
SELECT column1, column2
FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column;
5. UNION
- Combine the result set of two or more SELECT statements.
SELECT column1, column2
FROM table1
UNION
SELECT column1, column2
FROM table2;
6. GROUP BY
- Group rows sharing a property so that an aggregate function can be applied.
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;
7. HAVING
- Filter groups based on a condition.
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1
HAVING COUNT(*) > 1;
8. EXISTS
- Check for the existence of rows in a subquery.
SELECT column1
FROM table_name
WHERE EXISTS (SELECT column1 FROM table_name WHERE condition);
SQL Functions
1. Aggregate Functions
- Perform calculations on a set of values.
Recommended by LinkedIn
- COUNT(): Returns the number of rows.
- SUM(): Returns the total sum.
- AVG(): Returns the average value.
- MIN(): Returns the smallest value.
- MAX(): Returns the largest value.
2. String Functions
- Manipulate string values.
- LEN(): Returns the length of a string.
- UPPER(): Converts a string to uppercase.
- LOWER(): Converts a string to lowercase.
- SUBSTRING(): Extracts characters from a string.
3. Date Functions
- Work with date and time values.
- GETDATE(): Returns the current date and time.
- DATEADD(): Adds a time interval to a date.
- DATEDIFF(): Returns the difference between two dates.
- CONVERT(): Converts a value to a specified data type.
Common SQL Clauses
1. LIKE
- Search for a specified pattern.
SELECT column1, column2
FROM table_name
WHERE column1 LIKE pattern;
2. IN
- Check if a value matches any value in a list.
SELECT column1, column2
FROM table_name
WHERE column1 IN (value1, value2);
3. BETWEEN
- Select values within a range.
SELECT column1, column2
FROM table_name
WHERE column1 BETWEEN value1 AND value2;
4. ALIAS
- Rename a column or table with an alias.
SELECT column1 AS alias_name
FROM table_name;
Indexing and Constraints
1. CREATE INDEX
- Create an index on a table.
CREATE INDEX index_name
ON table_name (column1, column2);
2. UNIQUE
- Ensure all values in a column are unique.
CREATE TABLE table_name (
column1 datatype UNIQUE,
column2 datatype
);
3. PRIMARY KEY
- Uniquely identify each row in a table.
CREATE TABLE table_name (
column1 datatype PRIMARY KEY,
column2 datatype
);
4. FOREIGN KEY
- Maintain referential integrity between tables.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
FOREIGN KEY (column1) REFERENCES other_table(column)
);
Practical SQL Tips
1. Backup Your Data: Always backup your database before performing any major operations like updates or deletions.
2. Use Transactions: For complex operations, use transactions to ensure data integrity.
3. Optimize Queries: Regularly review and optimize your queries for performance.
4. Keep Learning: SQL is vast and constantly evolving. Keep exploring advanced topics and best practices.
With this SQL cheat sheet, you'll have a handy reference to quickly recall essential commands and functions, making your database interactions smoother and more efficient. Save it, share it, and refer back to it whenever you need a quick refresher.