SQL Commands
SQL Commands
What is SQL?
• SQL is stand for structured query language.
• This database language is mainly designed
for maintaining the data in relational
database management systems.
• SQL is standard language for accessing and
manipulating database.
01
SQL Commands
02
SQL Commands
SQL COMMANDS
03
SQL Commands
DDL
DDL stands for Data Definition Language.
DDL used to change the structure of the table Like
creating the table, altering the table and deleting the
table.
All the commands in the DDL are auto Committed that
means it permanently saves all the changes in the
database.
04
SQL Commands
CREATE
This command is used to create a new database or table.
Syntax:
CREATE TABLE table_name
(
columnl datatype,
column2 datatype,
column3 datatype,
);
Example:
CREA TE TABLE Employee
(
EmployeelD int,
FirstName varchar(255),
LastName varchar(255),
Addressline varchar(255),
City varchar(255)
);
05
SQL Commands
ALTER
The ALTER TABLE statement in Structured Query Language
allows you to add, modify, and delete columns of an existing
table.
Syntax:
ALTER TABLE table name
ADD column _name datatype;
Example:
ALTER TABLE Employee
ADD Email varchar(255);
06
SQL Commands
DROP
The DROP TABLE statement is used to drop an existing table
in a database. This command deletes both the structure &
records Stored in table.
Syntax:
DROP TABLE table_name;
Example:
Drop TABLE Employee;
07
SQL Commands
TRUNCATE
A truncate SQL statement is used to remove all rows
(complete data) from a table. It is similar to the DELETE
statement with no WHERE clause.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE Employee;
08
SQL Commands
DML
DML stands for Data Manipulation
Language.
DML commands are used to interact with
the data stored in a database.
These commands are primarily
responsible for adding, modifying, and
deleting data within database tables.
09
SQL Commands
INSERT
SQL INSERT statement is a SQL query. It is used to insert a
single or a multiple records in a table.
Syntax:
INSERT INTO table name
VALUES (valuel, value2, value3 .... ) ;
Example:
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (1, Sara , 19, Islamabad) ;
10
SQL Commands
UPDATE
The UPDATE statement is used to modify the existing
records in a table.
Syntax:
UPDATE table name
SET columnl = valuel, column2 = value2, ...
WHERE condition;
Example:
UPDATE Customers
SET ContactName = 'Zara', City= 'Lahore'
WHERE CustomerlD = 101;
11
SQL Commands
DELETE
The DELETE statement is used to delete existing records in a
table.
Syntax:
DELETE FROM table_name [WHERE condition] ;
Example:
DELETE FROM Customers WHERE CustomerName='Yadu";
12
SQL Commands
SELECT
It is often grouped with DML commands because it retrieves
and manipulates data from a database.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT first_name, last_name
FROM employees
WHERE age > 30;
13
SQL Commands
DCL
DCL stands for Data Control Language.
It is a subset of SQL commands used to
control access to data within a database.
DCL commands are primarily concerned
with defining the permissions and
privileges granted to database users.
14
SQL Commands
GRANT
It is used to give user access privileges to a database.
Syntax:
GRANT SELECT, UPDATE ON MY_ TABLE TO SOME_ USER, ANOTHER_USER;
Example:
GRANT SELECT, INSERT, UPDATE ON employees TO user1;
15
SQL Commands
REVOKE
The REVOKE command is used to remove previously granted
privileges from database users.
Syntax:
REVOKE privileges ON object FROM user;
Example:
REVOKE SELECT, INSERT, UPDATE ON employees FROM user1;
16
SQL Commands
TCL
"TCL" stands for Transaction Control
Language.
It is a subset of SQL commands used to
manage transactions within a database.
TCL commands are responsible for
controlling the beginning and ending of
transactions, ensuring the integrity and
consistency of the database.
17
SQL Commands
COMMIT
Commits a Transaction. The COMMIT command saves all the
transactions to the database since the last COMMIT or
ROLLBACK command.
Syntax:
COMMIT;
Example:
DELETE FROM Student WHERE AGE = 20;
COMMIT;
18
SQL Commands
ROLLBACK
If any error occurs with any of the SQL grouped statements,
all changes need to be aborted. The process of reversing
changes is called rollback
Syntax:
ROLLBACK;
Example:
DELETE FROM Student WHERE AGE = 20;
ROLLBACK;
19
SQL Commands
SAVEPOINT
Sets a point within the current transaction to which you can
later roll back.
Syntax:
SAVEPOINT savepoint_name;
Example:
SAVEPOINT before_hr_update;
20
SQL Commands
SET TRANSACTION
Sets characteristics for the transaction, such as isolation
level or access mode.
Syntax:
SET TRANSACTION [ transaction_mode [, ...] ];
Example:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
21
SQL Commands
DQL
DQL stands for Data Query Language.
It's a subset of SQL that specifically deals
with querying or retrieving data from a
database.
DQL commands are used to retrieve,
filter, and manipulate data stored in
database tables.
22
SQL Commands
SELECT
SELECT is primarily considered a query command, because it
retrieves and manipulates data from a database.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT first_name, last_name
FROM employees
WHERE age > 30;
23
SQL OPERATORS
SQL OPERATORS
The SQL reserved words and characters are called
operators, which are used with a WHERE clause in a
SQL query.
24
SQL OPERATORS
Arithmetic Operators
Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus (Remainder): %
25
SQL OPERATORS
Examples
SELECT 10 + 5 AS AdditionResult,
10 - 5 AS SubtractionResult,
10 * 5 AS MultiplicationResult,
10 / 5 AS DivisionResult,
10 % 3 AS ModulusResult;
26
SQL OPERATORS
Comparison Operators
Equal to: =
Not equal to: <> or !=
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
27
SQL OPERATORS
Examples
SELECT * FROM Products WHERE Price > 100;
28
SQL OPERATORS
Logical Operators
AND: AND
OR: OR
NOT: NOT
29
SQL OPERATORS
Examples
SELECT * FROM Employees WHERE Department = 'Sales'
AND Salary > 50000;
30
SQL OPERATORS
Bitwise Operators
Bitwise AND: &
Bitwise OR: |
Bitwise NOT: ~
Bitwise XOR: ^
Left Shift: <<
Right Shift: >>
31
SQL OPERATORS
Examples
SELECT (5 & 3) AS BitwiseANDResult,
(5 | 3) AS BitwiseORResult,
(~5) AS BitwiseNOTResult,
(5 ^ 3) AS BitwiseXORResult;
32
SQL OPERATORS
Concatenation Operator
Concatenate strings: || (Oracle, PostgreSQL, SQLite),
CONCAT() (MySQL, SQL Server)
33
SQL OPERATORS
Examples
SELECT FirstName || ' ' || LastName AS FullName FROM
Employees;
34
SQL OPERATORS
Assignment Operator
Assign a value to a variable or column: =
35
SQL OPERATORS
Examples
UPDATE Employees SET Salary = 60000 WHERE
Department = 'Sales';
36
SQL OPERATORS
IN Operator
Checks whether a value exists in a list of values: IN
37
SQL OPERATORS
Examples
SELECT * FROM Products WHERE Category IN ('Electronics',
'Appliances');
38
SQL OPERATORS
LIKE Operator
Pattern matching using wildcards: LIKE
39
SQL OPERATORS
Examples
SELECT * FROM Customers WHERE LastName LIKE 'S%';
40
SQL OPERATORS
IS NULL Operator
Checks if a value is NULL: IS NULL
41
SQL OPERATORS
Examples
SELECT * FROM Orders WHERE ShipDate IS NULL;
42
SQL OPERATORS
BETWEEN Operator
Checks if a value is within a range: BETWEEN
43
SQL OPERATORS
Examples
SELECT * FROM Sales WHERE SaleAmount BETWEEN 1000
AND 5000;
44
SQL OPERATORS
EXISTS Operator
Checks for the existence of rows in a subquery: EXISTS
45
SQL OPERATORS
Examples
SELECT * FROM Orders WHERE EXISTS (SELECT * FROM
Customers WHERE Orders.CustomerID =
Customers.CustomerID);
46
SQL KEYS
SQL KEYS
In SQL, keys are important concepts for defining
relationships between tables and ensuring data
integrity.
47
SQL KEYS
Primary Key
A primary key uniquely identifies each record in a table. It
must be unique and not NULL.
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);
48
SQL KEYS
Foreign Key
A foreign key establishes a relationship between two tables
by referencing the primary key of another table.
Example:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
49
SQL KEYS
Composite Key
A composite key consists of two or more columns that
together uniquely identify each record in a table.
Example:
CREATE TABLE Students (
StudentID INT,
CourseID INT,
EnrollmentDate DATE,
PRIMARY KEY (StudentID, CourseID)
);
50
SQL KEYS
Unique Key
A unique key ensures that each value in a column (or a group
of columns) is unique, but unlike a primary key, it can
contain NULL values.
Example:
CREATE TABLE Users (
UserID INT PRIMARY KEY,
Username VARCHAR(50) UNIQUE,
Email VARCHAR(100) UNIQUE
);
51
SQL KEYS
Candidate Key
A candidate key is a column or set of columns that can
uniquely identify a record in a table. It may or may not be
chosen as the primary key.
Example:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductCode VARCHAR(20) UNIQUE,
ProductName VARCHAR(100),
VendorID INT
);
52
SQL KEYS
Alternate Key
An alternate key is a candidate key that was not chosen as
the primary key. It can be used as a unique identifier for
records.
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
SSN VARCHAR(20) UNIQUE,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);
53