Escolar Documentos
Profissional Documentos
Cultura Documentos
SQL
SQL is a standard language for accessing and
manipulating databases.
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards
Institute) standard
SQLSyntax
Database Tables
A database most often contains one or more tables.
Each table is identified by a name (e.g. "Customers" or
"Orders"). Tables contain records (rows) with data.
SQLSELECTStatement
The SELECT statement is used to select data
from a database.
SQL SELECT Syntax
SELECT column_name,column_name
FROM table_name;
and
SELECT * FROM table_name;
Tbl_Customers
AccountID
Name
Address
Contact
Age
2014001
Florence Torino
New York
235-12209
21
2014002
Grace More
Miami
243-22104
23
2014003
Joan Black
New York
235-12209
43
2014004
Adam Smith
New York
235-12209
34
2014005
Bryan Gibson
Miami
243-22104
54
2014006
Walter Smith
Boston
236-13254
23
The following SQL statement selects only the distinct values from the "City" columns from the "Customers" table:
Tbl_Customers
AccountID
Name
Address
Contact
Age
2014001
Florence Torino
New York
235-12209
21
2014002
Grace More
Miami
243-22104
23
2014003
Joan Black
New York
235-12209
43
2014004
Adam Smith
New York
235-12209
34
2014005
Bryan Gibson
Miami
243-22104
54
2014006
Walter Smith
Boston
236-13254
23
SQLWHEREClause
The WHERE clause is used to extract only those records
that fulfill a specified criterion.
SQL WHERE Syntax
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
The following SQL statement selects only the distinct values from the "City" columns from the "Customers" table:
Tbl_Customers
AccountID
Name
Address
Contact
Age
2014001
Florence Torino
New York
235-12209
21
2014002
Grace More
Miami
243-22104
23
2014003
Joan Black
New York
235-12209
43
2014004
Adam Smith
New York
235-12209
34
2014005
Bryan Gibson
Miami
243-22104
54
2014006
Walter Smith
Boston
236-13254
23
Description
Equal
<>
Not equal. Note: In some versions of SQL this operator may be written
as !=
>
Greater than
<
Less than
>=
<=
BETWEEN
LIKE
IN
OR Operator Example
The following SQL statement selects all
customers from the city "Berlin" OR "Mnchen",
in the "Customers" table:
Example
SELECT * FROM Customers
WHERE City='Berlin'
OR City='Mnchen';
The following SQL statement selects only the distinct values from the "City" columns from the "Customers" table:
Tbl_Customers
AccountID
Name
Address
Contact
Age
2014001
Florence Torino
New York
235-12209
21
2014002
Grace More
Miami
243-22104
23
2014003
Joan Black
New York
235-12209
43
2014004
Adam Smith
New York
235-12209
34
2014005
Bryan Gibson
Miami
243-22104
54
2014006
Walter Smith
Boston
236-13254
23
The following SQL statement selects only the distinct values from the "City" columns from the "Customers" table:
Tbl_Customers
AccountID
Name
Address
Contact
Age
2014001
Florence Torino
New York
235-12209
21
2014002
Grace More
Miami
243-22104
23
2014003
Joan Black
New York
235-12209
43
2014004
Adam Smith
New York
235-12209
34
2014005
Bryan Gibson
Miami
243-22104
54
2014006
Walter Smith
Boston
236-13254
23
Example
SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='Mnchen');
The following SQL statement selects only the distinct values from the "City" columns from the "Customers" table:
Tbl_Customers
AccountID
Name
Address
Contact
Age
2014001
Florence Torino
New York
235-12209
21
2014002
Grace More
Miami
243-22104
23
2014003
Joan Black
New York
235-12209
43
2014004
Adam Smith
New York
235-12209
34
2014005
Bryan Gibson
Miami
243-22104
54
2014006
Walter Smith
Boston
236-13254
23
SQLORDER BYKeyword
The ORDER BY keyword is used to sort the
result-set by one or more columns.
The ORDER BY keyword sorts the records in
ascending order by default. To sort the records
in a descending order, you can use the DESC
keyword.
CustomerID
CustomerNa
me
ContactName Address
City
PostalCode
Country
Alfreds
Futterkiste
12209
Germany
Ana Trujillo
Ana Trujillo
Emparedado
s y helados
Avda. de la
Constitucin
2222
Mxico D.F.
05021
Mexico
Antonio
Moreno
Taquera
Antonio
Moreno
Mataderos
2312
Mxico D.F.
05023
Mexico
Around the
Horn
Thomas
Hardy
WA1 1DP
UK
Berglunds
snabbkp
Christina
Berglund
Berguvsvge Lule
n8
S-958 22
Sweden
SQLINSERT INTOStatement
The INSERT INTO statement is used to insert
new records in a table.
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
SQLUPDATEStatement
The UPDATE statement is used to update
existing records in a table.
SQL UPDATE Syntax
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
CustomerID
CustomerNa
me
ContactName Address
City
PostalCode
Country
Alfreds
Futterkiste
12209
Germany
Ana Trujillo
Ana Trujillo
Emparedado
s y helados
Avda. de la
Constitucin
2222
Mxico D.F.
05021
Mexico
Antonio
Moreno
Taquera
Antonio
Moreno
Mataderos
2312
Mxico D.F.
05023
Mexico
Around the
Horn
Thomas
Hardy
WA1 1DP
UK
Berglunds
snabbkp
Christina
Berglund
Berguvsvge Lule
n8
S-958 22
Sweden
SQLDELETEStatement
The DELETE statement is used to delete rows in
a table.
SQL DELETE Syntax
CustomerID
CustomerNa
me
ContactName Address
City
PostalCode
Country
Alfreds
Futterkiste
12209
Germany
Ana Trujillo
Ana Trujillo
Emparedado
s y helados
Avda. de la
Constitucin
2222
Mxico D.F.
05021
Mexico
Antonio
Moreno
Taquera
Antonio
Moreno
Mataderos
2312
Mxico D.F.
05023
Mexico
Around the
Horn
Thomas
Hardy
WA1 1DP
UK
Berglunds
snabbkp
Christina
Berglund
Berguvsvge Lule
n8
S-958 22
Sweden
END OF SQL