The Wayback Machine - https://web.archive.org/web/20240822161839/https://www.geeksforgeeks.org/sql-ddl-dql-dml-dcl-tcl-commands/
Open In App

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

Last Updated : 05 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

SQL commands are very used to interact with the database. These commands allow users to perform various actions on a database. This article will teach us about SQL commands or SQL sublanguage commands like DDL, DQL, DML, DCL, and TCL.

All important SQL commands with their syntax and examples are covered in this article.

But before heading to the SQL command section, let’s briefly introduce SQL.

Short Overview of SQL

Structured Query Language (SQL), as we all know, is the database language by which we can perform certain operations on the existing database, and we can also use this language to create a database. SQL uses certain commands like CREATE, DROP, INSERT, etc. to carry out the required tasks. 

SQL commands are like instructions to a table. It is used to interact with the database with some operations. It is also used to perform specific tasks, functions, and queries of data. SQL can perform various tasks like creating a table, adding data to tables, dropping the table, modifying the table, set permission for users.

These SQL commands are mainly categorized into five categories: 

  1. DDL – Data Definition Language
  2. DQL – Data Query Language
  3. DML – Data Manipulation Language
  4. DCL – Data Control Language
  5. TCL – Transaction Control Language

Now, we will see all of these in detail.

sql commands categories

DDL (Data Definition Language)

DDL or Data Definition Language actually consists of the SQL commands that can be used to define the database schema. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in the database.

DDL is a set of SQL commands used to create, modify, and delete database structures but not data. These commands are normally not used by a general user, who should be accessing the database via an application.

List of DDL Commands

Some DDL commands and their syntax are:

Command Description Syntax
CREATE Create database or its objects (table, index, function, views, store procedure, and triggers) CREATE TABLE table_name (column1 data_type, column2 data_type, ...);
DROP Delete objects from the database DROP TABLE table_name;
ALTER Alter the structure of the database ALTER TABLE table_name ADD COLUMN column_name data_type;
TRUNCATE Remove all records from a table, including all spaces allocated for the records are removed TRUNCATE TABLE table_name;
COMMENT Add comments to the data dictionary COMMENT 'comment_text' ON TABLE table_name;
RENAME Rename an object existing in the database RENAME TABLE old_table_name TO new_table_name;

DQL (Data Query Language)

DQL statements are used for performing queries on the data within schema objects. The purpose of the DQL Command is to get some schema relation based on the query passed to it. We can define DQL as follows it is a component of SQL statement that allows getting data from the database and imposing order upon it. It includes the SELECT statement.

This command allows getting the data out of the database to perform operations with it. When a SELECT is fired against a table or tables the result is compiled into a further temporary table, which is displayed or perhaps received by the program i.e. a front-end.

DQL Command

There is only one DQL command in SQL i.e.

Command Description Syntax

SELECT

It is used to retrieve data from the database

SELECT column1, column2, ...FROM table_name<br>WHERE condition;

DML(Data Manipulation Language)

The SQL commands that deal with the manipulation of data present in the database belong to DML or Data Manipulation Language and this includes most of the SQL statements. 

It is the component of the SQL statement that controls access to data and to the database. Basically, DCL statements are grouped with DML statements.

List of DML commands

Some DML commands and their syntax are:

Command Description Syntax
INSERT Insert data into a table INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
UPDATE Update existing data within a table UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
DELETE Delete records from a database table DELETE FROM table_name WHERE condition;
LOCK Table control concurrency LOCK TABLE table_name IN lock_mode;
CALL Call a PL/SQL or JAVA subprogram CALL procedure_name(arguments);
EXPLAIN PLAN Describe the access path to data EXPLAIN PLAN FOR SELECT * FROM table_name;

DCL (Data Control Language)

DCL includes commands such as GRANT and REVOKE which mainly deal with the rights, permissions, and other controls of the database system. 

List of  DCL commands:

Two important DCL commands and their syntax are:

Command Description Syntax
GRANT Assigns 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];
REVOKE Removes 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];

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, the transaction fails.

Therefore, a transaction has only two results: success or failure. You can explore more about transactions here. Hence, the following TCL commands are used to control the execution of a transaction: 

List of TCL Commands

Some TCL commands and their syntax are:

Command Description Syntax
BEGIN TRANSACTION Starts a new transaction BEGIN TRANSACTION [transaction_name];
COMMIT Saves all changes made during the transaction COMMIT;
ROLLBACK Undoes all changes made during the transaction ROLLBACK;
SAVEPOINT Creates a savepoint within the current transaction SAVEPOINT savepoint_name;

Important SQL Commands

Some of the most important SQL commands are:

  1. SELECT: Used to retrieve data from a database.
  2. INSERT: Used to add new data to a database.
  3. UPDATE: Used to modify existing data in a database.
  4. DELETE: Used to remove data from a database.
  5. CREATE TABLE: Used to create a new table in a database.
  6. ALTER TABLE: Used to modify the structure of an existing table.
  7. DROP TABLE: Used to delete an entire table from a database.
  8. WHERE: Used to filter rows based on a specified condition.
  9. ORDER BY: Used to sort the result set in ascending or descending order.
  10. JOIN: Used to combine rows from two or more tables based on a related column between them.

SQL Commands With Examples

The examples demonstrates how to use an SQL command. Here is the list of popular SQL commands with Examples.

SQL Command Example
SELECT SELECT * FROM employees;
INSERT INSERT INTO employees (first_name, last_name, email) VALUES ('John', 'Doe', 'john.doe@example.com');
UPDATE UPDATE employees SET email = 'jane.doe@example.com' WHERE first_name = 'Jane' AND last_name = 'Doe';
DELETE DELETE FROM employees WHERE employee_id = 123;
CREATE TABLE CREATE TABLE employees ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50));
ALTER TABLE ALTER TABLE employees ADD COLUMN phone VARCHAR(20);
DROP TABLE DROP TABLE employees;
WHERE SELECT * FROM employees WHERE department = 'Sales';
ORDER BY SELECT * FROM employees ORDER BY hire_date DESC;
JOIN SELECT e.first_name, e.last_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id;

