SQL Cheat Sheet
Types of Language in SQL
Section titled “Types of Language in SQL”Data Definition Language (DDL): Defines and manages the structure of database objects.
CREATE, ALTER, DROP, TRUNCATE USE, DESC/ DESCRIBE, SHOWData Manipulation Language (DML): Manipulates data within database tables.
SELECT, INSERT, UPDATE, DELETEData Control Language (DCL): Controls access to data in the database.
GRANT, REVOKETransaction Control Language (TCL): Manages transactions to ensure data integrity.
COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION- SHOW DATABASES
SHOW DATABASES
SHOW DATABASES1. DDL For Databases
Section titled “1. DDL For Databases”- CREATE, DROP, USE, SHOW TABLES
CREATE DATABASE
CREATE DATABASE DatabaseName;-- orCREATE DATABASE IF NOT EXISTS DatabaseName;DROP DATABASE
DROP DATABASE DatabaseName;-- orDROP DATABASE IF EXISTS DatabaseNameUSE
-- Selects the specified database for the current sessionUSE DatabaseName;SHOW TABLES
-- Lists all tables in the currently selected databaseSHOW TABLES;2. DDL For Tables
Section titled “2. DDL For Tables”- CREATE x3, ALTER x5, DROP, SHOW COLUMNS, DESC/DESCRIBE
CREATE TABLE
CREATE TABLE TableName(ColumnName1 DataType [Size],ColumnName2 DataType [Size],...)-- orCREATE TABLE TableName(ColumnName1 DataType [Size] Constraints,...)REFERENCES (defines a foreign key constraint) ⭐
-- Links a column in one table to a `PRIMARY KEY` in another.CREATE TABLE TableName ( Column1 DataType, Column2 DataType, FOREIGN KEY (Column2) REFERENCES OtherTable(OtherColumn));Or inline:
CREATE TABLE TableName ( Column1 DataType, Column2 DataType REFERENCES OtherTable(OtherColumn));AS - SELECT
-- Create TAble from Existing TableCREATE TABLE TableName AS(SELECT ColumnName, ColumnName2 FROM TableName WHERE <Condition>)Full Use case Example ⭐
-- Table 1: Use of -> Primary Keys, SizeCREATE TABLE Department ( dept_id INT PRIMARY KEY, dept_name CHAR(30));
-- Table 2: Use of Primary Keys, Size, Foregin Key (References)CREATE TABLE Student ( student_id INT PRIMARY KEY, name CHAR(20), email VARCHAR(50), age INT, grade DECIMAL(4,2), dept_id INT, FOREIGN KEY (dept_id) REFERENCES Department(dept_id));
-- Table 3: Use of AS (Select)CREATE TABLE TopStudents ASSELECT student_id, name, gradeFROM StudentWHERE grade >= 90.00;NOTE
- Ensures
Column2inOrdersmust exist inOtherTAble(OtherColumn)⭐ REFERENCESrequires the target column to be uniquely identifiable (PRIMARY KEY or UNIQUE)
Benefit of linking to PRIMARY KEY:
- Ensures only valid values (that exist in the referenced table) can be inserted.
- Maintains referential integrity (no broken or orphan records).
- Enables relational mapping (e.g., Orders → Customers).
- Supports joins across tables efficiently.
- Prevents accidental deletion or updates (with
ON DELETE/ON UPDATErules). - This ensures one-to-one or many-to-one relationships can be enforced.
Constraint vs size
CHAR(5)→ allows exactly 5 characters (padded if shorter).VARCHAR(5)→ allows up to 5 characters.INT(5)→ does not restrict the number of digits. (INT,BIGINTstore binary integers, not character strings.)- Use
CHECKfor integer constraint
column2 INT,CHECK (column2 BETWEEN 10000 AND 99999) -- exactly 5-digit numbersALTER TABLES
ADD
ALTER TABLE TableNameADD ColumnName DataType (Size) <Constraint>MODIFY
ALTER TABLE TableNameMODIFY ColumnName DataType (Size)FIRST / AFTER
-- Places the modified column at the beginning of the ttableALTER TABLE TableName MODIFY ColumnName DataType(Size) FIRST;
-- Places the modified column immediately after the specified column.ALTER TABLE TableName MODIFY ColumnName DataType(Size) AFTER ColumnName2;CHNAGE
-- Changing ColumnNameALTER TABLE TableName CHANGE OldName NewName Datatype(Size)DROP
-- Removing Table ComponentsALTER TABLE TableNameDROP PRIMARY KEY,DROP FOREIGN KEY,DROP ColumnName,SHOW COLUMNS
-- to view column details of a tableSHOW COLUMNS FROM TableName;DESC or DESCRIBE
-- Shows the structure of the specified table (columns, data types, etc.)DESC TableName;-- orDESCRIBE TableName;DROP TABLE
-- Drop Table CommandsDROP TABLE TableName;-- orDROP TABLE IF EXISTS TableName;TRUNCATE TABLE
-- Truncate Table Command (removes all rows, resets identity)TRUNCATE TABLE TableName;- Truncate Don’t Delete Table itself, but its a
DDLCommand
NOTE :
- Faster than
DELETE(which is in DML) - Use
TRUNCATEwhen you need to quickly clear all rows from a table but keep the table structure. - Use
DROPwhen you want to permanently delete a table, including its structure.
Why Truncate is in DDL and not in DML ?
-
Resets structure (like AUTO_INCREMENT):
- Resets identity/auto-increment counters to start from initial value.
- Example:
TRUNCATE TABLE students; -- Next insert will start student_id from 1 again (if auto_increment) -
No row-level logging & mostly non-rollbackable:
- Most DBMS don’t log individual row deletions.
- Can’t
ROLLBACKafterTRUNCATE(unless in a transaction and supported).
-
Deallocates entire data pages:
- Instead of deleting row-by-row (like
DELETE), it frees up memory pages. - That’s why it’s faster and more efficient.
- Instead of deleting row-by-row (like
- SELECT, INSERT, DELETE, UPDATE
SELECT
-- Show all columnsSELECT * FROM TableName;
-- Show specific columnsSELECT ColumnName1, ColumnName2, ColumnName3 FROM TableName;
-- This adds a fixed text column in the result for every row.SELECT ColumnName, 'Text' FROM TableName;
/* ColumnName | 'Text' ---------------|----------- Value 1 | 'Text' Value 2 | 'Text' Value 3 | 'Text'*/DISTINCT
-- Select distinct rowsSELECT DISTINCT * FROM TableName;
-- Select distinct values in a specific columnSELECT DISTINCT ColumnName FROM TableName;ALL
-- select ALL (Include Non-Distinct) (ALL is implicit by default)
SELECT ALL * FROM TableName; -- Select all rowsSELECT ALL ColumnName FROM TableName; -- Select all values in a specific column
-- `ALL` as Comparison OperatorSELECT * FROM EmployeesWHERE Salary > ALL (SELECT Salary FROM Employees);-- This query selects employees whose salary is greater than all the salariesNote:- ALL is redundant because it’s the default behavior. It Select all non-distinct values (default).
Expression
-- Evaluate a expression (MySQL, PostgreSQL, SQL Server, etc.)SELECT 1 + 6; --/*| 1 + 6 ||-------|| 7 |*/
-- Evaluate a expression (used in Oracle, Also work in MySQL)SELECT 4 * 3 FROM DUAL;/*| 4 * 3 ||-------|| 12 |*/
-- Scalar expression with a selected fieldSELECT ColumnName * 100 FROM TableName;Note: DUAL is a special one-row, one-column table used in Oracle for expressions without needing actual data. DUAL allows you to execute expressions like arithmetic, string manipulations, or system functions without requiring a real table.
Top 100 fields ⭐
-- In SQL Server, Sybase, MS Access --SELECT TOP 100 * FROM TableName;
-- MySQL, MariaDB, PostgreSQL`SELECT * FROM TableName LIMIT 100;`AS
-- Using column aliasesSELECT ColumnName AS MyColumnName FROM TableName;IFNULL ⭐
-- Replaces NULL in ColumnName with 'ValueSubstitute'SELECT IFNULL(ColumnName, 'ValueSubstitute') FROM TableName;WHERE
-- Filters rows based on specified conditionsSELECT ColumnNameFROM TableNameWHERE <Conditions>;BETWEEN
-- Filters rows where ColumnName is between x and y (inclusive)... WHERE ColumnName BETWEEN x AND yIN
-- Filters rows where ColumnName matches any value in the list (x, y, z)... WHERE ColumnName IN (x, y, z)LIKE
-- Filters rows where ColumnName starts with '13'...WHERE ColumnName LIKE '13%'
-- Filters rows where ColumnName has exactly 3 characters...WHERE ColumnName LIKE "___"IS NULL
-- Filters rows where ColumnName has a NULL value...WHERE ColumnName IS NULL;Note: customerId = NULL ❌ Syntax Error
Full Use Case Example: ⭐
SELECT *FROM EmployeesWHERE (Bonus + Commission > 10000) AND (Department = 'Sales') AND (Name LIKE 'J___') -- Name starts with J and has 4 letters AND (YearsExperience IS NOT NULL) AND NOT (JobTitle LIKE 'Intern%') -- Exclude interns AND Department IN ('Sales', 'Marketing') -- Department is either Sales or Marketing AND Age BETWEEN 30 AND 40; -- Age between 30 and 40 (inclusive)<> : not
ORDER BY
-- Sorts the result set in ascending order by ColumnName (default)SELECT *FROM TableNameORDER By ColumName-- or... ORDER BY ColumnName ASC;
-- Sorts the result set in descending order by ColumnName... ORDER BY ColumnName DESC;GROUP BY
-- Groups rows based on unique values in ColumnName1 and calculates an aggregate function for each groupSELECT ColumnName1, AggregateFunction(ColumnName2)FROM TableNameGROUP BY ColumnName1;HAVING
-- Filters groups created by the GROUP BY clause based on a condition applied to an aggregate functionSELECT ColumnName1, AggregateFunction(ColumnName2)FROM TableNameGROUP BY ColumnName1HAVING AggregateFunction(ColumnName2) < Condition;Where vs Having Clause : ⭐
| Feature | WHERE Clause | HAVING Clause |
|---|---|---|
| Used On | Rows (Before grouping) | Groups (After GROUP BY) |
| Aggregate Use | Cannot use aggregate functions | Can use aggregate functions |
| Execution Order | Evaluated before GROUP BY | Evaluated after GROUP BY |
Non-Aggregate vs Aggregate functions: ⭐
| Aspect | Non-Aggregate Functions | Aggregate Functions |
|---|---|---|
| Definition | Work on individual rows | Work on groups of rows |
| Return Value | One result per row | One result per group or table |
| Usage | In SELECT, WHERE, etc. | In SELECT, HAVING, GROUP BY |
| Examples | UPPER(), LOWER(), LENGTH() | SUM(), AVG(), COUNT(), MAX(), MIN() |
| Can be used in WHERE? | Yes | No |
| Can be used in HAVING? | No | Yes |
Aggregate Functions
Section titled “Aggregate Functions”ROUND()
-- Rounds the values in ColumnNameSELECT ROUND(ColumnName) AS MyColumnName FROM TableName;AVG()
-- Calculates the average of distinct values in ColumnNameSELECT AVG(DISTINCT ColumnName) FROM TableName;
-- Calculates the average of all values in ColumnName (default)SELECT AVG(ALL ColumnName) FROM TAbleName;COUNT()
-- Counts all rows, including those with NULL values (* include null value)SELECT COUNT(*) FROM TableName;
-- Counts distinct non-NULL values in ColumnNameSELECT COUNT(DISTINCT ColumnName) FROM TableName;
-- Counts all non-NULL values in ColumnName (default)SELECT COUNT(ALL ColumnName) FROM TableName;MAX()
-- Finds the maximum of distinct values in ColumnNameSELECT MAX(DISTINCT ColumnName) FROM TableName;MIN()
-- Finds the minimum of distinct values in ColumnNameSELECT MIN(DISTINCT ColumnName) FROM TableName;SUM()`
-- Calculates the sum of distinct values in ColumnNameSELECT SUM(DISTINCT ColumnName) FROM TableName;✅ Revision Done Upto here - 18 June 2025
INSERT INTO
VALUES
INSERT INTO TableName ColumnName1 VALUES Value1 ;-- orINSERT INTO TableName (ColumnName1, ColumnName2) VALUES (Value1, Value2) ;SELECT
-- Inserts data into TableName by selecting rows from TableName2INSERT INTO TableName1 SELECT * FROM TableName2 WHERE <Conditions>;
-- Inserts specific columns into TableName1 by selecting them from TableName2INSERT INTO TableName SELECT ColumnName FROM TableName WHERE <Conditions>;UPDATE
SET
-- Updates the value of ColumnName to Value2 for rows that meet the ConditionUPDATE TableNameSET ColumnName = Value2 WHERE <Condition>;
-- Increments the value of ColumnName by 900 for all rows in TableNameUPDATE TableNameSET ColumnName = ColumnName + 900;DELETE
FROM
-- Delete All ContentDELETE FROM TableName
-- Delete Specific RowsDELETE FROM TableName WHERE <Predicate>Comments in SQL
Section titled “Comments in SQL”SQL supports different types of joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN.
-- or /* ... */
Section titled “-- or /* ... */”-- this is a single line
/* This is aMultiline Comment */- In MySQL Single-line comments can be implemented with
#too , along with--
SQL Clause order ⭐
Section titled “SQL Clause order ⭐”WHERE- Filters rows before any grouping happens.GROUP BY- Groups the filtered rows by specified column(s).HAVING- Filters groups (not individual rows) after aggregation.ORDER BY- Sorts the final result.
SELECTFROM--WHEREGROUP BYHAVINGORDER BY -- sorts the grouped resultWHERE → GROUP BY → HAVING → ORDER BY
Data Types in SQL
Section titled “Data Types in SQL”1. Numeric Data Types
INTorINTEGER- Integer (whole number)SMALLINT- Smaller range integerBIGINT- Large range integerDECIMAL(p,s)- Exact fixed-point numberNUMERIC(p,s)- Same as DECIMALFLOAT(p)- Approximate floating-point numberREAL- Approximate floating-point (lower prec)
2. String/Text Data Types
CHAR(n)- Fixed-length stringVARCHAR(n)- Variable-length stringVARCHAR(n)- Variable-length stringTEXT- Large variable-length text
3. Date & Time Data Types
DATE- Date (YYYY-MM-DD)TIME- Time (HH:MM:SS)DATETIME- Date and timeTIMESTAMP- Auto-generated datetimeYEAR- Year (2 or 4 digits)
4. Boolean Type
BOOLEAN- True or False (0/1)
5. Binary Data Types
BINARY(n)- Fixed-length binary dataVARBINARY(n)- Variable-length binary dataBLOB- Binary Large Object (images, files)
JOIN in SQL
Section titled “JOIN in SQL”- SQL supports different types of joins:
INNER JOIN,LEFT JOIN,RIGHT JOIN,FULL JOIN,CROSS JOIN. JOIN=Cross(Cartesian) Product +Condition
-- Table1| ID | Name ||----|-------|| 1 | Alice || 2 | Bob |
-- Table2| Code | Color ||------|--------|| A | Red || B | Blue |
-- Cartesian Product (Cross Join) Result| ID | Name | Code | Color ||----|-------|------|-------|| 1 | Alice | A | Red || 1 | Alice | B | Blue || 2 | Bob | A | Red || 2 | Bob | B | Blue |1. INNER JOIN = Intersection
INNER JOINor justJOIN- Definition: Returns only the rows where there is a match in both tables.
- Use Case: When you only need data that exists in both tables.
SELECT p.firstName, p.lastName, a.cityFROM Person pINNER JOIN Address a ON p.PersonId = a.PersonId;2. LEFT JOIN = Inner Join + Left
LEFT OUTER JOINor justLEFT JOIN- Definition: Returns all rows from the left table and the matched rows from the right table. If there is no match,
NULLis returned for columns from the right table. - Use Case: When you need all data from the left table regardless of matches in the right table.
SELECT p.firstName, p.lastName, a.cityFROM Person pLEFT JOIN Address a ON p.PersonId = a.PersonId;3. RIGHT JOIN = Inner Join + Right
RIGHT OUTER JOINor justRIGHT JOIN- Definition: Returns all rows from the right table and the matched rows from the left table. If there is no match,
NULLis returned for columns from the left table. - Use Case: When you need all data from the right table regardless of matches in the left table.
SELECT p.firstName, p.lastName, a.cityFROM Person pRIGHT JOIN Address a ON p.PersonId = a.PersonId;4. OUTER JOIN = Inner Join + Left + Right = Union
FULL OUTER JOINor justFULL JOINorOUTER JOIN- Definition: Returns all rows from both tables. When there is no match,
NULLis returned for columns from the table without a match. - Use Case: When you need all records from both tables, whether or not they have matches.
SELECT p.firstName, p.lastName, a.cityFROM Person pFULL OUTER JOIN Address a ON p.PersonId = a.PersonId;5. CROSS JOIN
- Definition: Returns the Cartesian product of the two tables, meaning every row from the first table is joined with every row from the second table.
- Use Case: Rarely used, but useful for generating combinations of rows from two tables.
SELECT p.firstName, a.cityFROM Person pCROSS JOIN Address a;- Natural Join without Using
CROSS JOINKeyword
SELECT P.firstName, a.cityFROM Person p, Address a;6. SELF JOIN
-
Definition: Joins a table to itself, which can be useful for comparing rows within the same table.
-
Use Case: When you need to find relationships within a single table.
SELECT e1.EmployeeName, e2.EmployeeName FROM Employee e1 JOIN Employee e2 ON e1.ManagerId = e2.EmployeeId; ```
7. NATURAL JOIN
- Definition: Automatically joins tables based on columns with the same names and removes duplicate columns from the result.
- It eliminates the need to write
ON tableA.column = tableB.column. i.e. it matches columns with the same name - Note: Avoid if column names mismatch or ambiguity is possible.
SELECT *FROM EmployeeNATURAL JOIN Department;- Natural Join without Using
NATURAL JOINKeyword
# Using OnlySELECT e.EmployeeName, d.DepartmentNameFROM Employee eJOIN Department d ON e.DepartmentId = d.DepartmentId;
# Using `WHERE` ClauseSELECT *FROM Employee e, Department dWHERE e.DepartmentId = d.DepartmentId;Types of Natural Join
- Natural + Inner Join :
NATURAL JOIN. - Natural + Left Jon :
NATURAL LEFT JOIN. - Natural + Right Jon : NATURAL RIGHT JOIN`.
- Natural + Full Outer Join :
NATURAL FULL JOINorNATURAL FULL OUTER JOIN.
Note:
NATURAL JOINNot supported by Many browsers such asSQL Server,Oracle,SQLiteNATURAL JOINcan be risky if the table structures change over time. This is why many developers prefer to use explicit join conditions withINNER JOIN,LEFT JOIN, etc., to have full control over which
Order of Keyword
NATURAL LEFT JOIN. ✅LEFT NATURAL JOIN. ❌ Syntax Error
Alias Name
- Table Alias: Short name for table
Employee AS e1 - Column Alias: Rename output column
e1.EmployeeName AS Employee
SELECT e1.EmployeeName AS Employee, e2.EmployeeName AS ManagerFROM Employee AS e1JOIN Employee AS e2 ON e1.ManagerId = e2.EmployeeId;Key Notes
Section titled “Key Notes”SQL Rules :
- SQL keywords are not case-sensitive.- Database, table, and column names are *usually not case-sensitive- An SQL statement must end with a `;`- Use single quotes (') for string literals in SQL.- Double quotes are usually reserved for identifiers (e.g., column names)- SQL is not white-space sensitive- Enclose subqueries in parentheses `()` or enclose queries that include more than one columns, condition, statements.Parenthesis ’()’ uses :
- Function Call Max()- Group Columns Together SELECT (Column1, Column2) FROM TableName;- Definin Datype Size VARCHAR(100)- Grouping WHERE Clauses ( Salary > 5000 AND Dept = 'IT') OR (Expression>5);- Subqueries SELECT * FROM Employee WHERE DepartmentID IN (SELECT ID FROM Departmnets WHERE = 'HR');
# Parenthesis ErrorParentheses are not required around the column definition when adding a single column. ALTER TABLE Employees ADD|Modify Email VARCHAR(100);Operators in SQL :
NULL Handeling : IS NULL, IS NOT NULLRelational Operator : <, >, =, <, >, <=, >=, <>(not equal)Logical Operator : OR(||), NOT(!), AND(&&)Arithmetic Operators : +, -, *, /Wildcards(used with like): `%` for sequence of character, `-` for a single characterConstraints :
NOT NULL, UNIQUE, PRIMARY KEY, DEFAULT 'Value', CHECK (<Conditions>), REFERENCESELECT Format :
NOTE: ALL can be used wherever DISTINCT is applicable (default is ALL).SELECT (ALL/ DISTINCT) (*/ColumnName) FROM TableName;Double use of Same Keyword DESC, AS, ALL
-- Two uses of `DESC`DESC table_name; -- Describe table structure (DDL)SELECT * FROM table_name ORDER BY column_name DESC; -- Sort in descending order (DML)
-- Two uses of `AS`SELECT column_name AS alias; -- Column alias (Rename column in output)CREATE TABLE new_table AS SELECT ...; -- Create new table using result of a SELECT
-- Two uses of `ALL`SELECT ALL column_name FROM table_name; -- (Default) Include duplicates in SELECTSELECT * FROM table_name WHERE salary > ALL (...); -- Comparison operator in subqueriesQuery Processing and Execution Order
Section titled “Query Processing and Execution Order”SQL Query Writing Order (Syntax Order):
SELECT column1, column2, ...FROM table_name[JOIN other_table ON condition][WHERE condition][GROUP BY column][HAVING condition][ORDER BY column ASC|DESC][LIMIT number OFFSET number];- Writing order is how you write the query.
Order of Execution in SQL Query:
1. FROM -- tables & joins2. ON -- join condition3. JOIN -- apply joins4. WHERE -- row filtering5. GROUP BY -- grouping rows6. HAVING -- group filtering7. SELECT -- select columns or expressions8. DISTINCT -- remove duplicates9. ORDER BY -- sort results10. LIMIT / OFFSET -- restrict output-
- Execution order is how SQL engine processes it internally.
Case Sensitivity
Section titled “Case Sensitivity”This is Just for Understanding Purposes and Not asked in SQL questions
Whether SQL is case-sensitive or not depends on the database system and the specific collation settings used.
Collation
- Collation in SQL refers to a set of rules that determine how data is sorted and compared in a database.
- It specifies how characters are compared, which affects operations like
ORDER BY,WHERE, andJOIN. - Collation is crucial for managing data in different languages and for case-sensitivity.
Components of Collation
- Character Set: ex
utf8,latin1 - Collation Name:
utf8_general_ciorutf8_bin - Case Sensitivity:
CSfor case-insensitivity,CIfor case-insensitive (e.g.a&A) - Accent Sensitivity:
ASfor accent-sensitivity ,AIfor accent-insensitivity (e.g.évs.e)
Examples Collation
utf8_general_ci:CI&AIutf8_bin:CS&ASSQL_Latin1_General_CP1_CI_AS(SQL Server) :CI&AS
Case Sensitivity in SQL Database
- MySQL :
- Case sensitivity depends on the collation of the columns or the database
- By default, string comparisons in MySQL are case-insensitive (e.g.,
utf8_general_cicollation). - However, column collation can be set to case-sensitive (e.g.,
utf8_bincollation).
- SQL Server :
- Case sensitivity is determined by the collation settings of the columns or database.
CIin collation (e.g.,SQL_Latin1_General_CP1_CI_AS) stands for Case Insensitive.CSin collation (e.g.,SQL_Latin1_General_CP1_CS_AS) stands for Case Sensitive.
- Oracle:
- Comparisons are case-sensitive by default. To make them case-insensitive, you can use functions like
UPPER()orLOWER().
- PostgreSQL:
- By default, it is case-sensitive when comparing strings.
Summary : MySQL - String comparison CI & Column Collation CS SQL Server - Depends on Setting Oracle - Comparisons are CS PostgreSQL - String Comparison CS