SQL Cheatsheet: Essential Commands for Database Management

Listen to this Post

Featured Image
SQL (Structured Query Language) is the backbone of database operations, enabling users to create, manipulate, and query data efficiently. Below is a comprehensive SQL cheatsheet covering essential commands, clauses, and functions.

1️⃣ Core SQL Commands

DDL (Data Definition Language)

– `CREATE TABLE table_name (column1 datatype, column2 datatype);`
– `DROP TABLE table_name;`
– `ALTER TABLE table_name ADD column_name datatype;`
– `TRUNCATE TABLE table_name;` (Deletes all records but keeps structure)
– `CREATE VIEW view_name AS SELECT column1 FROM table_name;`

DML (Data Manipulation Language)

– `SELECT FROM table_name;`
– `INSERT INTO table_name (column1, column2) VALUES (value1, value2);`
– `UPDATE table_name SET column1 = value1 WHERE condition;`
– `DELETE FROM table_name WHERE condition;`

DCL (Data Control Language)

– `GRANT permission ON database TO user;`
– `REVOKE permission ON database FROM user;`

DQL (Data Query Language)

– `SELECT column1, column2 FROM table_name WHERE condition;`

2️⃣ Clauses & Operators

– `WHERE column = value;`
– `HAVING COUNT(column) > 5;` (Used with GROUP BY)
– `GROUP BY column;`
– `ORDER BY column ASC/DESC;`
– Comparison Operators: `=, <>, >, <, >=, <=` - Logical Operators: `AND, OR, NOT` - Range Operators: `BETWEEN, IN, LIKE, EXISTS, ANY, ALL`

3️⃣ ORDER BY Examples

– `SELECT FROM employees ORDER BY salary ASC;` (Ascending)
– `SELECT FROM employees ORDER BY hire_date DESC;` (Descending)

4️⃣ Joins

  • INNER JOIN: Returns matching records.
  • LEFT JOIN: Returns all left table records + matched right table records.
  • RIGHT JOIN: Returns all right table records + matched left table records.
  • FULL JOIN: Returns all records when there’s a match in either table.

5️⃣ Functions

Aggregate Functions

– `COUNT()` – Counts rows.
– `SUM(column)` – Sums values.
– `AVG(column)` – Calculates average.
– `MIN(column), MAX(column)` – Finds min/max values.

Window Functions

– `OVER()` – Defines window for calculations.
– `RANK() OVER(ORDER BY column)` – Assigns rank with gaps.
– `DENSE_RANK() OVER(ORDER BY column)` – Assigns rank without gaps.
– `ROW_NUMBER() OVER()` – Assigns unique row numbers.
– `LAG(column, 1) OVER(ORDER BY column)` – Accesses previous row.
– `LEAD(column, 1) OVER(ORDER BY column)` – Accesses next row.

6️⃣ Aliasing

– `SELECT column AS alias FROM table;`
– `SELECT t1.column FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id;`

7️⃣ Table Management

– `CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(50));`
– `ALTER TABLE employees ADD email VARCHAR(100);`
– `TRUNCATE TABLE employees;`
– `DROP TABLE employees;`

8️⃣ Data Handling

– `SELECT FROM employees;` (All columns)
– `SELECT name, salary FROM employees;` (Specific columns)

9️⃣ Views

– `CREATE VIEW high_salary_employees AS SELECT FROM employees WHERE salary > 5000;`
– `DROP VIEW high_salary_employees;`

🔟 Schema Reference

  • Database → Tables → Fields
  • Tables → Rows (Records)

You Should Know:

SQL Injection Prevention

-- Use parameterized queries to prevent SQL injection (Example in Python with SQLite3):
import sqlite3 
conn = sqlite3.connect('database.db') 
cursor = conn.cursor() 
cursor.execute("SELECT  FROM users WHERE username = ? AND password = ?", (user_input, pass_input)) 

Backup & Restore Databases

MySQL:

mysqldump -u username -p database_name > backup.sql 
mysql -u username -p database_name < backup.sql 

PostgreSQL:

pg_dump -U username -d dbname -f backup.sql 
psql -U username -d dbname -f backup.sql 

Indexing for Performance

CREATE INDEX idx_name ON employees(name); 
DROP INDEX idx_name; 

Transaction Control

BEGIN TRANSACTION; 
UPDATE accounts SET balance = balance - 100 WHERE user_id = 1; 
UPDATE accounts SET balance = balance + 100 WHERE user_id = 2; 
COMMIT; -- Or ROLLBACK if an error occurs 

Linux Database Management

 Check running PostgreSQL service 
systemctl status postgresql

Start/Stop MySQL 
sudo systemctl start mysql 
sudo systemctl stop mysql

Log into PostgreSQL 
psql -U postgres 

Windows SQL Server Commands

 Start SQL Server service 
Start-Service -Name "MSSQLSERVER"

Backup database using SQLCMD 
sqlcmd -S server_name -U username -Q "BACKUP DATABASE dbname TO DISK='C:\backup.bak'" 

What Undercode Say:

SQL remains a fundamental skill for database administrators, developers, and cybersecurity professionals. Mastering SQL queries, joins, and transactions enhances data manipulation efficiency. Always sanitize inputs to prevent SQL injection attacks. Database optimization techniques like indexing and proper transaction handling ensure high performance in production environments.

Expected Output:

A structured SQL cheatsheet with practical commands, security best practices, and database management techniques.

Prediction:

As databases grow in complexity, SQL will continue evolving with AI-driven query optimizations and blockchain-integrated databases. Learning advanced SQL (window functions, CTEs) will be crucial for data engineering roles.

References:

Reported By: Aaronsimca Sql – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram