Introduction
SQL (Structured Query Language) is used to interact with relational databases. It helps you to store, retrieve, update, and delete data. SQL is used in almost every industry, from websites to banking systems. If you want to work with data, SQL is a must-have skill. This cheatsheet will give you short and clear definitions, code examples, and key points for the most important SQL topics.
1. SELECT Statement
-
Definition: Used to fetch data from a table.
-
Example
SELECT name, age FROM Students; -
Key Point: Use
*to select all columns.
2. WHERE Clause
-
Definition: Filters records based on a condition.
-
Example
SELECT * FROM Students WHERE age > 18; -
Key Point: Works with operators like
=,>,<,BETWEEN,IN,LIKE.
3. ORDER BY
-
Definition: Sorts the result in ascending or descending order.
-
Example
SELECT * FROM Students ORDER BY age DESC; -
Key Point: Default sort is ascending (
ASC).
4. INSERT INTO
-
Definition: Adds new records to a table.
-
Example
INSERT INTO Students (name, age) VALUES ('John', 20); -
Key Point: Match the column order with the values.
5. UPDATE
-
Definition: Changes existing records.
-
Example
UPDATE Students SET age = 21 WHERE name = 'John'; -
Key Point: Always use
WHEREto avoid updating all rows.
6. DELETE
-
Definition: Removes data from a table.
-
Example
DELETE FROM Students WHERE name = 'John'; -
Key Point: Without
WHERE, all rows will be deleted.
7. CREATE TABLE
-
Definition: Creates a new table.
-
Example
CREATE TABLE Students ( id INT PRIMARY KEY, name VARCHAR(50), age INT ); -
Key Point: Define data types clearly.
8. ALTER TABLE
-
Definition: Changes the structure of a table.
-
Examples
-
Add column:
ALTER TABLE Students ADD email VARCHAR(100); -
Drop column:
ALTER TABLE Students DROP COLUMN email;
-
9. DROP TABLE
-
Definition: Deletes the table and its data.
-
Example
DROP TABLE Students; -
Key Point: This action cannot be undone.
10. JOINs
-
Definition: Combines rows from two or more tables.
-
Example (INNER JOIN)
SELECT Students.name, Marks.score FROM Students INNER JOIN Marks ON Students.id = Marks.student_id; -
Types
-
INNER JOIN: Only matching records.
-
LEFT JOIN: All records from left + matches.
-
RIGHT JOIN: All from right + matches.
-
FULL JOIN: All records from both.
-
11. GROUP BY
-
Definition: Groups rows based on a column and applies an aggregate function.
-
Example:
SELECT age, COUNT(*) FROM Students GROUP BY age; -
Key Point: Always use with aggregate functions.
12. HAVING
-
Definition: Filters groups created by
GROUP BY. -
Example:
SELECT age, COUNT(*) FROM Students GROUP BY age HAVING COUNT(*) > 1; -
Key Point: Use
HAVINGafterGROUP BY.
13. Aggregate Functions
-
Definition: Perform calculations on multiple values.
-
Common ones:
COUNT(),SUM(),AVG(),MIN(),MAX() -
Example
SELECT AVG(age) FROM Students;
14. DISTINCT
-
Definition: Removes duplicate rows.
-
Example
SELECT DISTINCT age FROM Students;
15. BETWEEN, IN, LIKE
-
BETWEEN
SELECT * FROM Students WHERE age BETWEEN 18 AND 25; -
IN
SELECT * FROM Students WHERE age IN (18, 21, 23); -
LIKE
SELECT * FROM Students WHERE name LIKE 'A%';
16. LIMIT / TOP
-
Definition: Restricts number of rows returned.
-
MySQL
SELECT * FROM Students LIMIT 5; -
SQL Server
SELECT TOP 5 * FROM Students;
17. UNION
-
Definition: Combines results of two
SELECTstatements. -
Example
SELECT name FROM Students UNION SELECT name FROM Teachers; -
Key Point: Both queries must have the same number of columns.
18. Subquery
-
Definition: A query inside another query.
-
Example
SELECT name FROM Students WHERE age = (SELECT MAX(age) FROM Students);
19. Views
-
Definition: A virtual table based on the result of a query.
-
Example
CREATE VIEW Teenagers AS SELECT * FROM Students WHERE age BETWEEN 13 AND 19;
20. Indexes
-
Definition: Improve search performance on columns.
-
Example
CREATE INDEX idx_name ON Students(name); -
Key Point: Indexes speed up
SELECTbut may slow downINSERT/UPDATE.
21. Constraints
-
Definition: Rules applied on columns.
-
Types
-
PRIMARY KEY: Unique and not null. -
FOREIGN KEY: Links to another table’s primary key. -
UNIQUE: Ensures all values are different. -
NOT NULL: Prevents null values. -
CHECK: Validates a condition.
-
-
Example
CREATE TABLE Students ( id INT PRIMARY KEY, age INT CHECK (age >= 0) );
22. DEFAULT Constraint
-
Definition: Sets a default value for a column if none is provided.
-
Example
CREATE TABLE Students ( id INT, name VARCHAR(50), status VARCHAR(10) DEFAULT 'active' ); -
Key Point: Helps avoid
NULLwhen no value is provided.
23. CASE Statement
-
Definition: Adds conditional logic inside a query.
-
Example
SELECT name, CASE WHEN age >= 18 THEN 'Adult' ELSE 'Minor' END AS age_group FROM Students; -
Key Point: Works like if-else in SQL.
24. EXISTS
-
Definition: Checks whether a subquery returns any row.
-
Example
SELECT name FROM Students s WHERE EXISTS ( SELECT 1 FROM Marks m WHERE m.student_id = s.id ); -
Key Point: Faster than
INin many cases for large datasets.
25. NOT IN vs NOT EXISTS
-
Caution:
NOT INfails if anyNULLexists in subquery. UseNOT EXISTSfor safer logic. -
Example
SELECT name FROM Students WHERE NOT EXISTS ( SELECT 1 FROM Graduates WHERE Students.id = Graduates.id );

Join the conversation! Your thoughts help the community grow.