Common Types of SQL

The most common types of SQL (Structured Query Language) are used for managing and manipulating relational databases. SQL commands are broadly categorized based on their functionality:

1. How the Families Fit Together

SQL Command Families DDLCREATE, ALTER, DROP, TRUNCATEDMLINSERT, UPDATE, DELETEDQLSELECTDCLGRANT, REVOKETCLCOMMIT, ROLLBACK, SAVEPOINTDatabase enginecatalog, tables, locks, logDDL changes structure, DML and DQL work on rows, DCL controls who may do it, TCL decides when changes become permanent.

The classification is about intent. DDL (Data Definition Language) changes the schema: tables, views, indexes. DML (Data Manipulation Language) changes rows. DQL (Data Query Language) reads them; many texts fold SELECT into DML, which is why it appears in both lists below. DCL (Data Control Language) manages privileges. TCL (Transaction Control Language) groups DML statements into atomic units. Whether DDL is transactional varies: PostgreSQL can roll back most DDL, while MySQL and Oracle commit implicitly before and after DDL statements.

1. Data Definition Language (DDL)

Purpose:

DDL commands are used to define and manage database structures such as tables, indexes, and schemas.

Common Commands:

2. Data Manipulation Language (DML)

Purpose:

DML commands are used for inserting, updating, deleting, and retrieving data within the database.

Common Commands:

3. Data Control Language (DCL)

Purpose:

DCL commands are used to control access to data in the database.

Common Commands:

4. Transaction Control Language (TCL)

Purpose:

TCL commands are used to manage transactions in the database, ensuring the integrity of data.

Common Commands:

5. Data Query Language (DQL)

Purpose:

DQL is primarily concerned with the retrieval of data from the database.

Common Commands:

7. Additional SQL Variants and Extensions

8. How a SELECT Is Evaluated

Logical Order of SELECT Evaluation FROM / JOINbuild rowsWHEREfilter rowsGROUP BYform groupsHAVINGfilter groupsSELECTcompute columnsORDER BY / LIMITsort, cutThis is why a column alias defined in SELECT cannot be used in WHERE, but can be used in ORDER BY.

SQL is declarative: you describe the result, and the optimizer chooses join orders, indexes and algorithms. The logical order in the diagram defines what a query means, even though the physical execution plan may differ. Understanding it explains several rules: aggregates cannot appear in WHERE (groups do not exist yet), HAVING filters after aggregation, and window functions are computed after grouping, so to filter on a window function result you wrap the query in a subquery or common table expression. Use EXPLAIN to see the physical plan.

9. Worked Example: All Five Families

-- DDL
CREATE TABLE accounts (
  id       INT PRIMARY KEY,
  owner    VARCHAR(100) NOT NULL,
  balance  DECIMAL(12,2) NOT NULL CHECK (balance >= 0)
);

-- DML
INSERT INTO accounts VALUES (1, 'Alice', 500.00), (2, 'Bob', 100.00);

-- TCL: transfer money atomically
BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
COMMIT;            -- or ROLLBACK; if either update failed

-- DQL
SELECT owner, balance,
       RANK() OVER (ORDER BY balance DESC) AS rnk
FROM accounts
WHERE balance > 0
ORDER BY rnk;

-- DCL
CREATE ROLE reporting;
GRANT SELECT ON accounts TO reporting;
REVOKE SELECT ON accounts FROM reporting;

The transfer shows why TCL matters: without the transaction, a failure between the two updates would lose 50 from Alice without crediting Bob. The CHECK constraint is enforced by the engine, so an overdraft makes the first UPDATE fail and the whole transaction can be rolled back.

10. Choosing the Right Command

GoalCommandNotes
Remove all rows quicklyTRUNCATEMinimal logging, resets storage; usually no WHERE and no row triggers
Remove some rowsDELETE ... WHERELogged per row, fires triggers, can be rolled back
Remove the table itselfDROP TABLERemoves data and definition
Insert or update in one stepMERGE / INSERT ... ON CONFLICTSyntax varies by engine
Partial undo inside a transactionSAVEPOINT / ROLLBACK TOKeeps earlier work in the transaction

11. Pitfalls