0% found this document useful (0 votes)
2 views11 pages

SQL Commands

The document provides an overview of SQL commands categorized into five types: DDL (Data Definition Language), DML (Data Manipulation Language), DQL (Data Query Language), DCL (Data Control Language), and TCL (Transaction Control Language). Each category includes commands with examples, explaining their purpose such as creating tables, manipulating data, querying data, controlling access, and managing transactions. It serves as a foundational guide for understanding and using SQL commands effectively.

Uploaded by

sitirasmiya67
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

SQL Commands

The document provides an overview of SQL commands categorized into five types: DDL (Data Definition Language), DML (Data Manipulation Language), DQL (Data Query Language), DCL (Data Control Language), and TCL (Transaction Control Language). Each category includes commands with examples, explaining their purpose such as creating tables, manipulating data, querying data, controlling access, and managing transactions. It serves as a foundational guide for understanding and using SQL commands effectively.

Uploaded by

sitirasmiya67
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQL Commands: DDL, DML, DQL, DCL, TCL

Data Definition Language (DDL)

Data Definition Language (DDL) commands are used for defining the
database structure or schema.

Let's look at some DDL commands with a simple example for each
command.

i. CREATE

In SQL, the CREATE TABLE command is used to create a new table in the
database. For example,

CREATE TABLE Products (

product_id INT,

name VARCHAR(100),

price DECIMAL

);

Run Code

Here, the SQL command creates a new table named Products with three
columns: product_id (integer type), name (string type up
to 100 characters), and price (decimal type for storing prices).

ii. ALTER TABLE

In SQL, the ALTER TABLE command is used to modify the structure of an


existing table like adding, deleting, renaming columns, etc.

Let's look at an example.

-- add email column to Customers table

ALTER TABLE Customers

ADD email VARCHAR(100);

Run Code

Here, the SQL command adds a column named email to


the Customers table.

iii. DROP TABLE

In SQL, the DROP TABLE command is used to delete the specified table in
our database. For example,

-- delete Orders table


DROP TABLE Orders;

Run Code

Here, the SQL command will delete the table named Orders.

Data Manipulation Language (DML)

DML (Data Manipulation Language) are SQL commands focused on


handling data within the database, including most SQL statements.

Let's look at some DML commands with a simple example for each
command.

i. INSERT INTO

In SQL, the INSERT INTO statement is used to insert new rows into a
database table. For example,

-- insert a row in the Customers table

INSERT INTO Customers (customer_id, first_name, last_name, age,


country)

VALUES (6, 'Alice', 'Brown', 30, 'Canada');

Run Code

Here, the SQL command inserts a new row into the Customers table with
the given values.

ii. UPDATE

The SQL UPDATE statement is used to edit an existing row in a database


table.

Let's look at an example.

-- update a single value in the given row

UPDATE Customers

SET age = 29

WHERE customer_id = 5;

Run Code

This command updates the age field of the customer


with customer_id 5 to 29.

iii. DELETE
The SQL DELETE statement is used to delete row(s) from a database table.
For example,

DELETE FROM Customers

WHERE country = 'UAE';

Run Code

Here, the SQL command deletes all rows from the Customers table where
the country is UAE.

Data Query Language (DQL)

DQL is used for querying and retrieving data from a database. It allows us
to specify the exact data we want to see from one or more tables based
on given conditions.

SELECT

In SQL, the SELECT statement is used to select (retrieve) data from a


database table. For example,

SELECT * FROM Customers

WHERE country = 'UK';

Run Code

Here, the SQL command retrieves all rows from the Customers table
where the country is UK.

Data Control Language (DCL)

DCL commands include GRANT and REVOKE, which are used to control
access to the database.

i. GRANT

In SQL, the GRANT statement gives users access privileges to the


database. For example,

GRANT SELECT, UPDATE ON Customers TO user1;

This command grants SELECT and UPDATE permissions on


the Customers table to user1.

ii. REVOKE
In SQL, the REVOKE statement withdraws access privileges given by
the GRANT statement.

Let's look at an example.

REVOKE SELECT ON Customers FROM user1;

Here, the SQL query revokes SELECT permission on the Customers table
from user1.

Transaction Control Language (TCL)

