SQL
Commands
Usha, Neeru, Naveen, Ishika, Shristhi Dixit
Presenters
Introduction To SQL
• SQL stands for Structured Query Language. It is used for storing and managing data
in relational database management system (RDBMS)
• It is a standard language for Relational Database System. It enables a user to create,
read, update and delete relational databases and tables.
• It is a standard language for Relational Database System. It enables a user to create,
read, update and delete relational databases and tables.
• It allows you to write queries that the computer can execute and then provide
database insights in return.
SQL Commands
SQL commands are instructions. It is used to communicate with the database. It is also used to
perform specific tasks, functions and queries of data
SQL commands allow you to create, manage, retrieve, and manipulate data stored in a database.
They help ensure data integrity, security, and consistency.
SQL commands are standardized, meaning they work across most database systems (like MySQL, PostgreSQL,
Oracle, SQL Server).
They enable users to define access levels and control who can view or modify the data.
SQL Command Categories
Data Manipulation Language (DML): Data Query Language (DQL):
Manage data records with INSERT, Retrieve data primarily using
UPDATE, DELETE, and SELECT SELECT statements
Commands. .
Transaction Control Language (TCL): Data Control Language (DCL):
Manage transactions integrity Control access rights using
With COMMIT, ROLLBACK and GRANT and REVOKE
.SAVEPOINT commands
What Is DML, DQL, DCL & TCL ?
Data Manipulation Language (DML) refers to the commands used to manipulate data stored in a database. It
includes operations such as inserting new records, updating existing records, deleting records, and retrieving
data from tables.
Data Query Language (DQL) is used specifically for querying and fetching information from the database. It
mainly focuses on retrieving data using the SELECT command based on user-defined conditions.
Data Control Language (DCL) is used for controlling access and permissions in the database. It manages who
can view or modify data by granting or revoking user privileges.
Transaction Control Language (TCL) is used to manage transactions and maintain the integrity of the database.
It ensures that changes made by DML commands are either saved permanently or undone through commands
like COMMIT, ROLLBACK, and SAVEPOINT.
Database Control with DML
DML commands are used to manipulate data stored in database tables.
They allow inserting new data, updating existing data, and deleting unwanted data.
DML enables retrieving and viewing data from tables using conditions.
These commands do not change the structure of the table, only the data inside it.
Common DML commands include INSERT, UPDATE, DELETE, and SELECT.
DML Commands
INSERT Command: Insert Is a very important data manipulation command in SQL, which allow users to insert
data in the database.
Syntax of INSERT Command :
Examples of INSERT Command:
This example describes how to insert the record in the database table. Let’s take the following
student table, which consists of only 2 records of the student.
STUDENT_ID NAME AGE
671 Aayush 27
672 Khushi 22
Now Suppose, you want to insert a new record for Saksham, into the student table. For this, you have to
write the following DML INSERT Command:
Result for the updated TABLE STUDENT
STUDENT_ID NAME AGE
671 Aayush 27
672 Khushi 22
673 Saksham 20
SELECT Command: The most important command of DML in SQL. The SELECT command is the
primary command for retrieving data from database tables. While some consider SELECT part of
DML, its importance for data retrieval has led many to classify it as DQL.
Using SELECT command we can retrieve all columns or specific columns from a table.
Syntax for SELECT command :
Syntax for SELECTING all columns from the EMPLOYEES TABLE
Example for the TABLE EMPLOYEES
The asterisk (*) retrieves every column in the table.
Result for the TABLE EMPLOYEES
emp_id first_name last_name department age
101 Rahul Sharma HR 28
102 Sneha Patil Sales 34
103 Arjun Verma IT 42
104 Karan Desai Finance 35
Syntax for SELECTING Specific columns from the EMPLOYEES TABLE
Example for the TABLE EMPLOYEES
Result for the TABLE EMPLOYEES with selected column
emp_id first_name last_name department
101 Rahul Sharma HR
102 Sneha Patil Sales
103 Arjun Verma IT
104 Karan Desai Finance
The WHERE Clause: The WHERE clause is used to filter records based on a specific condition.
Syntax for the WHERE Clause
Example from the TABLE EMPLOYEES
So Let’s suppose from the we only want the first name and the department of the Employees
who work in the IT department.
Now the Result for the TABLE EMPLOYEES will be
first_name department
Arjun IT
The DISTINCT Clause: The DISTINCT clause is used to remove duplicate values from the result.
Syntax for the DISTINCT Clause
Example
So let’s take an example from a TABLE STUDENTS where we want to select distinct classes.
student_id name class age
101 Vikas 8 14
102 Sneha 7 13
103 Harshit 7 14
104 Sonali 9 15
105 Isha 8 14
Now to get the distinct or unique classes we can do something like this
Now the Result will be
class
9
The IN Clause: The IN clause allows you to specify multiple values in a WHERE clause.
Syntax for the IN Clause
Example
So let’s take an example from the same TABLE EMPLOYEES where we want the first name of
the employees that are in the IT and sales department.
Result will be
first_name department
Sneha Sales
Arjun IT
The BETWEEN Clause: The BETWEEN clause is used to remove duplicate values from the result.
Syntax for the BETWEEN Clause
Example
So let’s take an example that we want the products between the price of 500Rs to 2000Rs from
the TABLE PRODUCTS.
product_id product_name price
201 Keyboard 800
202 Mouse 300
203 Headphone 1500
204 Monitor 7000
205 Webcam 1200
Now to get the products between 500Rs to 2000Rs. we can do something like this
Now the Result will be
product_id product_name price
201 Keyboard 800
203 Headphone 1500
205 Webcam 1200
The LIMIT Clause: The LIMIT clause is used to restrict the number of rows returned in the result. It
is very important when we want control output size, pagination and performance optimization.
Syntax for the LIMIT Clause
Example
So let’s take an example from the same TABLE PRODUCTS where we want the first three
product name and price.
Now the Result will be
product_name price
Keyboard 800
Mouse 300
Headphone 1500
The GROUP BY Clause: The GROUP BY clause is used to group rows that have the same values in a
specified column. It is commonly used with aggregate functions like COUNT(), SUM(), AVG(), MAX(),
MIN().
Syntax for the GROUP BY Clause
Example
So let’s take an example from the TABLE EMPLOYEES which is given below, we want to find
the average salary of employees in each department.
emp_id department salary
301 IT 30000
302 Sales 25000
303 IT 35000
304 Sales 27000
305 HR 28000
Now the Result will be
department average_salary
IT 32500
Sales 26000
HR 28000
The HAVING Clause: The HAVING clause is used to filter groups created by GROUP BY. It works
after grouping (unlike WHERE which works before grouping)
Syntax for the HAVING Clause
Example
So let’s take an example from the same TABLE EMPLOYEES, and we want to find the
departments whose average salary is greater than 30000.
Now the Result will be
department average_salary
IT 32500
Aggregate functions helps to summarize the large
volumes of data. These functions can produce a single
value for an entire group or table.
Aggregate List of aggregate functions:
COUNT
Functions In SUM
SELECT Command AVG
MIN
MAX
STDEV
VARIANCE
COUNT() FUNCTION: The COUNT() function returns the number of rows that matches a specified
criterion.
Syntax for COUNT() function
SUM() FUNCTION: The SUM() function returns the total sum of a numeric column.
Syntax for SUM() function
AVG() FUNCTION: The AVG() function returns the average value of a numeric column.
Syntax for AVG() function
STDEV() FUNCTION: The STDEV() function is used to calculate the Standard Deviation of total
records (or rows) selected by the SELECT Statement.
Syntax for STDEV() function
MIN() FUNCTION: The MIN() function returns the smallest value of the selected column.
Syntax for MIN() function
MAX() FUNCTION: The MAX() function returns the largest value of the selected column.
Syntax for MAX() function
VARIANCE() FUNCTION: It returns the population standard variance of all the fields in a particular column.
Syntax for VARIANCE() function
UPDATE Command: The UPDATE command is used to modify existing records in a table.
It updates one or more columns in selected rows.
Syntax of UPDATE Command :
Examples of UPDATE Command:
This example describes how to update record in the database table. Let’s take the TABLE
PRODUCTS, where we want to update the product name and price for the product Mouse with
product id 202.
Now the Result will be
product_id product_name price
201 Keyboard 800
202 RBG Mouse 500
203 Headphone 1500
204 Monitor 7000
205 Webcam 1200
DELETE Command: The DELETE command is used to remove existing records (rows) from a table.
It deletes data but does not remove the table structure.
Syntax of DELETE Command :
Examples of DELETE Command:
This example describes how to delete multiple record in the database table. Let’s take the TABLE
PRODUCTS, where we want to delete the products whose price is less than 1000Rs.
Now the Result will be
product_id product_name price
203 Headphone 1500
204 Monitor 7000
205 Webcam 1200
Database Access with DCL
DCL (Data Control Language) commands are used to control access and permissions in the database.
These commands help in granting and revoking rights to users for performing operations on database objects.
DCL ensures data security, privacy, and authorized usage of the database.
They do not manipulate data, but rather control who can access or modify the data.
Common DCL commands include GRANT and REVOKE.
DCL Commands
GRANT Command: It is employed to grant a privilege to a user. GRANT command allows specified users to
perform specified tasks.
Syntax of GRANT Command with single permission and multiple permissions:
Examples of GRANT Command:
We can grant single permission as well as multiple permission, or all permissions to a user like
this:
Single Permission : user1 can only retrieve data from the employees table.
Here user2 can view, add, and modify data in products.
Here manager can perform any operation on sales table.
REVOKE Command: The REVOKE command is used to remove or withdraw permissions that were previously
granted to a user. In short, REVOKE does the opposite of GRANT.
Syntax of REVOKE Command with single permission and multiple permissions:
Examples of REVOKE Command:
We can revoke single permission as well as multiple permission, or all permissions to a user like
this:
Remove Single Permission
Remove Multiple Permissions:
Here manager can perform any operation on sales table.
Managing Transactions with
TCL
TCL (Transaction Control Language) commands are used to manage transactions in a database..
A transaction is a group of SQL operations that are treated as a single unit.
TCL ensures data consistency — either all changes happen or none happen.
These commands allow us to save, undo, or control changes made by DML commands (INSERT, UPDATE,
DELETE).
Common TCL commands include COMMIT, ROLLBACK, and SAVEPOINT.
TCL Commands
COMMIT Command: The COMMIT command is used to permanently save all the changes made during the
current transaction, meaning after COMMIT changes can not be undone.
Syntax of COMMIT Command
Examples of COMMIT Command:
Suppose we want to update an existing record in the employees table of the employee whose
employee id is 103.
The update is now permanently saved in the database.
ROLLBACK Command: The ROLLBACK command is used to undo changes made in the current transaction
before they are committed, meaning Rollback restores the database to the last committed state.
Syntax of ROLLBACK Command
Examples of ROLLBACK Command:
Suppose we want to delete the record of the employee whose employee ID is 105 from the employees
table. However, later we realize that the deletion was a mistake, so we use the ROLLBACK command to
undo the delete and restore the record.
SAVEPOINT Command: A SAVEPOINT is a checkpoint within a transaction that allows you to rollback part of
the transaction instead of undoing all previous changes. It helps in controlling and managing large
transactions step-by-step.
Syntax of SAVEPOINT Command
Examples of SAVEPOINT Command: Suppose we want to perform multiple delete operations in
the employees table :
We first delete the employee with employee ID 101, then we create a SAVEPOINT to mark
that stage.
After that, we delete the employee with employee ID 102.
Later, we decide to undo only the second deletion, so we use ROLLBACK TO SAVEPOINT to
restore the record of employee 102 while keeping the first deletion.