SQL SERVER DATA TYPES
String Data Types
Data type Description Max size Storage
char(n) Fixed width character 8,000 characters Defined width
string
varchar(n) Variable width 8,000 characters 2 bytes + number of
character string chars
varchar(max) Variable width 1,073,741,824 2 bytes + number of
character string characters chars
text Variable width 2GB of text data 4 bytes + number of
character string chars
SQL SERVER DATA TYPES
Numeric Data Types
int Allows whole numbers between - 4 bytes
2,147,483,648 and
2,147,483,647
Date and Time Data Types
datetime 8 bytes
date 3 bytes
time 3-5 bytes
WHAT IS SQL?
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL became a standard of the American National
Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in
1987
WHAT CAN SQL DO?
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
TYPES OF SQL:
1.DDL – Data Definition Language
2.DQL – Data Query Language
3.DML – Data Manipulation Language
4.DCL – Data Control Language
5.TCL – Transaction Control Language
DDL (DATA DEFINITION LANGUAGE)
DDL or Data Definition Language actually consists of the SQL
commands that can be used to define the database schema.
List of DDL commands:
• CREATE: This command is used to create the database or its
objects
• DROP: This command is used to delete objects from the database.
• ALTER: This is used to alter the structure of the database.
• TRUNCATE: This is used to remove all records from a table.
• COMMENT: This is used to add comments to the data dictionary.
• RENAME: This is used to rename an object existing in the
database.
DQL (DATA QUERY
LANGUAGE)
It is a component of SQL statement that allows getting
data from the database and imposing order upon it. It
includes the SELECT statement. This command allows
getting the data out of the database to perform
operations with it.
List of DQL:
• SELECT: It is used to retrieve data from the
database.
DML(DATA MANIPULATION LANGUAGE)
It is the component of the SQL statement that controls
access to data and to the database.
List of DML commands:
• INSERT: It is used to insert data into a table.
• UPDATE: It is used to update existing data within a
table.
• DELETE: It is used to delete records from a database
table.
• LOCK: Table control concurrency.
DCL (DATA CONTROL LANGUAGE)
DCL includes commands such as GRANT and REVOKE which
mainly deal with the rights, permissions, and other controls of
the database system.
List of DCL commands:
• GRANT: This command gives users access privileges to the
database.
Syntax:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
LIST OF DCL COMMANDS:
• REVOKE: This command withdraws the user’s
access privileges given by using the GRANT
command.
Syntax:
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2
;
TCL (TRANSACTION CONTROL LANGUAGE)
TCL commands are used to control the execution of a transaction.
• COMMIT: Commits a Transaction.
Syntax:
COMMIT;
• ROLLBACK: Rollbacks a transaction in case of any error occurs.
Syntax:
ROLLBACK;
• SAVEPOINT: Sets a save point within a transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
SQL CREATE DATABASE STATEMENT
The CREATE DATABASE statement is used to create a new SQL
database.
Syntax:
CREATE DATABASE databasename;
The following SQL statement creates a database called "testDB":
CREATE DATABASE testDBS;
The SQL DROP DATABASE Statement:
Syntax:
DROP DATABASE databasename;
DROP DATABASE testDBS;
THE SQL CREATE TABLE STATEMENT
• 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)
);
THE SQL DROP TABLE
STATEMENT
The following SQL statement drops the existing
table “Persons":
• DROP TABLE table_name;
EXAMPLE:
• DROP TABLE Persons;
SQL TRUNCATE TABLE
Truncate statement is used to delete the data
inside a table, but not the table itself.
Syntax:
TRUNCATE TABLE table_name;
SQL ALTER TABLE STATEMENT
• The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table .
• The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
ALTER TABLE - ADD Column
ALTER TABLE table_name
ADD column_name datatype;
Example:
ALTER TABLE Persons
ADD Email varchar(255);
ALTER TABLE - DROP COLUMN
ALTER TABLE table_name
DROP COLUMN column_name;
Example:
ALTER TABLE Persons
DROP COLUMN Email;
ALTER TABLE - RENAME
COLUMN
• ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
ALTER TABLE - ALTER/MODIFY DATATYPE
• ALTER TABLE table_name
ALTER COLUMN column_name datatype;
SQL SELECT STATEMENT
The SELECT statement is used to select data from a database.
SELECT column1, column2, ...
FROM table_name;
Example:
SELECT CustomerName, City FROM Customers;
Return all the columns from the Customers table:
SELECT * FROM Customers;
SQL SELECT
DISTINCT STATEMENT
The SELECT DISTINCT statement is used to return only distinct (different) values.
Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;
Example:
• Select all the different countries from the "Customers" table:
• SELECT DISTINCT Country FROM Customers;
SELECT Example Without DISTINCT
• If you omit the DISTINCT keyword, the SQL
statement returns the "Country" value from all
the records of the "Customers" table:
Example
• SELECT Country FROM Customers;
COUNT DISTINCT
By using the DISTINCT keyword in a function
called COUNT, we can return the number of
different countries.
Example
• SELECT COUNT(DISTINCT Country) FROM
Customers;
SQL WHERE CLAUSE
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
• Select all customers from Mexico:
SELECT * FROM Customers
WHERE Country='Mexico';
OPERATORS IN THE WHERE CLAUSE
Example
Select all customers with a CustomerID greater
than 80:
• SELECT * FROM Customers
WHERE CustomerID > 80;
COMPARISON OPERATORS
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
SQL ORDER BY KEYWORD
• The ORDER BY keyword is used to sort the result-set in ascending
or descending order.
• Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example
• Sort the products by price:
SELECT * FROM Products
ORDER BY Price;
DESC
• The ORDER BY keyword sorts the records in
ascending order by default. To sort the records in
descending order, use the DESC keyword.
Example
• Sort the products from highest to lowest price:
SELECT * FROM Products
ORDER BY Price DESC;
ORDER ALPHABETICALLY
• For string values the ORDER BY keyword will
order alphabetically:
Example
• Sort the products alphabetically by
ProductName:
SELECT * FROM Products
ORDER BY ProductName;
ALPHABETICALLY DESC
• To sort the table reverse alphabetically, use the
DESC keyword:
Example
• Sort the products by ProductName in reverse
order:
SELECT * FROM Products
ORDER BY ProductName DESC;
ORDER BY SEVERAL COLUMNS
• SELECT * FROM Customers
ORDER BY Country, CustomerName;
Using Both ASC and DESC:
• SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
OTHER OPERATORS
Operator Meaning
BETWEEN Between two values (inclusive)
...AND...
IN(list) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
SQL AND OPERATOR
The WHERE clause can contain one or many AND operators.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
SELECT *
FROM Customers
WHERE Country = 'Spain' AND Customername =‘ali’;
THE SQL OR OPERATOR
• The WHERE clause can contain one or more OR operators.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Example:
SELECT *
FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';
OR VS AND
• The OR operator displays a record if any of the
conditions are TRUE.
• The AND operator displays a record if all the
conditions are TRUE.
NOT OPERATOR
The NOT operator is used in combination with other operators to give the
opposite result , also called the negative result.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Example:
Select only the customers that are NOT from Spain:
• SELECT * FROM Customers
WHERE NOT Country = 'Spain';