SQL Statements
SQL statements are the individual commands you use to interact with the database. Each statement tells the database what action to perform — from retrieving data to modifying the structure.
✍️ What Are SQL Statements?
A SQL statement is a complete instruction made up of one or more clauses. These statements can be grouped into several categories based on their purpose:
- Data Query Language (DQL) – Retrieve data (
SELECT) - Data Manipulation Language (DML) – Modify data (
INSERT,UPDATE,DELETE) - Data Definition Language (DDL) – Define or alter database objects (
CREATE,ALTER,DROP) - Data Control Language (DCL) – Manage permissions (
GRANT,REVOKE) - Transaction Control Language (TCL) – Manage transactions (
COMMIT,ROLLBACK,SAVEPOINT)
⚙️ Common SQL Statements Overview
| Category | Statement | Purpose | Example |
|---|---|---|---|
| DQL | SELECT | Retrieve data from tables | SELECT * FROM employees; |
| DML | INSERT | Add new records | INSERT INTO employees (name) VALUES ('Alice'); |
UPDATE | Modify existing records | UPDATE employees SET age = 30 WHERE id = 1; | |
DELETE | Remove records | DELETE FROM employees WHERE age < 20; | |
| DDL | CREATE TABLE | Create a new table | CREATE TABLE employees (id INT, name VARCHAR(50)); |
ALTER TABLE | Change table structure | ALTER TABLE employees ADD COLUMN salary INT; | |
DROP TABLE | Delete a table | DROP TABLE employees; | |
| DCL | GRANT | Give permissions to users | GRANT SELECT ON employees TO user1; |
REVOKE | Remove permissions | REVOKE SELECT ON employees FROM user1; | |
| TCL | COMMIT | Save all changes in a transaction | COMMIT; |
ROLLBACK | Undo changes since last COMMIT | ROLLBACK; |
🧩 Statement Syntax Basics
Every SQL statement has:
- A command keyword (e.g.,
SELECT,INSERT) - Optional clauses (e.g.,
WHERE,ORDER BY) - Terminates with a semicolon (
;)
Example:
SELECT name, age
FROM employees
WHERE age > 25
ORDER BY age DESC;💡 Tips to Remember
- Always end statements with a semicolon.
- Use uppercase for commands to improve readability.
- Write one statement per query execution (unless your database supports batch execution).
- Use comments (we’ll cover next) to explain complex queries.
🧠 Quick Recap
| Key Point | Details |
|---|---|
| SQL Statement | Command instructing database action |
| Categories | DQL, DML, DDL, DCL, TCL |
| End with semicolon | Marks completion of statement |
| Write clear and readable | Use uppercase commands and formatting |
