SQL Statements and Their Uses

post

In this guide, we'll explore the different types of SQL statements with simple explanations and examples.

What are SQL Statements?

SQL (Structured Query Language) statements are used to manage and interact with data stored in a database. These commands help you:

Add, change, delete, or fetch data.

Build and maintain databases.

Commonly used SQL statements include DDL and DML commands.

Developers use basic SQL commands like SELECT, UPDATE, and DELETE while building and deploying real-world applications.
The CRUD operations (Create, Read, Update, Delete) are also done through SQL statements.

Demo Database

Let's take an example of a demo database called DebugShala.

Query:

SELECT * FROM DebugShala;

Output:
(Displays all the records from the DebugShala table.)

Uses of SQL Statements

Perform CRUD operations easily.

Help in creating and managing databases.

DML statements allow data manipulation.

DDL statements define database structure.

Types of SQL Statements

SQL commands are divided into 5 main categories based on what they do:

1. DDL (Data Definition Language)

DDL commands help define and modify the database structure, such as creating or deleting tables.

Common DDL Commands:

CREATE
Creates a new table.

Example:

CREATE TABLE DebugShala (  emp_id VARCHAR(5),  name VARCHAR(50),  location VARCHAR(50),  experience INT );

ALTER
Modifies an existing table.

Example:

ALTER TABLE DebugShala ADD department VARCHAR(30);

DROP
Deletes a table completely.

Example:

DROP TABLE DebugShala;

TRUNCATE
Removes all data from a table without deleting its structure.

Example:

TRUNCATE TABLE DebugShala;

2. DML (Data Manipulation Language)

DML commands manage the data inside the tables.

Two Types:

Procedural DML: You tell how to fetch the data.

Declarative DML: You just specify what data you need.

Common DML Commands:

INSERT
Adds new records.

Example:

INSERT INTO DebugShala VALUES ('E101', 'Aarav', 'Mumbai', 2);

UPDATE
Modifies existing data.

Example:

UPDATE DebugShala SET experience = 3 WHERE name = 'Aarav';

DELETE
Removes specific records.

Example:

DELETE FROM DebugShala WHERE emp_id = 'E101';

3. DQL (Data Query Language)

DQL is mainly used to query and retrieve data from the database.

Common DQL Command:

SELECT
Fetches data from a table.

Example:

SELECT * FROM DebugShala;

4. TCL (Transaction Control Language)

TCL commands manage changes made by DML statements.

Common TCL Commands:

COMMIT
Saves all changes permanently.

Example:

COMMIT;

ROLLBACK
Undo changes and revert to the last saved state.

Example:

ROLLBACK TO savepoint_name;

SAVEPOINT
Sets a savepoint to which you can later rollback.

Example:

SAVEPOINT savepoint_name;

5. DCL (Data Control Language)

DCL commands control access to the database.

Common DCL Commands:

GRANT
Gives user privileges like read, update, etc.

Example:

GRANT UPDATE ON DebugShala TO user;

REVOKE
Removes previously granted privileges.

Example:

REVOKE UPDATE ON DebugShala FROM user;

Summary

In this tutorial, we learned:

What SQL statements are.

Their categories: DDL, DML, DQL, TCL, and DCL.

Syntax and examples for each type.

SQL statements are powerful tools that help you manage your database efficiently. Mastering them is key to working with any database system!


Share This Job:

Write A Comment

    No Comments