SQL Commands: DDL, DQL, DML, DCL and TCL With Examples (2024)

Last Updated : 05 Jun, 2024

Improve

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.

Table of Content

  • Short Overview of SQL
  • DDL (Data Definition Language)
  • DQL (Data Query Language)
  • DML(Data Manipulation Language)
  • DCL (Data Control Language)
  • TCL (Transaction Control Language)
  • Important SQL Commands
  • SQL Commands with Examples

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: DDL, DQL, DML, DCL and TCL With Examples (1)

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:

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 'comment_text' ON TABLE table_name;
RENAMERename an object existing in the databaseRENAME 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.

CommandDescriptionSyntax

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:

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;
LOCKTable control concurrencyLOCK TABLE table_name IN lock_mode;
CALLCall a PL/SQL or JAVA subprogramCALL procedure_name(arguments);
EXPLAIN PLANDescribe the access path to dataEXPLAIN 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:

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];

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:

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;

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 CommandExample
SELECTSELECT * FROM employees;
INSERTINSERT INTO employees (first_name, last_name, email) VALUES ('John', 'Doe', 'john.doe@example.com');
UPDATEUPDATE employees SET email = 'jane.doe@example.com' WHERE first_name = 'Jane' AND last_name = 'Doe';
DELETEDELETE FROM employees WHERE employee_id = 123;
CREATE TABLECREATE TABLE employees ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50));
ALTER TABLEALTER TABLE employees ADD COLUMN phone VARCHAR(20);
DROP TABLEDROP TABLE employees;
WHERESELECT * FROM employees WHERE department = 'Sales';
ORDER BYSELECT * FROM employees ORDER BY hire_date DESC;
JOINSELECT 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.



SQL Commands: DDL, DQL, DML, DCL and TCL With Examples (2)

GeeksforGeeks

Improve

Previous Article

SQL Operators

Next Article

SQL CREATE DATABASE

Please Login to comment...

SQL Commands: DDL, DQL, DML, DCL and TCL With Examples (2024)

References

Top Articles
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 6317

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.