SQL Cheat Sheet
SQL Cheat Sheet
SHEET
By ai_with_thiru
1. What is SQL
SQL (Structured Query Language) is a special language used to manage and work with
databases. A database is like a digital storage system that organizes and stores information
in tables. SQL helps us to:
SQL is widely used in popular database systems such as MySQL, PostgreSQL, SQL
Server, and Oracle.
Explanation:
• id INT PRIMARY KEY: A unique ID for each employee (Primary Key ensures
uniqueness).
• salary DECIMAL(10,2): Stores salary values with two decimal places (e.g.,
50000.50).
b) ALTER TABLE (Modifying an Existing Table)
The ALTER TABLE statement is used to change an already existing table by adding,
deleting, or modifying columns.
Example:
2. Deleting a column:
3. Modifying a column:
This changes the salary column from DECIMAL to FLOAT (useful for approximate
. values).
c) DROP TABLE (Deleting a Table)
The DROP TABLE command completely removes a table and all its data from the
database.
Example:
This will delete the employees table and all its records permanently.
4. DML (Data Manipulation Language)
DML is a set of SQL commands used to insert, update, delete, and retrieve data from
database tables.
Example:
Explanation:
• Adds a new employee with ID = 1, Name = John Doe, Department = HR, and
Salary = 50,000.
Example:
Explanation:
• The WHERE condition ensures that only one specific record is updated
The DELETE statement is used to remove specific records from the table.
Example:
Explanation:
• If we don’t use the WHERE clause, it will delete all records from the table!
5. DQL (Data Query Language)
DQL commands help us retrieve and view data from the database.
Examples:
• Fetches all columns (id, name, department, salary, etc.) for all employees.
Examples:
Example:
Explanation:
• Fetches all employees, sorted by salary in descending order (highest salary first).
• To sort in ascending order, use ORDER BY salary ASC.
6. FULL OUTER JOIN
Joins are used to combine data from two or more tables based on a common column.
• A FULL OUTER JOIN returns all records from both tables, even if there is no
match between them.
• If there is no matching record, NULL values are returned for missing data.
Example Query:
Explanation:
• The HAVING clause filters grouped results (like WHERE, but for grouped data).
• This filters only departments where the total salary exceeds 100,000.
8. Subqueries
A subquery is a query inside another query. It helps break down complex queries into
smaller, easier parts.
Explanation:
• The inner query (SELECT AVG(salary) FROM employees) calculates the average
salary.
• The outer query retrieves employees earning more than this average.
•
9. Indexes
Indexes help speed up searches by allowing the database to find records faster, just like
a table of contents in a book.
Creating an Index
How it Helps?
• When you search for employees with a specific salary (WHERE salary = 60000), the
database quickly finds the matching records instead of scanning the whole table.
Removing an Index
10. Views
A view is like a virtual table based on a query. It doesn’t store data but allows you to
use saved queries as if they were tables.
Creating a View
• This retrieves all employees earning more than 60,000, without needing to rewrite the
query every time.
Deleting a View
11. Stored Procedures
A stored procedure is a set of SQL commands saved in the database, which can be
executed anytime.
12. Transactions
A transaction ensures that multiple SQL commands either complete together or fail
together.
Example of a Transaction
Key Commands:
13. Triggers
A Trigger is a special SQL command that automatically runs when a certain event
happens in a database, such as INSERT, UPDATE, or DELETE.
Why Use Triggers?
Explanation:
• This trigger runs before a new row is inserted into the employees table.
• The final SELECT retrieves data from the CTE instead of rewriting the whole query.
CTEs can also be used in JOINs and filters for more advanced queries.
2. The main query finds employees who earn more than their department’s average.