🧠 What is DML?
Data Manipulation Language (DML) is a core component
of SQL used to manage data within relational databases. It includes
commands that let you:
DML commands operate on table data, not the structure
(which is handled by DDL).
🔧 1. INSERT – Adding New
Data
✅ Basic Syntax
sql
INSERT
INTO table_name (column1, column2, ...)
VALUES
(value1, value2, ...);
✅ Example
sql
INSERT
INTO Students (StudentID, Name, Age)
VALUES
(1, 'Alice', 22);
🔹 Insert Multiple Rows
sql
INSERT
INTO Students (StudentID, Name, Age)
VALUES
(2,
'Bob', 20),
(3,
'Carol', 23);
📋 Table: Students
|
StudentID |
Name |
Age |
|
1 |
Alice |
22 |
|
2 |
Bob |
20 |
|
3 |
Carol |
23 |
⚠️ Best Practices
📝 2. SELECT – Retrieving
Data
The SELECT statement is the most used SQL command to
query data.
✅ Select All Columns
sql
SELECT
* FROM Students;
✅ Select Specific Columns
sql
SELECT
Name, Age FROM Students;
✅ Filtering with WHERE
sql
SELECT
* FROM Students
WHERE
Age > 21;
✅ Sorting Results
sql
SELECT
* FROM Students
ORDER
BY Age DESC;
✅ Using Aliases
sql
SELECT
Name AS StudentName FROM Students;
📋 SELECT with Clauses
|
Clause |
Purpose |
|
WHERE |
Filter rows |
|
ORDER BY |
Sort result |
|
LIMIT |
Restrict number of
rows |
|
GROUP BY |
Group rows by
a column |
|
HAVING |
Filter grouped rows |
✅ Aggregate Functions
sql
SELECT
COUNT(*), AVG(Age)
FROM
Students;
🔄 3. UPDATE – Modifying
Data
✅ Syntax
sql
UPDATE
table_name
SET
column1 = value1, column2 = value2
WHERE
condition;
✅ Example
sql
UPDATE
Students
SET
Age = 24
WHERE
StudentID = 1;
⚠️ Important Note
Always use a WHERE clause unless you want to update all
records.
✅ Update Multiple Columns
sql
UPDATE
Students
SET
Name = 'Alice Smith', Age = 23
WHERE
StudentID = 1;
📋 Table After Update
|
StudentID |
Name |
Age |
|
1 |
Alice Smith |
23 |
|
2 |
Bob |
20 |
|
3 |
Carol |
23 |
🗑️ 4. DELETE – Removing
Data
✅ Syntax
sql
DELETE
FROM table_name
WHERE
condition;
✅ Example
sql
DELETE
FROM Students
WHERE
StudentID = 2;
⚠️ Without a WHERE clause, all
rows will be deleted.
🔁 5. DML vs DDL – Quick
Recap
|
Feature |
DML |
DDL |
|
Purpose |
Manipulate data |
Manage structure |
|
Affects |
Table rows |
Table
definitions |
|
Commands |
INSERT, UPDATE,
DELETE, SELECT |
CREATE, ALTER, DROP |
🧪 Real-World Use Case
Let’s say you’re managing an e-commerce database.
✅ Products Table
sql
CREATE
TABLE Products (
ProductID INT PRIMARY KEY,
Name VARCHAR(50),
Price DECIMAL(10, 2),
InStock INT
);
✅ Insert Data
sql
INSERT
INTO Products VALUES
(1,
'Laptop', 799.99, 5),
(2,
'Mouse', 25.00, 30),
(3,
'Keyboard', 45.00, 15);
✅ Increase Price of All Products
by 10%
sql
UPDATE
Products
SET
Price = Price * 1.10;
✅ Delete Out-of-Stock Items
sql
DELETE
FROM Products
WHERE
InStock = 0;
✅ View Expensive Products
sql
SELECT
* FROM Products
WHERE
Price > 100
ORDER
BY Price DESC;
🔍 Additional Filtering
Techniques
✅ BETWEEN
sql
SELECT
* FROM Products
WHERE
Price BETWEEN 30 AND 100;
✅ LIKE (Pattern Matching)
sql
SELECT
* FROM Products
WHERE
Name LIKE 'K%'; -- Starts with K
✅ IN
sql
SELECT
* FROM Students
WHERE
Age IN (20, 22, 23);
🔐 Transaction Support
with DML
DML statements can be wrapped in transactions for
safety.
sql
START
TRANSACTION;
UPDATE
Products
SET
Price = 0
WHERE
ProductID = 1;
ROLLBACK;
-- Undo changes
Use
COMMIT to save changes permanently.
📋 Transaction Commands
|
Command |
Purpose |
|
START TRANSACTION |
Begins transaction |
|
COMMIT |
Saves changes
permanently |
|
ROLLBACK |
Reverts all changes in
the block |
✅ Summary Table
|
Command |
Action |
Common Use Case |
|
INSERT |
Add new records |
Register users, add
products |
|
SELECT |
Retrieve
records |
Reporting,
filtering, dashboards |
|
UPDATE |
Modify existing
records |
Edit profile, change
password |
|
DELETE |
Remove
records |
Delete
expired data, users |
A: SQL is a query language, not a general-purpose programming language. It’s used to communicate with databases.
A: SQL is a language. MySQL is a database management system (DBMS) that uses SQL.
A: Yes. Libraries like JDBC (Java) or sqlite3/sqlalchemy (Python) allow SQL integration.
A: A JOIN combines rows from two or more tables based on a related column between them.
A: The process of structuring data to reduce redundancy and improve integrity.
A: WHERE filters rows before aggregation. HAVING filters groups after aggregation.
A: SQL keywords are case-insensitive, but string comparisons may be case-sensitive, depending on the DBMS.
A: SQL can handle moderate volumes. For large-scale datasets, use SQL-based engines like Presto, Hive, or BigQuery.
A: A view is a virtual table based on the result of a query. It doesn't store data itself.
A: Use parameterized queries or prepared statements instead of dynamic SQL strings
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)