SQL is a database computer language designed for the retrieval and
management of data in a relational database. SQL stands for Structured Query
Language.
SQL is the standard language for Relational Database System.
used 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.
MS SQL Server using T-SQL,
Oracle using PL/SQL,
MS Access version of SQL is called JET SQL (native format) etc.
Applications of SQL
SQL is one of the most widely used query language over the databases.
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 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.
SQL Commands
The standard SQL commands to interact with relational databases are
CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands
can be classified into the following groups based on their nature −
DDL - Data Definition Language
CREATE
Creates a new table, a view of a table, or other object in the database.
ALTER
Modifies an existing database object, such as a table.
DROP
Deletes an entire table, a view of a table or other objects in the
database.
DML - Data Manipulation Language
SELECT
Retrieves certain records from one or more tables.
INSERT
Creates a record.
UPDATE
Modifies records.
DELETE
Deletes records.
DCL - Data Control Language
GRANT
Gives a privilege to user.
REVOKE
Takes back privileges granted from user.
Create Table
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
)
Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
Alter Table :
ALTER TABLE table_name
ADD column_name datatype;
Example
ALTER TABLE Customers
ADD Email varchar(255);
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE Customers
DROP COLUMN Email;
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
Drop Table
Drop Table Tablename
Truncate Table
Truncate table Tablename
DML Statements
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006',
'Norway');
Inserting data into few columns
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
SQL DELETE Statement
The DELETE statement is used to delete existing records in a table.
DELETE Syntax
DELETE FROM table_name WHERE condition;
Select Statement
The SQL SELECT Statement
SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
SELECT column1, column2, ...
FROM table_name;
SELECT CustomerName, City FROM Customers;
SELECT * FROM table_name;
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT * FROM Customers
WHERE Country='Mexico';
SELECT * FROM Customers
WHERE CustomerID=1001;
Operators used in Select Query
Greater than
Less Than
Greater than or equal to
Less than or equal to
Equal to
Not equal to
Like
Not like
Between