SQL Statements

๐Ÿ“ SQL Statements are commands used to communicate with a relational database. They allow you to create database objects, insert data, retrieve information, update existing records, delete unwanted data, manage transactions, and control user permissions. Every interaction with a database is performed using one or more SQL statements.

๐Ÿ“– What are SQL Statements?

An SQL statement is a complete instruction given to a Database Management System (DBMS). The database interprets the statement, performs the requested operation, and returns a result or confirmation message.

Information

Every SQL statement follows a defined syntax and typically ends with a ; (semicolon), which marks the end of the statement.

๐Ÿ—‚๏ธ Categories of SQL Statements

SQL statements are commonly grouped into five major categories based on their purpose.

CategoryFull FormPurpose
DDLData Definition LanguageDefines and modifies database structures.
DMLData Manipulation LanguageInserts, updates, and deletes data.
DQLData Query LanguageRetrieves data from tables.
DCLData Control LanguageControls user permissions and access.
TCLTransaction Control LanguageManages database transactions.

๐Ÿ—๏ธ Data Definition Language (DDL)

DDL statements are used to create and modify the structure of database objects such as tables, views, indexes, and schemas.

1๏ธโƒฃ CREATE

The CREATE statement creates new database objects.

Create a Table

CREATE TABLE Students (
    StudentID INT PRIMARY KEY,
    Name VARCHAR(100),
    Department VARCHAR(50)
);

2๏ธโƒฃ ALTER

The ALTER statement modifies an existing database object.

Add a New Column

ALTER TABLE Students
ADD Email VARCHAR(100);

3๏ธโƒฃ DROP

The DROP statement permanently removes a database object.

Drop a Table

DROP TABLE Students;

4๏ธโƒฃ TRUNCATE

TRUNCATE removes all rows from a table while preserving its structure.

Truncate a Table

TRUNCATE TABLE Students;

Warning

DROP removes both the table structure and its data, whereas TRUNCATE removes only the data while keeping the table intact.

โœ๏ธ Data Manipulation Language (DML)

DML statements are used to add, modify, and remove records stored in tables.

1๏ธโƒฃ INSERT

Insert a Record

INSERT INTO Students (StudentID, Name, Department)
VALUES (101, 'Alice', 'Computer Science');

2๏ธโƒฃ UPDATE

Update a Record

UPDATE Students
SET Department = 'Information Technology'
WHERE StudentID = 101;

3๏ธโƒฃ DELETE

Delete a Record

DELETE FROM Students
WHERE StudentID = 101;

Important

Always use a WHERE clause with UPDATE and DELETE unless you intentionally want to affect every row in the table.

๐Ÿ” Data Query Language (DQL)

DQL consists primarily of the SELECT statement, which retrieves data from one or more tables.

Retrieve Student Records

SELECT StudentID, Name, Department
FROM Students
WHERE Department = 'Computer Science'
ORDER BY Name ASC;

๐Ÿ” Data Control Language (DCL)

DCL statements manage user privileges and database security.

1๏ธโƒฃ GRANT

Grant SELECT Permission

GRANT SELECT
ON Students
TO User1;

2๏ธโƒฃ REVOKE

Revoke SELECT Permission

REVOKE SELECT
ON Students
FROM User1;

๐Ÿ”„ Transaction Control Language (TCL)

TCL statements manage transactions, ensuring that groups of related operations are completed successfully or rolled back if an error occurs.

1๏ธโƒฃ COMMIT

Commit Changes

COMMIT;

2๏ธโƒฃ ROLLBACK

Rollback Changes

ROLLBACK;

3๏ธโƒฃ SAVEPOINT

Create a Savepoint

SAVEPOINT UpdatePoint;

๐Ÿ“Š Summary of Common SQL Statements

StatementCategoryPurpose
CREATEDDLCreate database objects.
ALTERDDLModify database objects.
DROPDDLRemove database objects.
TRUNCATEDDLRemove all rows from a table.
INSERTDMLAdd new records.
UPDATEDMLModify existing records.
DELETEDMLDelete records.
SELECTDQLRetrieve data.
GRANTDCLGrant user privileges.
REVOKEDCLRemove user privileges.
COMMITTCLSave a transaction.
ROLLBACKTCLUndo a transaction.
SAVEPOINTTCLCreate a transaction checkpoint.

๐Ÿ’ผ Real-World Example

Consider a student management system. An administrator first creates the Students table using CREATE, adds student records with INSERT, retrieves information using SELECT, updates student details with UPDATE, removes outdated records using DELETE, and commits the changes using COMMIT. User access is managed through GRANT and REVOKE.

โœ… Best Practices

Best Practice

Use descriptive table and column names, format SQL statements for readability, always test UPDATE and DELETE queries with a SELECT statement first, use transactions for related operations, and grant only the minimum permissions required to users.

๐Ÿš€ Key Points to Remember

  • ๐Ÿ“Œ SQL statements are the primary way to communicate with a database.
  • ๐Ÿ“Œ DDL defines database structures.
  • ๐Ÿ“Œ DML modifies the data stored in tables.
  • ๐Ÿ“Œ DQL retrieves data using the SELECT statement.
  • ๐Ÿ“Œ DCL manages user access and permissions.
  • ๐Ÿ“Œ TCL ensures reliable transaction management.
>>"Well-structured SQL statements make databases easier to manage, maintain, and scale."

Summary

โœ… SQL statements are the building blocks of database operations. By understanding the five major categoriesโ€”DDL, DML, DQL, DCL, and TCLโ€”you can confidently create, manage, query, secure, and maintain relational databases.