In SQL, TCL commands manage changes affecting the database.

i. COMMIT

In SQL, the COMMIT command is used for saving the changes made in the
database. For example,

UPDATE Customers

SET country = 'UK'

WHERE customer_id = 4;

COMMIT;

Here, the SQL command updates the country of the customer


with customer_id 4 and commits the transaction.

ii. ROLLBACK

In SQL, ROLLBACK is used to undo the transactions that have not been
saved in the database. For example,

DELETE FROM Orders;

ROLLBACK;

Here, the query deletes all records from the Orders table but then rolls
back the transaction.
SQL Commands | DDL, DQL, DML, DCL and TCL Commands

Last Updated : 22 May, 2026

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
setting permissions for users. Before learning SQL command types, it is
important to understand some basic terms:

 Database: A collection of organized data.

 Table: Stores data in rows and columns.

 Row (Record): A single entry in a table.

 Column (Field): An attribute of the data.

 Primary Key: Uniquely identifies each record.

 Query: A request to access or modify data.

 Constraint: Rules applied to ensure valid data.

SQL Commands are mainly categorized into five categories:


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

Command Description Syntax

Create database or its CREATE TABLE


objects (table, index, table_name (column1
CREATE
function, views, stored data_type, column2
procedure and triggers) data_type, ...);

Delete objects from the


DROP DROP TABLE table_name;
database

ALTER Alter the structure of the ALTER TABLE table_name


database ADD COLUMN
Command Description Syntax

column_name data_type;

Remove all records from


a table, including all TRUNCATE TABLE
TRUNCATE
spaces allocated for the table_name;
records are removed

COMMENT ON TABLE
Add comments to the
COMMENT table_name IS
data dictionary
'comment_text';

RENAME TABLE
Rename an object
RENAME old_table_name TO
existing in the database
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.

Comman
d Description Syntax

SELECT It is used to retrieve data SELECT column1,


Comman
d Description Syntax

column2, ...FROM table_name


from the database
WHERE condition;

Indicates the table(s) from SELECT column1


FROM
which to retrieve data. FROM table_name;

SELECT column1
Filters rows before any
WHERE FROM table_name
grouping or aggregation
WHERE condition;

SELECT column1,
Groups rows that have the
GROUP AVG_FUNCTION(column2)
same values in specified
BY FROM table_name
columns.
GROUP BY column1;

SELECT column1,
AVG_FUNCTION(column2)
Filters the results of GROUP
HAVING FROM table_name
BY
GROUP BY column1
HAVING condition;

SELECT DISTINCT column1,


DISTINC Removes duplicate rows
column2, ...
T from the result set
FROM table_name;

SELECT column1
ORDER Sorts the result set by one FROM table_name
BY or more columns ORDER BY column1 [ASC |
DESC];

LIMIT Used to restrict the number SELECT * FROM table_name


of rows returned in a SELECT LIMIT number;
query (commonly supported
Comman
d Description Syntax

in MySQL and PostgreSQL).

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.

Comma
nd Description Syntax

INSERT INTO table_name (column1,


Insert data into a
INSERT column2, ...) VALUES (value1,
table
value2, ...);

UPDATE table_name SET column1 =


Update existing
UPDATE value1, column2 = value2 WHERE
data within a table
condition;

Delete records from DELETE FROM table_name WHERE


DELETE
a database table 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.

Command Description Syntax

Assigns new privileges to GRANT privilege_type


a user account, allowing [(column_list)] ON
GRANT access to specific [object_type]
database objects, actions object_name TO user
or functions. [WITH GRANT OPTION];

Removes previously REVOKE [GRANT OPTION


granted privileges from a FOR] privilege_type
user account, taking [(column_list)] ON
REVOKE
away their access to [object_type]
certain database objects object_name FROM user
or actions. [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.
Command Description Syntax

BEGIN BEGIN TRANSACTION


Starts a new transaction
TRANSACTION [transaction_name];

Saves all changes made


COMMIT COMMIT;
during the transaction

Undoes all changes


ROLLBACK made during the ROLLBACK;
transaction

Creates a savepoint
SAVEPOINT
SAVEPOINT within the current
savepoint_name;
transaction

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.

You might also like