SELECT * FROM table_name; -- Select all columns
SELECT column1, column2 FROM table_name; -- Select specific columns
SELECT * FROM table_name
WHERE column_name = 'value'; -- Exact match
SELECT * FROM table_name
WHERE column_name > 100; -- Greater than filter
SELECT * FROM table_name
WHERE column_name BETWEEN 10 AND 50; -- Range filter
SELECT * FROM table_name
WHERE column_name IN ('A', 'B', 'C'); -- Multiple values filter
SELECT * FROM table_name
ORDER BY column_name ASC; -- Ascending order
SELECT * FROM table_name
ORDER BY column_name DESC; -- Descending order
SELECT COUNT(*) FROM table_name; -- Count rows
SELECT AVG(column_name) FROM table_name; -- Average
SELECT SUM(column_name) FROM table_name; -- Sum
SELECT MIN(column_name) FROM table_name; -- Minimum
SELECT MAX(column_name) FROM table_name; -- Maximum
SELECT a.column1, b.column2
FROM table1 a
INNER JOIN table2 b
ON a.id = b.id;
SELECT a.column1, b.column2
FROM table1 a
LEFT JOIN table2 b
ON a.id = b.id;
SELECT a.column1, b.column2
FROM table1 a
RIGHT JOIN table2 b
ON a.id = b.id;
SELECT a.column1, b.column2
FROM table1 a
FULL OUTER JOIN table2 b
ON a.id = b.id;
SELECT *
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees); -- Subquery for average salary
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
SELECT name, salary,
SUM(salary) OVER (PARTITION BY department ORDER BY id) AS cumulative_salary
FROM employees;
INSERT INTO table_name (column1, column2)
VALUES ('value1', 'value2');
UPDATE table_name
SET column1 = 'new_value'
WHERE column2 = 'value';
DELETE FROM table_name
WHERE column_name = 'value';
CREATE INDEX index_name
ON table_name (column_name);
DROP INDEX index_name;
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;
DROP VIEW view_name;
SELECT * INTO OUTFILE '/tmp/data.csv'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
FROM table_name;
LOAD DATA INFILE '/tmp/data.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
- Test Queries Safely: Use SELECT first before DELETE or UPDATE.
- Index Key Columns: Optimize query performance for large datasets.
- Backup Regularly: Always save snapshots before data modifications.
- Use Aliases: Simplify queries with table or column aliases.
- Comment Your Code: Document queries for clarity and future use.
This cheat sheet summarizes the most important SQL commands and techniques for everyday tasks.
CompliedByUdithaWICK