SQL: Structured Query LanguageSQL FundamentalsSQL Command Categories
What are the main categories of SQL commands?
Question 9 of 30 · Updated
On this page
- 1. SQL command categories at a glance
- 2. DDL — Data Definition Language
- 3. DML — Data Manipulation Language
- 4. DQL — Data Query Language
- 5. DCL — Data Control Language
- 6. TCL — Transaction Control Language
- 7. DELETE vs TRUNCATE vs DROP
- 8. Testing and verification
- 9. Interview-ready answer
- Quick interview follow-ups
The main categories of SQL commands are DDL (Data Definition Language), DML (Data Manipulation Language), DQL (Data Query Language), DCL (Data Control Language), and TCL (Transaction Control Language). These categories group statements by their primary purpose: defining database structures, changing data, retrieving data, controlling access, and managing transactions.
This five-category classification is a common teaching and interview convention, not a universally exclusive classification defined identically by every database product. Some references classify SELECT as part of DML rather than treating DQL as a separate category. The important distinction is what each statement does.
1. SQL command categories at a glance#
| Category | Full form | Primary purpose | Common examples |
|---|---|---|---|
| DDL | Data Definition Language | Define or change database objects | CREATE, ALTER, DROP, TRUNCATE |
| DML | Data Manipulation Language | Insert, modify or remove stored rows | INSERT, UPDATE, DELETE, MERGE |
| DQL | Data Query Language | Retrieve or compute data | SELECT |
| DCL | Data Control Language | Grant or revoke access privileges | GRANT, REVOKE |
| TCL | Transaction Control Language | Manage transaction boundaries and outcomes | BEGIN, COMMIT, ROLLBACK, SAVEPOINT |
Think of a library application: DDL creates the books table, DML records new books and changes stock, DQL retrieves available books, DCL controls who can access the table, and TCL ensures a group of changes is committed or undone together.
2. DDL — Data Definition Language#
DDL defines database objects and their structure. Creating a table establishes column names, data types and constraints; altering a table changes its definition. DROP removes an object, and TRUNCATE removes all rows from a table while preserving the table definition. TRUNCATE is commonly taught under DDL, although it operates on data and its classification and transaction behavior vary across products.
Use a fresh, disposable database for the following examples. Do not run them against an existing production library database.
SQLCREATE TABLE books ( book_id INTEGER PRIMARY KEY, title TEXT NOT NULL, copies INTEGER NOT NULL CHECK (copies >= 0) ); ALTER TABLE books ADD COLUMN shelf TEXT;
The first statement creates a table with three columns. The second adds a nullable fourth column named shelf. No book records have been added yet; DDL can define a structure independently of the stored data.
To demonstrate removing an object without destroying the main example, create and drop a separate scratch table:
SQLCREATE TABLE scratch (id INTEGER); DROP TABLE scratch;
After DROP TABLE, the scratch table no longer exists. DROP can remove both a table definition and its data, and may be blocked by dependencies. Review dependencies, permissions and backups before destructive schema changes. Do not assume all DDL automatically commits: PostgreSQL supports rollback of many DDL operations in transactions, whereas some other engines implicitly commit many DDL statements.
3. DML — Data Manipulation Language#
DML changes the data stored in existing tables. Common commands include INSERT to add records, UPDATE to modify records, DELETE to remove records, and MERGE to perform conditional changes in engines that support it.
Continue using the books table created above:
SQLINSERT INTO books (book_id, title, copies) VALUES (1, 'SQL Basics', 3), (2, 'Data Modeling', 1);
The table now contains two book records:
| book_id | title | copies |
|---|---|---|
| 1 | SQL Basics | 3 |
| 2 | Data Modeling | 1 |
Update the number of copies of book 1 and remove book 2:
SQLUPDATE books SET copies = 4 WHERE book_id = 1; DELETE FROM books WHERE book_id = 2;
Each statement affects one row in this example. Neither changes the table's column definitions. Constraints still apply to DML operations; for example, a negative number of copies violates the CHECK constraint.
Safety: UPDATE or DELETE without an appropriate WHERE condition can affect every row. Before modifying production data, verify the target rows, expected affected-row count and transaction strategy. A successful command does not necessarily mean a row was changed: a WHERE predicate might match nothing.
4. DQL — Data Query Language#
DQL is the common teaching label for SQL statements that retrieve or compute information. SELECT can return columns, filter rows, combine tables, aggregate values and calculate expressions.
SQLSELECT book_id, title, copies FROM books WHERE copies > 0 ORDER BY book_id;
After the preceding DML statements, the output is:
| book_id | title | copies |
|---|---|---|
| 1 | SQL Basics | 4 |
This ordinary SELECT reads the available inventory without changing the stored records. Some SQL references include SELECT in DML because it manipulates information in the broader sense. In interviews, you can describe DQL as the retrieval subset in the familiar five-category teaching model.
Do not infer the category solely from the presence of a result set. PostgreSQL's INSERT, UPDATE and DELETE can also return rows using RETURNING. Conversely, some SELECT statements invoke functions with side effects or acquire locks, so SELECT is not an unconditional guarantee of harmless execution.
5. DCL — Data Control Language#
DCL controls database access through privileges. GRANT assigns a privilege to a role, and REVOKE removes a grant. Database authorization is different from changing the data itself.
The following PostgreSQL-specific example assumes a newly created, dedicated role named library_reader, no other grants or role memberships, and an administrator operating in a disposable database. These DCL commands are illustrative and were not executed:
SQLCREATE ROLE library_reader NOLOGIN; GRANT USAGE ON SCHEMA public TO library_reader; GRANT SELECT ON TABLE public.books TO library_reader; REVOKE SELECT ON TABLE public.books FROM library_reader;
CREATE ROLE is setup DDL or an administrative command; the GRANT and REVOKE statements demonstrate DCL. The table must exist in the public schema for the example. USAGE allows access to objects in the namespace subject to individual object permissions. SELECT grants table-read permission, but does not by itself grant INSERT, UPDATE or DELETE. REVOKE removes the direct SELECT grant; other privileges from PUBLIC, role memberships, ownership or other grants can still affect effective access.
SQLite does not provide PostgreSQL-style GRANT/REVOKE or database roles, so this section cannot be tested in SQLite. In Supabase and other API-backed systems, grants and row-level security are separate layers; use least-privilege grants and appropriate row-level policies for exposed tables. The role here is a hypothetical dedicated teaching role, not an anonymous or authenticated application role. No actual role or privilege configuration was modified for this lesson.
6. TCL — Transaction Control Language#
TCL manages transactions, which are logical units of work that can contain multiple statements. BEGIN (or START TRANSACTION in supporting systems) opens a transaction, COMMIT makes its changes committed, ROLLBACK undoes its uncommitted changes, and SAVEPOINT creates a marker to which a transaction can partially roll back.
Suppose the library currently has four copies of SQL Basics. A transaction attempts two stock reductions but cancels the second one:
SQLBEGIN; UPDATE books SET copies = copies - 1 WHERE book_id = 1; SAVEPOINT before_second_change; UPDATE books SET copies = copies - 1 WHERE book_id = 1; SELECT copies FROM books WHERE book_id = 1; ROLLBACK TO SAVEPOINT before_second_change; RELEASE SAVEPOINT before_second_change; SELECT copies FROM books WHERE book_id = 1; COMMIT; SELECT copies FROM books WHERE book_id = 1;
| Point in the transaction | Copies |
|---|---|
| Before BEGIN | 4 |
| After both reductions | 2 |
| After rollback to savepoint | 3 |
| After COMMIT | 3 |
The two UPDATE commands are DML; the SELECT commands are DQL; BEGIN, SAVEPOINT, ROLLBACK TO, RELEASE and COMMIT are TCL. One transaction can contain statements from multiple categories. COMMIT preserves the first reduction; the second is undone. A savepoint is not a separate committed transaction.
Now demonstrate a full rollback:
SQLBEGIN; UPDATE books SET copies = 0 WHERE book_id = 1; ROLLBACK; SELECT copies FROM books WHERE book_id = 1;
The result is still 3 because the uncommitted update was undone. A rollback does not normally undo an already committed transaction. Transaction and DDL semantics vary between products, and many clients use autocommit unless an explicit transaction is started.
Production caveat: The library example illustrates transaction boundaries, not a complete book checkout system. A real application would need concurrency control, correct stock validation, reservation or checkout records, and error handling.
7. DELETE vs TRUNCATE vs DROP#
These are frequently confused because they all can remove data.
| Command | Typical category | Effect | WHERE filter? | Table remains? |
|---|---|---|---|---|
| DELETE | DML | Deletes matching rows, or all rows without a filter | Yes | Yes |
| TRUNCATE | Commonly DDL | Removes all rows from specified tables | No | Yes |
| DROP TABLE | DDL | Removes the table object and its data | No | No |
In PostgreSQL, TRUNCATE can be rolled back if the containing transaction is rolled back, and it has different locking, foreign-key and trigger behavior from DELETE. Other engines may implicitly commit it. SQLite does not support TRUNCATE TABLE. The TRUNCATE comparison is conceptual and was not executed. Never choose between these commands using only a generic speed claim; consider desired semantics and engine-specific behavior.
8. Testing and verification#
The executable DDL, DML, DQL and TCL examples were tested using SQLite 3.46.1 in an isolated in-memory database with explicit transaction control. Ten assertions passed: added-column metadata, inserted records, UPDATE and DELETE row counts, filtered SELECT output, state before savepoint rollback, state after rollback to savepoint, committed state, full rollback state, and removal of the scratch table.
The examples use common SQL syntax intended to work in PostgreSQL as well. PostgreSQL execution of the DDL, DML, DQL and TCL examples was not independently performed. The PostgreSQL-specific DCL commands and discussion of TRUNCATE are clearly marked as untested. No educational DDL, DML or privilege changes were executed against production tables.
9. Interview-ready answer#
SQL commands are commonly grouped into five categories. DDL defines or changes database structures using CREATE, ALTER and DROP. DML changes stored data using INSERT, UPDATE and DELETE. DQL retrieves information using SELECT. DCL manages permissions through GRANT and REVOKE. TCL controls transactions through BEGIN, COMMIT, ROLLBACK and SAVEPOINT. These categories describe the main purpose of the statement rather than its implementation. Some classifications include SELECT under DML, and transaction behavior differs across database engines.
Quick interview follow-ups#
- Is SELECT DML or DQL? Both conventions exist: DQL is a common separate teaching category, while broader DML classifications include SELECT.
- Does DELETE remove a table? No. It removes rows; DROP TABLE removes the table itself.
- Is COMMIT a DML command? No. It controls a transaction that may contain DML statements.
- Does DDL always auto-commit? No. It depends on the database and specific operation.
- Are GRANT and REVOKE supported in every SQL engine? No. Authorization mechanisms differ across products.
References
- 1.Supabase — Postgres Rolessupabase.com
- 2.Supabase — Securing your APIsupabase.com
- 3.PostgreSQL — SQL Commands (not live-verified)postgresql.org
- 4.PostgreSQL — Transactions (not live-verified)postgresql.org
- 5.PostgreSQL — TRUNCATE (not live-verified)postgresql.org
Related questions
Explore other topics:Forward Deployed EngineeringPythonAI System DesignMachine Learning