Mastering SQL: The Ultimate Guide to Querying and Managing Relational Databases

0 0 0 0 0

📘 Chapter 3: Data Manipulation Language (DML)

🧠 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:

  • Insert new data
  • Update existing data
  • Delete records
  • Retrieve data (via SELECT)

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

  • Ensure data types match column types.
  • Use explicit column lists to avoid errors.
  • Use constraints to avoid inserting invalid data.

📝 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

Back

FAQs


1. Q: Is SQL a programming language?

A: SQL is a query language, not a general-purpose programming language. It’s used to communicate with databases.

2. Q: What's the difference between SQL and MySQL?

A: SQL is a language. MySQL is a database management system (DBMS) that uses SQL.

3. Q: Can SQL be used with Python or Java?

A: Yes. Libraries like JDBC (Java) or sqlite3/sqlalchemy (Python) allow SQL integration.

4. Q: What is a JOIN in SQL?

A: A JOIN combines rows from two or more tables based on a related column between them.

5. Q: What is normalization?

A: The process of structuring data to reduce redundancy and improve integrity.

6. Q: What’s the difference between WHERE and HAVING?

A: WHERE filters rows before aggregation. HAVING filters groups after aggregation.

7. Q: Is SQL case-sensitive?

A: SQL keywords are case-insensitive, but string comparisons may be case-sensitive, depending on the DBMS.

8. Q: Can SQL handle big data?

A: SQL can handle moderate volumes. For large-scale datasets, use SQL-based engines like Presto, Hive, or BigQuery.

9. Q: What is a view in SQL?

A: A view is a virtual table based on the result of a query. It doesn't store data itself.

10. Q: How do I prevent SQL injection?

A: Use parameterized queries or prepared statements instead of dynamic SQL strings