SQL Commands (DDL, DML, DQL, DCL, TCL) with Examples

Learn about SQL commands, their types (DDL, DML, DQL, DCL, TCL), syntax, and examples for managing and manipulating relational databases effectively.

SQL Commands Featured Image

SQL(Structured Query Language) is the standard language for managing and manipulating relational databases. SQL commands are the instructions used to communicate with a Relational Database Management System (RDBMS) to create database structures, manipulate data, and control access.

Whether you need to build a new table, update customer records, or retrieve a specific dataset, you will use specific subsets of SQL. This comprehensive guide covers the five main categories of SQL commands, such as DDL, DML, DQL, DCL, and TCL, complete with definitions, syntax, and practical code examples.

Academy Pro

SQL Course

Master SQL and Database management with this SQL course: Practical training with guided projects, AI support, and expert instructors.

7 Hrs
2 Projects
Take SQL Course Now

SQL Commands Cheat Sheet

A quick reference for the most common commands.

CommandCategoryDescription
ALTERDDLAdds or modifies columns in a table.
COMMITTCLSaves changes permanently.
CREATEDDLCreates a new table or database.
DELETEDMLRemoves rows from a table.
DROPDDLDeletes a table entirely.
GRANTDCLGives a user access permissions.
INSERTDMLAdds new rows to a table.
MERGE DML Conditionally inserts, updates, or deletes data (Upsert)
ROLLBACKTCLUndoes recent changes.
SELECTDQLRetrieves data.
TRUNCATEDDLWipes all data but keeps the table.
UPDATEDMLEdits existing data.
Academy Pro

SQL Course

Master SQL and Database management with this SQL course: Practical training with guided projects, AI support, and expert instructors.

7 Hrs
2 Projects
Take SQL Course Now

Data Definition Language (DDL)

Data Definition Language (DDL) commands define and manage the structure (schema) of your database. They allow you to create, modify, or delete database objects such as tables, indexes, and views.

Note: In most relational databases (like Oracle and MySQL), DDL commands are auto-committed. This means once you execute a DROP or TRUNCATE command, the change is permanent and cannot be undone using a ROLLBACK command.

Data Definition Language (DDL) Commands

1. CREATE:

Creates a new database or table.

Example:

CREATE TABLE Employees (
    ID INT PRIMARY KEY,
    Name VARCHAR(100),
    Age INT,
    Department VARCHAR(50)
);

2. ALTER:

Modifies the structure of an existing table (e.g., adding, deleting, or modifying columns).

Example:

ALTER TABLE Employees
ADD Salary DECIMAL(10, 2);

3. DROP:

Deletes a table and all its data permanently.

Example:

DROP TABLE Employees;

4. TRUNCATE:

Deletes all data inside a table but keeps the structure.

Example:

TRUNCATE TABLE Employees;

5. RENAME:

Changes the name of a database object (like a table or column).

Example:

RENAME TABLE Employees TO Staff;

6. COMMENT:

Adds or modifies a comment on a database object (like a table or column).

Example:

COMMENT ON COLUMN Employees.Name IS 'Employee Name';

Data Manipulation Language (DML)

DML commands manage the data within your database. They allow you to add, edit, or remove records in the tables, working with the data inside the structure created by DDL.

Data Manipulation Language (DML) Commands

7. INSERT:

Adds new records to a table.

Example:

INSERT INTO Employees (ID, Name, Age, Department) 
VALUES (1, 'John Doe', 30, 'HR');

8. UPDATE:

Modifies existing records in a table.

Example:

UPDATE Employees
SET Age = 31
WHERE ID = 1;

9. DELETE:

Removes records from a table.

Example:

DELETE FROM Employees
WHERE ID = 1;

Data Control Language (DCL)

DCL commands manage security, access, and permissions. They control who can view or edit data, ensuring only authorized users can modify sensitive information.

Data Control Language (DCL) Commands.

10. GRANT:

This command gives permissions to users or roles. You can grant permissions such as SELECT (view), INSERT (add data), UPDATE (modify data), and DELETE (remove data).

