Open In App

SQL Commands | DDL, DQL, DML, DCL and TCL Commands

Last Updated : 12 Nov, 2025
Comments
Improve
Suggest changes
863 Likes
Like
Report

SQL commands are fundamental building blocks used to perform given operations on database. The operations include queries of data. creating a table, adding data to tables, dropping the table, modifying the table and set permission for users.

SQL Commands are mainly categorized into five categories: 

sql_commands
SQL Commands

1. DDL - Data Definition Language

DDL (Data Definition Language) consists of SQL commands that can be used for defining, altering and deleting database structures such as tables, indexes and schemas. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in the database

CommandDescriptionSyntax
CREATECreate database or its objects (table, index, function, views, store procedure and triggers)CREATE TABLE table_name (column1 data_type, column2 data_type, ...);
DROPDelete objects from the databaseDROP TABLE table_name;
ALTERAlter the structure of the databaseALTER TABLE table_name ADD COLUMN column_name data_type;
TRUNCATERemove all records from a table, including all spaces allocated for the records are removedTRUNCATE TABLE table_name;
COMMENTAdd comments to the data dictionaryCOMMENT ON TABLE table_name IS 'comment_text';
RENAMERename an object existing in the databaseRENAME TABLE old_table_name TO new_table_name;

Example:

CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE
);

In this example, a new table called employees is created with columns for employee ID, first name, last name and hire date.

2. DQL - Data Query Language

DQL is used to fetch data from the database. The main command is SELECT, which retrieves records based on the query. The output is returned as a result set (a temporary table) that can be viewed or used in applications.

CommandDescriptionSyntax

SELECT

It is used to retrieve data from the database

SELECT column1, column2, ...FROM table_name WHERE condition;

FROM

Indicates the table(s) from which to retrieve data.

SELECT column1
FROM table_name;

WHERE

Filters rows before any grouping or aggregation

SELECT column1
FROM table_name
WHERE condition;

GROUP BY

Groups rows that have the same values in specified columns.

SELECT column1, AVG_FUNCTION(column2)
FROM table_name
GROUP BY column1;

HAVING

Filters the results of GROUP BY

SELECT column1, AVG_FUNCTION(column2)
FROM table_name
GROUP BY column1
HAVING condition;

DISTINCT

Removes duplicate rows from the result set

SELECT DISTINCT column1, column2, ...
FROM table_name;

ORDER BY

Sorts the result set by one or more columns

SELECT column1
FROM table_name
ORDER BY column1 [ASC | DESC];

LIMIT

By default, it sorts in ascending order unless specified as DESC

SELECT * FROM table_name LIMIT number;

Note: DQL has only one command, SELECT. Other terms like FROM, WHERE, GROUP BY, HAVING, ORDER BY, DISTINCT and LIMIT are clauses of SELECT, not separate commands.

Example:

SELECT first_name, last_name, hire_date
FROM employees
WHERE department = 'Sales'
ORDER BY hire_date DESC;

This query retrieves employees first and last names, along with their hire dates, from the employees table, specifically for those in the 'Sales' department, sorted by hire date.

3. DML - Data Manipulation Language

DML commands are used to manipulate the data stored in database tables. With DML, you can insert new records, update existing ones, delete unwanted data or retrieve information.

CommandDescriptionSyntax
INSERTInsert data into a tableINSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
UPDATEUpdate existing data within a tableUPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
DELETEDelete records from a database tableDELETE FROM table_name WHERE condition;

Example:

INSERT INTO employees (first_name, last_name, department)
VALUES ('Jane', 'Smith', 'HR');

This query inserts a new record into employees table with first name 'Jane', last name 'Smith' and department 'HR'.

4. DCL - Data Control Language

DCL (Data Control Language) includes commands such as GRANT and REVOKE which mainly deal with the rights, permissions and other controls of the database system. These commands are used to control access to data in the database by granting or revoking permissions.

CommandDescriptionSyntax
GRANTAssigns new privileges to a user account, allowing access to specific database objects, actions or functions.GRANT privilege_type [(column_list)] ON [object_type] object_name TO user [WITH GRANT OPTION];
REVOKERemoves previously granted privileges from a user account, taking away their access to certain database objects or actions.REVOKE [GRANT OPTION FOR] privilege_type [(column_list)] ON [object_type] object_name FROM user [CASCADE];

Example:

GRANT SELECT, UPDATE ON employees TO user_name;