These are common examples of some important SQL commands. The examples provide better understanding of the SQL commands and teaches correct way to use them.

Conclusion

SQL commands are the foundation of an effective database management system. Whether you are manipulating data, or managing data, SQL provides all sets of tools. Now, with this detailed guide, we hope you have gained a deep understanding of SQL commands, their categories, and syntax with examples.



Previous Article
Next Article

Similar Reads

SQL | DDL, DML, TCL and DCL
Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL) form the backbone of SQL. Each of these languages plays a critical role in defining, managing, and controlling data within a database system, ensuring both structural integrity and user access control. In this articl
6 min read
Difference Between DDL and DML in DBMS
DDL is a Data Definition Language that is used to define data structures. For example: creating a table, and altering a table are instructions in SQL. DML is a Data Manipulation Language that is used to manipulate data itself. For example: insert, update, and delete are instructions in SQL. Data Definition Language DDL is used to specify a database
3 min read
Difference between DDL and TCL
Prerequisite – SQL Commands 1 Data Definition Language (DDL) is a set of SQL (Structured Query Language) commands used to create, modify, and delete database objects such as tables, indexes, views, and constraints. DDL statements are used to define the schema or structure of a database, including its data types, relationships between tables, and ru
2 min read
Difference between DML and TCL
Prerequisite - DDL, DML, TCL and DCL 1. Data Manipulation Language (DML) : DML is used to manipulate data in the database. For example, insert, update and delete instructions in SQL. 2. Transaction Control Language (TCL) : TCL deals with the transactions within the database. Difference between DML and TCL : S. no.CategoryDMLTCL1.Full FormDML stands
2 min read
DDL Commands &amp; Syntax
In this article, we will discuss the overview of DDL commands and will understand DDL commands like create, alter, truncate, drop. We will cover each command syntax with the help of an example for better understanding. Let's discuss it one by one. Overview :Data Definition Language(DDL) is a subset of SQL and a part of DBMS(Database Management Syst
3 min read
How to Maintain Audit Information For DML in SQL?
Pre-requisites: Audit Trail In today's world, data plays a vital role for any organization. Not just for the organization but also for customers. It becomes vital in any application to maintain track of changes happening in your data to keep audit trails. There are numerous reasons to implement an auditing mechanism for your application or data. Le
7 min read
DQL Full Form
Structured Query Language (SQL) is a nonprocedural language used for retrieving data from queries. It was introduced by IBM as a part of the R project. It was declared as a standard language by ANSI and ISO. DQL statements are used for performing queries on the data within schema objects. The purpose of the DQL Command is to get some schema relatio
3 min read
DCL Full Form
The full form of DCL is Data Control Language in Structured Query Language (SQL). DCL commands are used to control privileges in the database. The privileges (right to access the data) are required for performing all the database operations, like creating tables, views, or sequences. DCL statements are used to perform the work related to the rights
2 min read
DDL Full Form
DDL stands for Data Definition Language. These commands are used to change the structure of a database and database objects.For example, DDL commands can be used to add, remove, or modify tables with in a database.The DDL commands are: CREATEALTERDROPTRUNCATERENAME 1. CREATE : This command is used to create table in the relational database . This c
4 min read
TCL Full Form
Transaction Control Language (TCL) is a critical component of SQL used to manage transactions and ensure data integrity in relational databases. By using TCL commands, we can control how changes to the database are committed or reverted, maintaining consistency across multiple operations. In this article, We will learn about the TCL Full Form by un
5 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Auto-commit commands and Compensating transactions in SQL
As the name suggests, the auto-commit statement is used to avoid the explicit mention of the commit command after each SQL statement. The commit statement itself gets executed by the server after each SQL statement. Each SQL statement is treated as a different transaction. An SQL statement executed in autocommit mode cannot be rolled back. Auto-com
3 min read
SQL Server DELETE and DROP TABLE Commands
In SQL Server, managing data involves not only inserting and updating records but also removing them when they're no longer needed. Two important commands for data removal are DELETE and DROP TABLE. These commands play crucial roles in maintaining database integrity and managing database structures. In this article, we'll learn about the DELETE and
4 min read
Basic SQL Commands
QUESTION: Consider the following table ITEM given below, write the commands in SQL for 1 – 10 and output for 11 – 20 TABLE: ITEM S_NoItem_NameCompanyCostPurchase_QtyDOP1MonitorABC6300202010-05-302CPUCDF8000302010-07-233MouseFGH250252010-08-044UPSHIJ2100102010-06-195PrinterJKL840042010-11-276ScannerJKL450062010-08-14 Write the SQL commands for 1. to
2 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
Dynamic SQL and Temporary Tables in SQL Server
In SQL Server, creating and using Temp Tables using dynamic SQL is a good feature when we need to temporarily create tables at run time and delete automatically all within a session. They can be very useful when we need to store temporary data in a structured format and do data manipulation using Data Manipulation Language in SQL. In this article l
6 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
Difference between SQL and T-SQL
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
4 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
Dynamic SQL in SQL Server
In SQL Server, at times the SQL Queries need to be dynamic and not static, meaning the complete SQL query may be built dynamically at run time as a string using the user inputs and any specific application logic. This can be done in queries run from back-end applications or inside stored procedures. In this article let us look into the details abou
6 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
SQL Quiz : Practice SQL Questions Online
This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
three90RightbarBannerImg