Example:

GRANT SELECT, INSERT ON Employees TO user123;

11. REVOKE:

This command removes permissions from users or roles. It takes away rights that were previously granted. You can revoke permissions like SELECT, INSERT, UPDATE, or DELETE.

Example:

REVOKE SELECT, INSERT ON Employees FROM user123;

12. DENY:

While GRANT and REVOKE are universal standard SQL commands, DENY is specifically used in Microsoft SQL Server (T-SQL). It explicitly prevents a user or role from gaining a permission, overriding any permissions they might have inherited from other group memberships.

DENY DELETE ON Employees TO user123;

Data Query Language (DQL)

DQL commands are used to retrieve data from the database. It focuses only on reading information, not changing it.

The primary DQL command is SELECT. It can be combined with clauses like WHERE, JOIN, and HAVING to refine results and analyze data in detail.

13. SELECT:

The SELECT command retrieves data from a database using clauses such as WHERE, JOIN, and HAVING to refine and filter the results.

Example:

SELECT Name, Age FROM Employees WHERE Department = 'Sales';

Use of SELECT with different clauses:

-- SELECT + WHERE
SELECT Name, Age FROM Employees WHERE Department = 'Sales';
-- SELECT + WHERE + ORDER BY
SELECT Name, Salary FROM Employees WHERE Salary > 50000 ORDER BY Salary DESC;
-- SELECT + JOIN
SELECT Employees.Name, Departments.Name AS DeptName FROM Employees JOIN Departments ON Employees.DeptID = Departments.ID;
-- SELECT + GROUP BY + HAVING
SELECT Department, COUNT(*) AS TotalEmployees FROM Employees GROUP BY Department HAVING COUNT(*) > 10;
-- SELECT + DISTINCT
SELECT DISTINCT Department FROM Employees;
-- SELECT + ORDER BY + LIMIT
SELECT Name, Age FROM Employees ORDER BY Age DESC LIMIT 5;

Transaction Control Language (TCL)

TCL is a set of SQL commands used to manage transactions. A transaction is a group of SQL operations that should be treated as a single unit. TCL ensures that either all actions in a transaction are saved, or none are saved if something goes wrong.

Transaction Control Language (TCL) Commands

The TCL commands are:

14. COMMIT

The COMMIT command permanently saves all changes made during the current transaction to the database. Once committed, the changes are visible to other users and cannot be rolled back.

-- Note: Syntax for starting a transaction varies by RDBMS. 
-- SQL Server uses BEGIN TRANSACTION, PostgreSQL uses BEGIN, and MySQL uses START TRANSACTION.
BEGIN TRANSACTION;
UPDATE employees SET salary = 5000 WHERE id = 1;
COMMIT;

In this example, the salary update is saved to the database.

15. ROLLBACK

The ROLLBACK command undoes all mistaken changes made during the current transaction.

Example:

BEGIN TRANSACTION;
UPDATE employees SET salary = 5000 WHERE id = 1;
ROLLBACK;

Here, the salary update is undone, and the data remains unchanged.

16. SAVEPOINT

The SAVEPOINT command sets a point within a transaction that you can roll back to if needed.

Example:

BEGIN TRANSACTION;
UPDATE employees SET salary = 5000 WHERE id = 1;
SAVEPOINT salary_update;
UPDATE employees SET salary = 6000 WHERE id = 2;
ROLLBACK TO salary_update;
COMMIT;

In this example, if the second update causes an issue, you can roll back to the salary_update point, keeping the first update intact.

17. SET TRANSACTION

The SET TRANSACTION command is used to set properties for the current transaction, such as isolation level.

Example:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
UPDATE employees SET salary = 5000 WHERE id = 1;
COMMIT;

This sets the isolation level to SERIALIZABLE, ensuring that no other transactions can access the data until the current transaction completes.

Other SQL Resources:

Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Go Beyond Learning. Get Job-Ready.

Build in-demand skills for today's jobs with free expert-led courses and practical AI tools.

Explore All Courses
Scroll to Top