SQL (Structured Query Language) is a standard programming language used to store, retrieve, manage, and manipulate data in relational databases. It is widely used in database management systems (DBMS) like MySQL, PostgreSQL, SQL Server, and Oracle.
* Declarative Language – You specify what you want, not how to do it.
* Relational Database Management – Works with structured data stored in tables.
* Data Manipulation & Querying – Retrieve and modify data efficiently.
* Scalability & Security – Supports large datasets and access control.
SQL commands are categorized into five main types :
Category | Description | Examples |
---|---|---|
DDL (Data Definition Language) | Defines the structure of databases and tables | CREATE , ALTER , DROP , TRUNCATE |
DML (Data Manipulation Language) | Manages and manipulates data | SELECT , INSERT , UPDATE , DELETE |
DQL (Data Query Language) | Queries data from the database | SELECT |
DCL (Data Control Language) | Controls access to the database | GRANT , REVOKE |
TCL (Transaction Control Language) | Manages database transactions | COMMIT , ROLLBACK , SAVEPOINT |
-- Create a table
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2)
);
-- Insert data into the table
INSERT INTO employees (id, name, department, salary)
VALUES (1, 'Alice', 'HR', 55000.00);
-- Retrieve data
SELECT * FROM employees;
-- Update a record
UPDATE employees SET salary = 60000 WHERE id = 1;
-- Delete a record
DELETE FROM employees WHERE id = 1;
* Universal Standard – Used in almost all database systems
* Powerful & Flexible – Handles large datasets efficiently
* Easy to Learn – Simple syntax for querying and managing data
* Supports ACID Properties – Ensures Atomicity, Consistency, Isolation, Durability in transactions.
The DELETE command in SQL is used to remove specific records (rows) from a table based on a given condition. It allows selective deletion using the WHERE clause.
DELETE FROM table_name WHERE condition;
* table_name
– The name of the table from which you want to delete records.
* condition
– Specifies which rows should be deleted.
If you omit the WHERE clause, all rows in the table will be deleted!
DELETE FROM employees WHERE id = 3;?
Deletes the row where id = 3
from the employees
table.
DELETE FROM employees WHERE department = 'Sales';?
Deletes all employees in the Sales department.
DELETE FROM employees;?
DELETE FROM employees;
Removes all records from the employees
table without deleting the table structure.
Feature | Description |
---|---|
Selective Deletion | Deletes specific rows using the WHERE clause |
Fully Logged | Each row deletion is logged (can impact performance) |
Can Be Rolled Back | Supports transactions (COMMIT , ROLLBACK ) |
Triggers Fire | If triggers are defined, they will execute |
Does Not Reset Identity | If the table has an AUTO_INCREMENT column, the numbering is not reset |
The TRUNCATE command in SQL is used to remove all records from a table efficiently. Unlike DELETE
, it does not delete rows one by one but instead deallocates the entire table’s data pages, making it much faster.
TRUNCATE TABLE table_name;?
table_name
– The name of the table from which all rows will be removed.
TRUNCATE TABLE employees;
This removes all records from the employees
table but keeps the table structure intact.
Feature | Description |
---|---|
Removes All Rows? | Yes (without a WHERE clause) |
Faster Than DELETE? | Yes (deallocates table pages instead of row-by-row deletion) |
Can Be Rolled Back? | No (except in some databases like SQL Server with transactions) |
Resets Auto-Increment? | Yes (identity column is reset to its initial value) |
Fires Triggers? | No (triggers are not activated) |
Locks? | Uses table-level lock, while DELETE uses row-level locks |
* Use TRUNCATE
when you need to quickly remove all records from a table.
* Do not use TRUNCATE
if you need to retain identity values or trigger actions.
* If you need to delete specific rows, use DELETE
instead.
Feature | DELETE | TRUNCATE |
---|---|---|
Operation Type | DML (Data Manipulation Language) | DDL (Data Definition Language) |
Removes Rows? | Deletes selected rows based on a condition (WHERE clause is supported) | Removes all rows from the table (No WHERE clause support) |
Logging | Fully logged (each deleted row is logged) | Minimal logging (faster execution) |
Performance | Slower, as each row is deleted individually | Faster, as it deallocates entire table pages |
Can be Rolled Back? | Yes, if used within a transaction (uses COMMIT/ROLLBACK) | No, cannot be rolled back (unless inside a transactional DB like SQL Server with TRANSACTION ) |
Auto Increment Reset? | No, identity column values remain unchanged | Yes, identity column values are reset |
Triggers Activation | Yes, triggers are fired | No, triggers are not fired |
Foreign Key Dependence | Can be used on tables with foreign key constraints | Cannot be used if the table has a foreign key reference |
* Example Usage :
-- DELETE example (removes specific rows)
DELETE FROM employees WHERE department = 'Sales';
-- TRUNCATE example (removes all rows)
TRUNCATE TABLE employees;