Structured Query Language: SQL Is A Standard Language For Accessing and Manipulating Databases
Structured Query Language: SQL Is A Standard Language For Accessing and Manipulating Databases
SQL Syntax
SQL is NOT case sensitive: select is the same as SELECT
Some database systems require a semicolon at the end of each SQL statement.
- use semicolon at the end of each SQL statement.
Some of The Most Important SQL Commands
CREATE DATABASE
ALTER DATABASE
CREATE TABLE
ALTER TABLE
DROP TABLE
Example
Example
SELECT * FROM Customers
WHERE Country='Mexico';
Text Fields vs. Numeric Fields
SQL requires single quotes around text values (most database systems will also allow double
quotes).
However, numeric fields should not be enclosed in quotes:
Example
SELECT * FROM Customers
WHERE CustomerID=1;
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 second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Result:
91
92
Wolski
Cardinal
Zbyszek
null
Filtrowa 68
null
Walla
Stavanger
01-012
null
Poland
Norway
Note: Be very careful when deleting records. You cannot undo this statement!
Example
SELECT TOP 2 * FROM Customers;
Example
SELECT TOP 50 PERCENT * FROM Customers;
Tip: The "%" sign is used to define wildcards (missing letters) both before and after the pattern.
You will learn more about wildcards in the next chapter.
The following SQL statement selects all customers with a City ending with the letter "s":
Example
SELECT * FROM Customers
WHERE City LIKE '%s';
The following SQL statement selects all customers with a Country containing the pattern "land":
Example
SELECT * FROM Customers
WHERE Country LIKE '%land%';
Using the NOT keyword allows you to select records that do NOT match the pattern.
The following SQL statement selects all customers with Country NOT containing the pattern
"land":
Example
SELECT * FROM Customers
WHERE Country NOT LIKE '%land%';
Description
[charlist]
[^charlist]
or
[!charlist]
The following SQL statement selects all customers with a City starting with any character,
followed by "erlin":
Example
SELECT * FROM Customers
WHERE City LIKE '_erlin';
The following SQL statement selects all customers with a City starting with "L", followed by any
character, followed by "n", followed by any character, followed by "on":
Example
SELECT * FROM Customers
WHERE City LIKE 'L_n_on';