In this comprehensive guide, we’ll delve into the different types of SQL statements and explore their applications with real-world examples. Let’s get started.
Introduction: Different Types of SQL Statements
SQL (Structured Query Language) is a powerful tool for managing and manipulating data in relational databases. It offers various types of statements, each serving a specific purpose in database management.
Let’s now look into various types of SQL Statements.
Data Query Language (DQL)
Data Query Language (DQL) is a fundamental component of SQL that focuses on retrieving data from one or more database tables.
The SELECT statement is the cornerstone of DQL and is primarily used for retrieving data from one or more database tables. It allows you to specify the columns you want to retrieve and apply filters to fetch specific records.
-- Retrieve all customer names from the 'Customers' table
SELECT CustomerName FROM Customers;
Read More : Select Query in SQL with Examples
Data Manipulation Language (DML)
DML statements are crucial for modifying data within a database. They include:
The INSERT statement is used to add new records into a table.
-- Insert a new employee into the 'Employees' table
INSERT INTO Employees (EmployeeID, FirstName, LastName) VALUES (101, 'John', 'Doe');
The UPDATE statement allows you to modify existing records in a table.
-- Update the salary of an employee in the 'Employees' table
UPDATE Employees SET Salary = 60000 WHERE EmployeeID = 101;
The DELETE statement is used to remove records from a table.
-- Delete an employee from the 'Employees' table
DELETE FROM Employees WHERE EmployeeID = 101;
Data Definition Language (DDL)
DDL statements are employed to define the structure of the database, including tables, indexes, and constraints. They include:
The CREATE statement is used to create database objects like tables, indexes, or views.
-- Create a new 'Products' table
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Price DECIMAL(10, 2)
The ALTER statement allows you to modify existing database objects, such as adding or removing columns from a table.
-- Add a 'Description' column to the 'Products' table
ALTER TABLE Products ADD Description TEXT;
The DROP statement is used to delete database objects, such as tables or indexes.
-- Delete the 'Products' table
DROP TABLE Products;
The TRUNCATE statement removes all rows from a table while retaining the table structure.
-- Remove all records from the 'Products' table
TRUNCATE TABLE Products;
Data Control Language (DCL)
DCL statements are responsible for controlling data visibility and access rights. They include:
The GRANT statement provides specific privileges to users or roles, allowing them to perform certain actions on database objects.
-- Grant SELECT privilege on the 'Orders' table to the 'SalesTeam' role
GRANT SELECT ON Orders TO SalesTeam;
The REVOKE statement revokes previously granted privileges from users or roles.
-- Revoke INSERT privilege on the 'Customers' table from user 'Alice'
REVOKE INSERT ON Customers FROM Alice;
Transaction Control Language (TCL)
TCL statements are used to manage transactions within a database. They include:
The COMMIT statement is used to permanently save the changes made during a transaction.
-- Commit the changes made during the current transaction
The ROLLBACK statement is used to undo the changes made during a transaction.
-- Roll back the current transaction and discard changes
The SAVEPOINT statement sets a point within a transaction to which you can later roll back.
-- Create a savepoint 'before_update' within the current transaction
Conclusion: Different Types of SQL Statements
SQL statements play a pivotal role in managing and manipulating data in relational databases. Understanding the different types of SQL statements and their applications is essential for efficient database management
Related Articles :