SQL Commands


Structured Query Language (SQL) is the foundation for working with relational databases. Whether you're managing a small database or working with large datasets, SQL commands help you interact with your data efficiently.


1. What Are SQL Commands?

SQL commands are instructions used to perform operations on data within a database. These commands can be grouped into several categories:

  • DDL (Data Definition Language): Defines the structure of the database (e.g., creating tables).
  • DML (Data Manipulation Language): Deals with the manipulation of data (e.g., inserting or updating records).
  • DCL (Data Control Language): Controls access to data (e.g., granting permissions).
  • TCL (Transaction Control Language): Manages database transactions (e.g., committing or rolling back changes).

Let’s dive into some of the most important SQL commands in each of these categories.


2. Data Definition Language (DDL) Commands

2.1 CREATE DATABASE

The CREATE DATABASE command is used to create a new database.

Syntax:
CREATE DATABASE database_name;
Example:
CREATE DATABASE school;

This command creates a new database named school.


2.2 CREATE TABLE

The CREATE TABLE command is used to define a new table in the database.

Syntax:
CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    ...
);
Example:
CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    grade VARCHAR(5)
);

This creates a table named students with columns for student ID, name, age, and grade.


2.3 ALTER TABLE

The ALTER TABLE command allows you to modify an existing table's structure, such as adding, deleting, or modifying columns.

Syntax:
ALTER TABLE table_name ADD column_name datatype;
Example:
ALTER TABLE students ADD email VARCHAR(100);

This adds a new column email to the students table.


2.4 DROP DATABASE

The DROP DATABASE command is used to delete an entire database and all of its associated data.

Syntax:
DROP DATABASE database_name;
Example:
DROP DATABASE school;

This command deletes the school database.


2.5 DROP TABLE

The DROP TABLE command deletes a table and its data permanently.

Syntax:
DROP TABLE table_name;
Example:
DROP TABLE students;

This removes the students table from the database.


3. Data Manipulation Language (DML) Commands

3.1 SELECT

The SELECT command is used to retrieve data from one or more tables.

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT name, grade FROM students WHERE age > 18;

This retrieves the names and grades of students older than 18.


3.2 INSERT INTO

The INSERT INTO command adds new records into a table.

Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
INSERT INTO students (student_id, name, age, grade)
VALUES (1, 'John Doe', 20, 'A');

This inserts a new student record into the students table.


3.3 UPDATE

The UPDATE command modifies existing records in a table.

Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE students
SET grade = 'B'
WHERE student_id = 1;

This updates the grade of the student with student_id 1 to 'B'.


3.4 DELETE

The DELETE command removes records from a table based on a condition.

Syntax:
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM students WHERE student_id = 1;

This deletes the student with student_id 1 from the students table.


4. Data Control Language (DCL) Commands

4.1 GRANT

The GRANT command is used to give users access to a database object, such as a table or a view.

Syntax:
GRANT privilege_type ON object TO user;
Example:
GRANT SELECT ON students TO user1;

This grants the user1 permission to perform a SELECT operation on the students table.


4.2 REVOKE

The REVOKE command removes a user's access to a database object.

Syntax:
REVOKE privilege_type ON object FROM user;
Example:
REVOKE SELECT ON students FROM user1;

This revokes the SELECT permission from user1 on the students table.


5. Transaction Control Language (TCL) Commands

5.1 COMMIT

The COMMIT command is used to save the changes made in the current transaction.

Syntax:
COMMIT;
Example:
COMMIT;

This saves all changes made to the database during the current session.


5.2 ROLLBACK

The ROLLBACK command is used to undo changes made during the current transaction.

Syntax:
ROLLBACK;
Example:
ROLLBACK;

This undoes all the changes made during the current transaction.


5.3 SAVEPOINT

The SAVEPOINT command is used to set a point in the transaction to which you can roll back.

Syntax:
SAVEPOINT savepoint_name;
Example:
SAVEPOINT before_update;

This creates a savepoint called before_update that you can roll back to later.