Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 5/ 160
01. SQL Foundations & Relational Concepts

SQL Command Categories (DDL, DML, DQL, DCL, TCL)

Group SQL statements into DDL, DML, DQL, DCL, and TCL with clear mental models.

BeginnerDDL · DML / DQL3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Some commands build tables, some change the data in them, some read it, some give people permission, and some save or undo a batch of changes.

Think of it like…

Running a library: DDL builds or removes shelves, DML adds, edits or removes books, DQL is reading, DCL hands out library cards with different access, and TCL is deciding whether today's changes to the catalogue are final or should be undone.

Where you’ve seen it

A release adds a column (DDL: ALTER TABLE), backfills it (DML: UPDATE), grants the reporting role access (DCL: GRANT), all wrapped in BEGIN … COMMIT (TCL). In PostgreSQL the whole migration can be rolled back if one step fails; in MySQL the ALTER commits immediately.

Words you’ll hear

DDL
CREATE, ALTER, DROP, TRUNCATE, RENAME: change the schema.
DML / DQL
INSERT, UPDATE, DELETE, MERGE / SELECT: change or read rows.
DCL
GRANT, REVOKE: control who can do what.
TCL
BEGIN, COMMIT, ROLLBACK, SAVEPOINT: control transactions.

Visual diagram

Loading interactive diagram

Theory & example queries

What each category does

DDL defines and changes database objects: tables, indexes, views, schemas. DML changes the rows inside tables, and DQL (SELECT) reads them; some textbooks fold SELECT into DML, so mention both conventions. DCL grants and revokes privileges on objects to users and roles.

TCL controls transactions: BEGIN (or START TRANSACTION) opens one, COMMIT makes its changes permanent, ROLLBACK undoes them, and SAVEPOINT marks a point you can roll back to without abandoning the whole transaction.

One of eachQuery example
1Input dataStart from the source rows or state that the statement sees.
This statement works with the current schema, configuration, or database state rather than a row dataset. The command still follows the same input → query → result flow.
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1CREATE TABLE notes (id bigint PRIMARY KEY, body text);
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/1
query.sql
1INSERT INTO2  notes3VALUES4  (1, 'hi');
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/4
query.sql
1SELECT2  *3FROM4  notes;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Project and calculate

Calculate the selected expressions and return the requested output columns.

1/4
query.sql
1GRANT2SELECT3  ON notes TO analyst;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · SQL expression

Read this line together with the surrounding statement as part of the same SQL expression.

1/3
query.sql
1COMMIT;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · SQL expression

Read this line together with the surrounding statement as part of the same SQL expression.

1/1
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative sequence
CREATE TABLE INSERT 0 1 SELECT row: id=1, body='hi' GRANT COMMIT

Note: Command tags and COMMIT behavior vary by client and dialect; the SELECT result reflects the single inserted row.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

(DDL) ·

(DML) ·

(DQL) ·

(DCL) ·

(TCL)

Key takeaway: Structure, data, reads, permissions, transactions.

Why the category matters in practice

DDL usually takes strong locks on the table it changes, so an ALTER on a busy table can block every query behind it. Large DDL in production is planned work: off-peak, with lock timeouts, or using online techniques such as CREATE INDEX CONCURRENTLY.

Transaction behaviour differs by database. PostgreSQL supports transactional DDL, so a failed migration can be rolled back entirely. MySQL and Oracle commit implicitly before and after most DDL statements, so a migration that fails halfway leaves earlier steps applied. TRUNCATE follows the same DDL rules.

A safe migration pattern in PostgreSQLQuery example
1Input dataStart from the source rows or state that the statement sees.
orders
3 rows
orders sample rows
idcustomer_idstatusamountcoupon_code
1011paid249NULL
1022paid120WELCOME10
1031shipped89.5NULL
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1BEGIN;23SET4  LOCAL lock_timeout = '5s';56ALTER TABLE orders7ADD COLUMN source text;89UPDATE orders10SET11  source = 'web'12WHERE13  source IS NULL;1415COMMIT;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Start transaction

Open a transaction so changes can be checked and committed or rolled back together.

1/15
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative PostgreSQL transaction
BEGIN SET ALTER TABLE UPDATE 120 COMMIT

Note: The 120-row update count is illustrative. PostgreSQL can roll back the entire sequence if a step fails before COMMIT.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

If any step fails, nothing is applied.

Key takeaway: DDL locks hard and, outside PostgreSQL, commits on its own.

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Schema

Define the structure (DDL)

Create tables, indexes and constraints, and change them later with ALTER.

SQL at this stepReadable preview
1CREATE TABLE … /2ALTER TABLE …
What to watch at this step
Locks
strong
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

SQL statements fall into five groups. DDL (Data Definition) changes structure: CREATE, ALTER, DROP, TRUNCATE. DML (Data Manipulation) changes rows: INSERT, UPDATE, DELETE, MERGE. DQL (Data Query) reads: SELECT. DCL (Data Control) manages permissions: GRANT, REVOKE. TCL (Transaction Control) groups changes: BEGIN, COMMIT, ROLLBACK, SAVEPOINT.

Why it matters: It's a classic fresher interview question, and the categories have real consequences: DDL takes stronger locks and, in MySQL and Oracle, commits the open transaction automatically, while DML can be rolled back. Knowing which group a command belongs to tells you how risky it is to run in production.

When to reach for it

  • Answering "what are DDL, DML, DCL and TCL?"
  • Planning database migrations and releases
  • Setting up roles and permissions

When to avoid or be careful

  • Running ALTER TABLE on a busy production table without a lock timeout
  • Assuming DDL can be rolled back in MySQL
  • Giving application users DDL privileges they don't need

How the categories behave

DDL
Best for: Creating and changing tables, indexes and views

Strengths: Defines structure and constraints the database enforces.

Watch out: Strong locks; implicit commits in MySQL and Oracle.

DML / DQL
Best for: Day-to-day reads and writes

Strengths: Row-level locking; fully transactional.

Watch out: Large DML (millions of rows) still needs batching.

DCL / TCL
Best for: Security and grouping changes safely

Strengths: Least privilege and atomic changes.

Watch out: Forgetting COMMIT, or leaving transactions open, causes subtle problems.

Interview signals & numbers to know

When this topic comes up

  • “Difference between DDL and DML”
  • “Is TRUNCATE DDL or DML?”
  • “Can you roll back a CREATE TABLE?”
  • Database migrations
  • GRANT / REVOKE

Benchmarks & rules of thumb

5 -
categories: DDL, DML, DQL, DCL, TCL
TRUNCATE -
DDL
PostgreSQL -
supports transactional DDL

Common mistakes & follow-ups

Common interview mistakes

  • Calling TRUNCATE a DML command.
  • Expecting a failed MySQL migration to roll back its earlier ALTER statements.
  • Forgetting to COMMIT in a client with autocommit turned off, so changes vanish when the session closes.

Likely interviewer follow-ups

  • Why is TRUNCATE considered DDL?
  • Can DDL be rolled back? Does it depend on the database?
  • Which category does SELECT belong to, and why do sources disagree?
Interview tip: List the five categories with two commands each, then add one practical consequence (DDL locks and implicit commits). That extra sentence moves the answer from memorised to experienced.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply SQL Command Categories (DDL, DML, DQL, DCL, TCL) with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Installing PostgreSQL / MySQL & Database Clients (DBeaver, pgAdmin)
Next topic
Core Data Types (Numeric, String/Text, Boolean, Date/Time, UUID)