SQL Commands
SQL Commands
SQL Commands
What is SQL?
SQL is Structured Query Language, which is a computer language for
storing, manipulating and retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the
Relational Database Management Systems (RDMS) like MySQL, MS Access,
Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their
standard database language.
Why SQL?
SQL is widely popular because it offers the following advantages −
Allows users to access data in the relational database management
systems.
Allows users to describe the data.
Allows users to define the data in a database and manipulate that
data.
Allows to embed within other languages using SQL modules, libraries
& pre-compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a
database.
Allows users to set permissions on tables, procedures and views.
Query Dispatcher
Optimization Engines
Classic Query Engine
SQL Query Engine, etc.
A classic query engine handles all the non-SQL queries, but a SQL query
engine won't handle logical files.
Following is a simple diagram showing the SQL Architecture −
Syntax –
CREATE TABLE table_name
(
column_1 datatype,
column_2 datatype,
column_3 datatype,
....
);
Example –
We need to create a table for storing Student information of a particular
College. Create syntax would be as below.
CREATE TABLE Student_info
(
College_Id number(2),
College_name varchar(30),
Branch varchar(10)
);
Command-2:
ALTER:
This command is used to add, delete or change columns in the existing
table. The user needs to know the existing table name and can do add,
delete or modify tasks easily.
Syntax–
Syntax to add a column to an existing table.
ALTER TABLE table_name
ADD column_name datatype;
Example–
In our Student_info table, we want to add a new column for CGPA. The
syntax would be as below as follows.
ALTER TABLE Student_info
ADD CGPA number;
Command-3:
TRUNCATE:
This command is used to remove all rows from the table, but the structure
of the table still exists.
Syntax–
Syntax to remove an existing table.
TRUNCATE TABLE table_name;
Example–
The College Authority wants to remove the details of all students for new
batches but wants to keep the table structure. The command they can use
is as follows.
TRUNCATE TABLE Student_info;
Command-4:
DROP:
This command is used to remove an existing table along with its structure
from the Database.
Syntax–
Syntax to drop an existing table.
DROP TABLE table_name
Example–
If the College Authority wants to change their Database by deleting the
Student_info Table.
DROP TABLE Student_info;
DML commands in DBMS
The structured query language (SQL) commands deal with the manipulation
of data present in the database that belongs to the DML or Data
Manipulation Language. This includes most of the SQL statements.
The examples of DML in the Database Management System (DBMS) are as
follows −
INSERT
Insert command is used to insert data into a table.
Syntax
Example
Syntax
For example, if we want to select all rows from the Employee database, we
can use the following command −
Example
Syntax
Example
Syntax
For example, if we want to update the name of the employee having the
employee id 001, we can use the command given below −
Example
Syntax:
GRANT privilege_list
ON Object_name
TO user_name;
REVOKE
This command is used to take permission/access back from the user. If you
want to return permission from the database that you have granted to the
users at that time you need to run REVOKE command.
Syntax:
REVOKE privilege_list
ON object_name
FROM user_name;
Following commands are granted to the user as a Privilege List:
EXECUTE
UPDATE
SELECT
DELETE
ALTER
ALL
Advantages Of DCL commands
It allows restricting the user from accessing data in database.
It ensures security in database when the data is exposed to multiple
users.
It is the wholesome responsibility of the data owner or data administrator
to maintain the authority of grant and revoke privileges to the users
preventing any threat to data.
It prevents other users to make changes in database who have no access
to Database
SQL Transaction Control Language (TCL)
TCL stands for Transaction Control Language.
This command is used to manage the changes made by DML statements.
TCL allows the statements to be grouped together into logical transactions.
TCL commands are as follows:
1. COMMIT
2. SAVEPOINT
3. ROLLBACK
4. SET TRANSACTION
1. COMMIT COMMAND
2. SAVEPOINT COMMAND
SAVEPOINT command is used for saving all the current point in the
processing of a transaction.
It marks and saves the current point in the processing of a transaction.
Syntax:
SAVEPOINT <savepoint_name>
Example:
SAVEPOINT no_update;
3. ROLLBACK COMMAND
Example:
ROLLBACK TO SAVEPOINT no_update;
4. SET TRANSACTION
Syntax:
SET TRANSACTION [Read Write | Read Only];
ROLLBACK COMMIT
Syntax: Syntax:
DELETE FROM table_name COMMIT;
ROLLBACK
1. Aggregate functions:
These functions are used to do operations from the values of the column
and a single value is returned.
1. AVG()
2. COUNT()
3. FIRST()
4. LAST()
5. MAX()
6. MIN()
7. SUM()
2. Scalar functions:
These functions are based on user input, these too returns single value.
1. UCASE()
2. LCASE()
3. MID()
4. LEN()
5. ROUND()
6. NOW()
7. FORMAT()
Students-Table
Aggregate Functions
AVG(): It returns average value after calculating from values in a numeric
column.
Syntax:
SELECT AVG(column_name) FROM table_name;
Queries:
1. Computing average marks of students.
SELECT AVG(MARKS) AS AvgMarks FROM Students;
1. Output:
AvgMarks
80
AvgAge
19.4
NumStudents
NumStudents
FIRST(): The FIRST() function returns the first value of the selected
column.
Syntax:
SELECT FIRST(column_name) FROM table_name;
Queries:
1. Fetching marks of first student from the Students table.
SELECT FIRST(MARKS) AS MarksFirst FROM Students;
1. Output:
MarksFirst
90
AgeFirst
19
LAST(): The LAST() function returns the last value of the selected
column. It can be used only in MS ACCESS.
Syntax:
SELECT LAST(column_name) FROM table_name;
Queries:
1. Fetching marks of last student from the Students table.
SELECT LAST(MARKS) AS MarksLast FROM Students;
1. Output:
MarksLast
82
AgeLast
18
MAX(): The MAX() function returns the maximum value of the selected
column.
Syntax:
SELECT MAX(column_name) FROM table_name;
Queries:
1. Fetching maximum marks among students from the Students table.
SELECT MAX(MARKS) AS MaxMarks FROM Students;
1. Output:
MaxMarks
95
MaxAge
21
MIN(): The MIN() function returns the minimum value of the selected
column.
Syntax:
SELECT MIN(column_name) FROM table_name;
Queries:
1. Fetching minimum marks among students from the Students table.
SELECT MIN(MARKS) AS MinMarks FROM Students;
1. Output:
MinMarks
50
MinAge
18
SUM(): The SUM() function returns the sum of all the values of the
selected column.
Syntax:
SELECT SUM(column_name) FROM table_name;
Queries:
1. Fetching summation of total marks among students from the
Students table.
SELECT SUM(MARKS) AS TotalMarks FROM Students;
1. Output:
TotalMarks
400
TotalAge
97
Scalar Functions
UCASE(): It converts the value of a field to uppercase.
Syntax:
SELECT UCASE(column_name) FROM table_name;
Queries:
1. Converting names of students from the table Students to uppercase.
SELECT UCASE(NAME) FROM Students;
1. Output:
NAME
HARSH
SURESH
PRATIK
DHANRAJ
RAM
NAME
harsh
suresh
pratik
dhanraj
ram
MID(): The MID() function extracts texts from the text field.
Syntax:
SELECT MID(column_name,start,length) AS some_name FROM table_name;
specifying length is optional here, and start signifies start position ( starting
from 1 )
Queries:
1. Fetching first four characters of names of students from the Students
table.
SELECT MID(NAME,1,4) FROM Students;
1. Output:
NAME
HARS
SURE
PRAT
DHAN
RAM
LEN(): The LEN() function returns the length of the value in a text field.
Syntax:
SELECT LENGTH(column_name) FROM table_name;
Queries:
1. Fetching length of names of students from Students table.
SELECT LENGTH(NAME) FROM Students;
1. Output:
NAME
MARKS
90
50
80
95
85
NOW(): The NOW() function returns the current system date and time.
Syntax:
SELECT NOW() FROM table_name;
Queries:
1. Fetching current system time.
SELECT NAME, NOW() AS DateTime FROM Students;
1. Output:
NAME DateTime
HARSH 2017-01-13
SURESH 2017-01-13
PRATIK 2017-01-13
DHANRAJ 2017-01-13
RAM 2017-01-13