SQL Cheatsheet β Quick Reference for Beginners and Professionals
π Key Takeaways
- SQL Cheatsheet β Quick Reference for Beginners and Professionals
- SQL Basics for Beginners
- SQL Data Types
- Inserting and Updating Data in SQL
- SQL SELECT Statement Explained
- SQL WHERE Clause
Table of Contents:
Structured Query Language (SQL) is the standard language used for managing and manipulating databases.
This SQL Cheatsheet is your complete quick reference for creating, updating, and managing data β whether you use MySQL, SQL Server, PostgreSQL, or Oracle.
SQL Basics for Beginners
| Command | Purpose | Example |
|---|---|---|
CREATE DATABASE | Creates a new database | CREATE DATABASE company; |
USE | Selects a database | USE company; |
CREATE TABLE | Creates a new table | CREATE TABLE employees (id INT, name VARCHAR(50)); |
DROP TABLE | Deletes a table | DROP TABLE employees; |
ALTER TABLE | Modifies a table structure | ALTER TABLE employees ADD salary INT; |
SQL Data Types
| Category | Data Types | Example |
|---|---|---|
| Numeric | INT, FLOAT, DECIMAL | salary DECIMAL(10,2) |
| String | CHAR, VARCHAR, TEXT | name VARCHAR(100) |
| Date/Time | DATE, TIME, DATETIME, TIMESTAMP | created_at DATETIME |
| Boolean | BOOLEAN, BIT | is_active BOOLEAN |
Inserting and Updating Data in SQL
Insert Data into a Table
INSERT INTO employees (id, name, salary)
VALUES (1, 'John Doe', 60000);
Update Existing Records
UPDATE employees
SET salary = 70000
WHERE id = 1;
Delete a Record
DELETE FROM employees
WHERE id = 1;
SQL SELECT Statement Explained
The SELECT statement is used to retrieve data from one or more tables.
SELECT name, salary FROM employees;
Aliases Example:
SELECT name AS EmployeeName, salary AS MonthlySalary FROM employees;
SQL WHERE Clause
Used to filter records based on a condition.
SELECT * FROM employees WHERE salary > 50000;
Common Operators:=, !=, >, <, >=, <=, BETWEEN, IN, LIKE, IS NULL
Example:
SELECT * FROM employees WHERE name LIKE 'J%';
SQL ORDER BY Clause
Sorts the result in ascending (ASC) or descending (DESC) order.
SELECT * FROM employees ORDER BY salary DESC;
SQL GROUP BY and HAVING Clauses
GROUP BY groups similar data.
HAVING filters aggregated data.
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
SQL Joins β Combining Data from Multiple Tables
| Type | Description | Example |
|---|---|---|
| INNER JOIN | Returns matching records from both tables | SELECT * FROM employees e INNER JOIN departments d ON e.dept_id = d.id; |
| LEFT JOIN | All records from left + matched from right | SELECT * FROM employees e LEFT JOIN departments d ON e.dept_id = d.id; |
| RIGHT JOIN | All records from right + matched from left | SELECT * FROM employees e RIGHT JOIN departments d ON e.dept_id = d.id; |
| FULL JOIN | All records when there is a match | SELECT * FROM employees e FULL JOIN departments d ON e.dept_id = d.id; |
SQL Aggregate Functions
| Function | Description | Example |
|---|---|---|
COUNT() | Returns number of rows | SELECT COUNT(*) FROM employees; |
SUM() | Returns total sum | SELECT SUM(salary) FROM employees; |
AVG() | Returns average value | SELECT AVG(salary) FROM employees; |
MIN() | Returns minimum value | SELECT MIN(salary) FROM employees; |
MAX() | Returns maximum value | SELECT MAX(salary) FROM employees; |
sql cheatsheet, sql tutorial, sql queries with examples, sql commands list, sql joins explained, sql functions, sql interview questions, database query guide, mysql cheatsheet, sql for beginners
SQL Constraints
| Constraint | Purpose | Example |
|---|---|---|
PRIMARY KEY | Uniquely identifies each record | id INT PRIMARY KEY |
FOREIGN KEY | Links to another table | FOREIGN KEY (dept_id) REFERENCES departments(id) |
NOT NULL | Ensures column cannot be null | name VARCHAR(50) NOT NULL |
UNIQUE | Prevents duplicate values | email VARCHAR(100) UNIQUE |
CHECK | Validates data | CHECK (salary > 0) |
DEFAULT | Sets default value | status VARCHAR(10) DEFAULT 'Active' |
SQL Subqueries
A subquery is a query inside another query.
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
SQL Views
A view is a virtual table.
CREATE VIEW high_salary AS
SELECT name, salary FROM employees WHERE salary > 70000;
SQL Indexes
Indexes improve query performance.
CREATE INDEX idx_name ON employees(name);
SQL Transactions
Used for safe execution of multiple statements.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
Use ROLLBACK; to undo changes.
SQL Interview Questions
Q1. What is the difference between WHERE and HAVING?
β‘οΈ WHERE filters rows before grouping, HAVING filters after grouping.
Q2. What is normalization?
β‘οΈ The process of organizing data to reduce redundancy.
Q3. What is a foreign key?
β‘οΈ Itβs a constraint that creates a relationship between two tables.
Q4. What is the difference between INNER JOIN and OUTER JOIN?
β‘οΈ INNER JOIN returns matched rows only, while OUTER JOIN returns all rows including unmatched ones.
FAQ β SQL Cheatsheet
Q1: What is SQL used for?
SQL is used to store, retrieve, and manipulate data in relational databases.
Q2: Which databases use SQL?
MySQL, PostgreSQL, SQL Server, Oracle, and SQLite all use SQL syntax.
Q3: Is SQL case-sensitive?
SQL keywords are not case-sensitive, but string comparisons may be.
Q4: What are DDL, DML, and DCL commands?
- DDL: Data Definition Language (
CREATE,ALTER,DROP) - DML: Data Manipulation Language (
INSERT,UPDATE,DELETE) - DCL: Data Control Language (
GRANT,REVOKE)