TABLE OF CONTENT
Sr.No. Topics Page No.
1. SELECT 3-4
2. WHERE 6
3. SQLAND, OR NOT Operators 8-9
4. SQL INSERT INTO STATEMENT 11
5. IS NULL 13
6. UPDATE 15
7. DELETE 17
8. THE SQL CREATE DATBASE STATEMENT 18
9. THE SQL CREATE TABLE STATEMENT 18
10. THE SQL DRROP TABLE STATEMENT 19
11. SQL TRUNCATE TABLE 19
1
Demo Database
Below is a selection from the “Customers” table in the
Northwind sample database:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
1. Alfreds Alfred Obere Str.57 Frank 11209 Germany
Futterkiste Schmidt Furt
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
2
Select
Syntax:
Select column1,column2,………
from table name;
Example:
Select Customer Name, City From Customers;
Result:
Customer Name City
Alfreds Futterkiste Frank furt
Ana Trujillo Emparedados y helados Mexico D.F.
Antonio Moreno Taqueria Mexico D.F.
Around the Horn London
Berglunds Snabbkop Lulea
Syntax:
Select * from table name;
Example:
Select * from Customers;
Result:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
1. Alfreds Alfred Obere Str.57 Frank 11209 Germany
Futterkiste Schmidt Furt
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
3
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
4
Demo Database
Below is a selection from the “Customers” table in the
Northwind sample database:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
1. Alfreds Alfred Obere Str.57 Frank 11209 Germany
Futterkiste Schmidt Furt
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
5
Where
Syntax:
Select column1,column2,………
from table name
where condition;
Example:
Select * from Customers
where country=Mexico;
Result:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
2. Ana Trujillo Ana Avda. De la Mexico 05021 Mexico
Emparedados Trujillo Constitucion D.F.
U helados
3. Antonio Antonia Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F.
Taqueria
Example:
Select * from Customers
where customerID=1;
Result:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
1. Alfreds Alfred Obere Str.57 Frank 11209 Germany
Futterkiste Schmidt Furt
6
Demo Database
Below is a selection from the “Customers” table in the
Northwind sample database:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
1. Alfreds Alfred Obere Str.57 Frank 11209 Germany
Futterkiste Schmidt Furt
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
7
Sql AND, OR, NOT Operators
AND Syntax:
Select column1,column2,………
from table name
where condition1 AND condition2 AND condition3…..;
Example:
Select * from Customers
where country=’Germany’ AND city=’Berlin’;
Result:
No result
OR Syntax:
Select column1,column2,………
from table name
where condition1 OR condition2;
Example:
Select * from Customers
where city=’London’ OR city=’Berlin’;
Result:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
8
NOT Syntax:
Select column1,column2,………
from table name
where NOT condition;
Example:
Select * from Customers
where NOT country=’Germany’;
Result:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
9
Demo Database
Below is a selection from the “Customers” table in the
Northwind sample database:
CustomerI Custome Contact Address City Postal Country
D r Name Code
Name
89. White Karl 305-14th Seattle 98128 USA
Clover Jablonski Ave.S.
Markets Suite 3B
90. Wilman Matti Keskuskatu Helsinki 21240 Finland
Kala Karttunen 45
91. Wolski Zbyszek UI. Walla 01-012 Poland
Filtrowa 68
10
Sql INSERT INTO Statement:
Insert Into Syntax:
Insert into table name(Column1, Column2, Column3,…..)
values(Value1, Value2, Value3,…….);
Example:
Insert into Customers(CustomerName, ContactName, Address, City,
Postal Code, Country)
values(‘Cardinal’,’Tom B.
Enrichen’,’Skagen21’,’Stavanger’,’4006’,’Norway’);
Result:
CustomerI Custome Contact Address City Postal Country
D r Name Code
Name
89. White Karl 305-14th Seattle 98128 USA
Clover Jablonski Ave.S.
Markets Suite 3B
90. Wilman Matti Keskuskatu Helsinki 21240 Finland
Kala Karttunen 45
91. Wolski Zbyszek UI. Walla 01-012 Poland
Filtrowa 68
92. Cradinal Tom B. Skagen21 Stavanger 4006 Norway
Enrichen
11
Demo Database
Assume we have the following “Persons” table:
ID Last_Name First_Name Address City
TH
1. Doe John 542 w.27 New York
Street
2. Bloggs Joe London
3. Roe Jane New York
4. Smith John 110 London
Bishopsgate
12
Is NULL
IS NULL Syntax:
Select table name
WHERE column name IS NULL;
Example:
Select Last_Name, First_Name, Address from Persons
WHERE Address IS NULL;
Result:
Last_Name First_Name Address
Bloggs Joe
Roe Jane
13
Demo Database
Below is a selection from the “Customers” table in the
Northwind sample database:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
1. Alfreds Alfred Obere Str.57 Frank 11209 Germany
Futterkiste Schmidt Furt
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
14
UPDATE
Update Syntax:
UPDATE table name
SET column1=value1, column2= value2,…….
WHERE condition;
Example:
Select UPDATE customers
SET CustomerName=’Alfred Schmid’, City=’Frankfurt’
WHERE CustomerID=1;
Result:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
1. Alfred Alfred Obere Str.57 Frank 11209 Germany
Schmidt Schmidt Furt
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
15
Demo Database
Below is a selection from the “Customers” table in the
Northwind sample database:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
1. Alfreds Alfred Obere Str.57 Frank 11209 Germany
Futterkiste Schmidt Furt
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
16
DELETE
Delete Syntax:
DELETE FROM table name
WHERE condition;
Example:
Select DELETE FROM Customers
WHERE CustomerName= ‘Alfreds Futterkiste’;
Result:
CustomerI Customer Contact Address City Postal Country
D Name Name Code
2. Ana Turjillo Ana Avda. De la Mexico 05021 Mexico
Emparedado Turjillo Constitucion D.F
s 2222
Yhelados
3. Antonio Antonio Mataderos Mexico 05023 Mexico
Moreno Moreno 2312 D.F
Taqueria
4. Around the Thomas 120 Hanover London WAI IDP UK
Horn Hardy Sq.
5. Berglunds Christina Berguvsvagen Lulea S-958 22 Sweden
Snabbkop Berglund
Delete ALL Records:
Syntax:
DELETE FROM table name;
OR
DELETE * FROM table name;
17
The SQL CREATE DATABASE Statement
Syntax:
CREATE DATABASE database name;
Example:
CREATE DATABASE test;
The SQL CREATE TABLE Statement
Syntax:
CREATE TABLE table name(
Column1 datatype,
Column2 datatype,
Column3 datatype,
……….
);
Example:
CREATE TABLE Persons(
Persons_ID int,
Last_Name varchar(225),
First_Name varchar(225),
Address varchar(225),
18
City varchar(255),
);
Result:
Result:
Person_Id Last_Name First_Nam Address City
e
The SQL DROP TABLE Statement
Syntax:
DROP TABLE table name;
Example:
DROP TABLE Shippers;
The SQL TRUNCATE TABLE Statement
Syntax:
TRUNCATE TABLE table name;
19
20