This command grants the user user_name the permissions to select and update records in the employees table.

5. TCL - Transaction Control Language

Transactions group a set of tasks into a single execution unit. Each transaction begins with a specific task and ends when all the tasks in the group are successfully completed. If any of the tasks fail, transaction fails. Therefore, a transaction has only two results: success or failure.

CommandDescriptionSyntax
BEGIN TRANSACTIONStarts a new transactionBEGIN TRANSACTION [transaction_name];
COMMITSaves all changes made during the transactionCOMMIT;
ROLLBACKUndoes all changes made during the transactionROLLBACK;
SAVEPOINTCreates a savepoint within the current transactionSAVEPOINT savepoint_name;

Example:

BEGIN TRANSACTION;
UPDATE employees SET department = 'Marketing' WHERE department = 'Sales';
SAVEPOINT before_update;
UPDATE employees SET department = 'IT' WHERE department = 'HR';
ROLLBACK TO SAVEPOINT before_update;
COMMIT;

In this example, a transaction is started, changes are made and a savepoint is set. If needed, the transaction can be rolled back to the savepoint before being committed.

Suggested Quiz
10 Questions

What is the primary purpose of Data Control Language (DCL) commands in SQL?

  • A

    To define database schemas (e.g., CREATE TABLE)

  • B

    To manage user permissions (e.g., GRANT, REVOKE)

  • C

    To manipulate data (e.g., INSERT, UPDATE)

  • D

    To handle transactions (e.g., COMMIT, ROLLBACK)

Explanation:

DCL commands control access to database objects by granting or revoking privileges, ensuring security and proper authorization.

Which SQL command category would you use to implement role-based access control in a database?

  • A

    DDL

  • B

    DML

  • C

    DCL

  • D

    TCL

Explanation:

DCL (Data Control Language) commands like GRANT and REVOKE manage user permissions, making them essential for access control.

What is the primary purpose of the SAVEPOINT command in TCL?

  • A

    To permanently save transaction changes

  • B

    To create a rollback point within a transaction

  • C

    To terminate a transaction immediately

  • D

    To grant temporary permissions

Explanation:

SAVEPOINT allows partial rollback of transactions to a defined point without affecting the entire transaction.

Which DDL command would permanently remove both table structure and data?

  • A

    TRUNCATE

  • B

    DELETE

  • C

    DROP

  • D

    ALTER

Explanation:

DROP deletes the table and its schema, while TRUNCATE/DELETE only remove data, and ALTER modifies structure.

In DML, what distinguishes the LOCK command from other DML operations?

  • A

    It modifies data values

  • B

    It controls concurrent access to tables

  • C

    It creates backup copies

  • D

    It enforces data integrity constraints

Explanation:

LOCK manages concurrency by restricting simultaneous access during transactions.

What happens when a ROLLBACK command is executed without a SAVEPOINT?

  • A

    Only the last operation is undone

  • B

    All changes in the current transaction are undone

  • C

    The database reverts to its last backup

  • D

    Nothing happens

Explanation:


Which DQL feature allows filtering of grouped data after aggregation?

  • A

    WHERE

  • B

    ORDER BY

  • C

    HAVING

  • D

    JOIN

Explanation:

HAVING filters grouped data post-aggregation, while WHERE filters before grouping.

What is the critical difference between CHAR and VARCHAR in DDL?

  • A

    CHAR supports Unicode, VARCHAR doesn't

  • B

    CHAR is fixed-length, VARCHAR is variable-length

  • C

    VARCHAR has larger maximum storage

  • D

    CHAR allows binary data

Explanation:

CHAR pads values to fixed lengths, while VARCHAR adjusts storage to actual data size.

Which command combination would you use to modify a table column's data type?

  • A

    UPDATE + SET

  • B

    ALTER + MODIFY

  • C

    CHANGE + COLUMN

  • D

    ALTER + DROP

Explanation:

ALTER TABLE with MODIFY COLUMN changes column definitions (syntax varies by DBMS).

In transaction management, what does the COMMIT command guarantee?

  • A

    Data is backed up

  • B

    Changes become permanent

  • C

    Locks are released immediately

  • D

    All queries are optimized

Explanation:

COMMIT finalizes transaction changes, making them durable in the database.

Quiz Completed Successfully
Your Score :   2/10
Accuracy :  0%
Login to View Explanation
1/10 1/10 < Previous Next >

Explore