Back to Data Science Notes
Topic #36

SQL DDL, DML, DCL, TCL & DQL

Understand the five core SQL statement categories—DDL, DML, DCL, TCL, and DQL—to correctly structure database interactions for schema definition, data manipulation, security, transaction control, and querying.

What it is

SQL statements are grouped into five functional categories based on their purpose. DDL (Data Definition Language) defines or modifies database structures like tables and indexes. DML (Data Manipulation Language) handles adding, changing, or deleting data within those structures. DCL (Data Control Language) manages user permissions and access rights. TCL (Transaction Control Language) groups multiple operations into atomic units to ensure data integrity. Finally, DQL (Data Query Language) retrieves data without modifying it. Note that some standards classify SELECT under DML, but distinguishing it as DQL clarifies its read-only nature in this context.

Why it matters

  • Security: Using DCL ensures only authorized users can view or modify sensitive data.
  • Data Integrity: TCL allows you to roll back changes if an error occurs mid-process, preventing partial updates.
  • Performance: Understanding DDL helps you create appropriate indexes before running heavy DQL queries.
  • Clarity: Separating structural changes (DDL) from data changes (DML) makes code reviews and migrations easier to manage.
  • Atomicity: Grouping related DML statements with TCL ensures all-or-nothing execution, critical for financial or inventory systems.

Syntax or steps

Each category uses specific keywords:
  • DDL: CREATE, ALTER, DROP, TRUNCATE.
  • DML: INSERT, UPDATE, DELETE.
  • DCL: GRANT, REVOKE.
  • TCL: BEGIN/START TRANSACTION, COMMIT, ROLLBACK.
  • DQL: SELECT.

Example

-- 1. DDL: Create a table structure
CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    salary DECIMAL(10, 2)
);

-- 2. DCL: Grant permission to a user
GRANT SELECT ON employees TO 'analyst_user';

-- 3. TCL & DML: Insert data within a transaction
BEGIN TRANSACTION;
INSERT INTO employees (id, name, salary) VALUES (1, 'Alice', 75000.00);
INSERT INTO employees (id, name, salary) VALUES (2, 'Bob', 80000.00);
COMMIT;

-- 4. DQL: Retrieve data
SELECT * FROM employees WHERE salary > 76000;
Explanation: The CREATE TABLE statement defines the schema (DDL). GRANT assigns read access to a specific user (DCL). The block between BEGIN TRANSACTION and COMMIT ensures both inserts succeed together or fail together (TCL wrapping DML). Finally, SELECT fetches rows meeting a condition (DQL).

Common mistakes

  • Forgetting COMMIT: Leaving a transaction open locks resources and may cause timeouts. Always end TCL blocks with COMMIT or ROLLBACK.
  • Using TRUNCATE instead of DELETE: TRUNCATE is DDL and cannot be rolled back in many databases, whereas DELETE is DML and respects transactions.
  • Over-granting permissions: Giving ALL PRIVILEGES via DCL when only SELECT is needed creates security risks.
  • Mixing DDL in Transactions: In some databases (like MySQL with MyISAM), DDL statements auto-commit, breaking your intended transaction logic. Check your DBMS behavior.

When to use it

CategoryUse When...Avoid When...
DDLSetting up new schemas or altering column types.Modifying individual row values.
DMLAdding, updating, or removing records.Changing table structure or permissions.
DCLOnboarding new users or revoking access.Querying or inserting data.
TCLEnsuring multiple DML steps succeed atomically.Running single, independent queries.
DQLReading data for reports or analysis.Writing or deleting data.

Practice

Guided Exercise: Write a script that creates a products table, grants SELECT to a guest, inserts two products inside a transaction, and selects all items. Challenge: Modify the transaction to include a ROLLBACK after the first insert. What happens to the second insert? Why? Hint: If you rollback before the second insert executes, neither product will exist in the final table because the entire transaction was undone.

Quick check

Question: Which category does the UPDATE statement belong to, and why? Answer: UPDATE belongs to DML because it modifies existing data within a table structure without changing the structure itself.

Summary

Mastering the distinction between DDL, DML, DCL, TCL, and DQL allows you to write safer, more efficient, and logically structured SQL. By applying the right tool for the job—such as using TCL for multi-step data integrity—you prevent common errors like partial updates or unauthorized access.

Want to go beyond the notes?

Join Coding Now Tech Institute's Data Science course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

SQL DDL, DML, DCL, TCL & DQL – FAQs

Quick answers about learning SQL DDL, DML, DCL, TCL & DQL in Data Science.

This free note from Coding Now Tech Institute explains SQL DDL, DML, DCL, TCL & DQL in Data Science — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Data Science topic on Coding Now Tech Institute, including SQL DDL, DML, DCL, TCL & DQL, is 100% free with no signup required.
With focused practice, most students grasp SQL DDL, DML, DCL, TCL & DQL in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now