SQL w3
SQL w3
SQL
HTML / CSS
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
The "Persons" table contains three rows (one for each person) and five columns (P_Id, LastName,
FirstName, Address, and City).
Another example is a table called "Orders":
O_Id
OrderNo
P_Id
77895
44678
22456
24562
The "Orders" table contains five rows (one for each order) and three columns (O_Id, OrderNo, P_Id).
Notice that the relationship between the two tables above is the "P_Id" column (which refers to the persons
in the "Persons" table without using their names).
The data in RDBMS is stored into group of tables, which might or might not be related.
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.
Below is an example of a table called "Persons":
P_Id
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
The table above contains three records (one for each person) and five columns (P_Id, LastName, FirstName,
Address, and City).
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements.
The following SQL statement will select all the records in the "Persons" table:
The DDL part of SQL permits database tables to be created or deleted. It also define indexes (keys), specify
links between tables, and impose constraints between tables. The most important DDL statements in SQL
are:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Now we want to select the content of the columns named "LastName" and "FirstName" from the table
above.
We use the following SELECT statement:
FirstName
Hansen
Ola
Svendson
Tove
Pettersen
Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Navigation in a Result-set
Most database software systems allow navigation in the result-set with programming functions, like: MoveTo-First-Record, Get-Record-Content, Move-To-Next-Record, etc.
Programming functions like these are not a part of this tutorial. To learn about accessing data with function
calls, please visit our ADO tutorial or our PHP tutorial.
In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes
you will want to list only the different (distinct) values in a table.
The DISTINCT keyword can be used to return only distinct (different) values.
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Now we want to select only the distinct values from the column named "City" from the table above.
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Now we want to select only the persons living in the city "Sandnes" from the table above.
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove
For numeric values:
This is correct:
SELECT * FROM Persons WHERE Year=1965
This is wrong:
SELECT * FROM Persons WHERE Year='1965'
Equal
<>
Not equal
>
Greater than
<
Less than
>=
<=
IN
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Now we want to select only the persons with the first name equal to "Tove" AND the last name equal to
"Svendson":
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to "Ola":
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
ORDER BY Example
The "Persons" table:
P_Id
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Nilsen
Tom
Vingvn 23
Stavanger
Now we want to select all the persons from the table above, however, we want to sort the persons by their
last name.
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Nilsen
Tom
Vingvn 23
Stavanger
Pettersen
Kari
Storgt 20
Stavanger
Svendson
Tove
Borgvn 23
Sandnes
Now we want to select all the persons from the table above, however, we want to sort the persons
descending by their last name.
We use the following SELECT statement:
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Nilsen
Tom
Vingvn 23
Stavanger
Hansen
Ola
Timoteivn 10
Sandnes
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Nilsen
Tom
Vingvn 23
Stavanger
Now we want to select only the two first records in the table above.
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Nilsen
Tom
Vingvn 23
Stavanger
Now we want to select only 50% of the records in the table above.
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Now we want to select the persons living in a city that starts with "s" from the table above.
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Next, we want to select the persons living in a city that ends with an "s" from the "Persons" table.
We use the following SELECT statement:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Next, we want to select the persons living in a city that contains the pattern "tav" from the "Persons" table.
We use the following SELECT statement:
LastName
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
The IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
IN Operator Example
The "Persons" table:
P_Id
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Now we want to select the persons with a last name equal to "Hansen" or "Pettersen" from the table above.
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
The BETWEEN operator is used in a WHERE clause to select a range of data between two values.
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Now we want to select the persons with a last name alphabetically between "Hansen" and "Pettersen" from
the table above.
We use the following SELECT statement:
WHERE LastName
BETWEEN 'Hansen' AND 'Pettersen'
The result-set will look like this:
P_Id
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Example 2
To display the persons outside the range in the previous example, use NOT BETWEEN:
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
SQL Alias
You can give a table or a column another name by using an alias. This can be a good thing to do if you have
very long or complex table names or column names.
An alias name could be anything, but usually it is short.
Alias Example
Assume we have a table called "Persons" and another table called "Product_Orders". We will give the table
aliases of "p" an "po" respectively.
Now we want to list all the orders that "Ola Hansen" is responsible for.
We use the following SELECT statement:
The JOIN keyword is used to query data from two or more tables, based on a relationship
between certain columns in these tables.
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a
relationship between certain columns in these tables.
Tables in a database are often related to each other with keys.
A primary key is a column with a unique value for each row. Each primary key value must be unique within
the table. The purpose is to bind data together, across tables, without repeating all of the data in every
table.
Look at the "Persons" table:
P_Id
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two rows can
have the same P_Id. The P_Id distinguishes two persons even if they have the same name.
Next, we have the "Orders" table:
O_Id
OrderNo
P_Id
77895
44678
22456
24562
34764
15
Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id" column refers to
the persons in the "Persons" table without using their names.
Notice that the relationship between the two tables above is the "P_Id" column.
JOIN: Return rows when there is at least one match in both tables
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
FULL JOIN: Return rows when there is a match in one of the tables
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
OrderNo
P_Id
77895
44678
22456
24562
34764
15
FirstName
OrderNo
Hansen
Ola
22456
Hansen
Ola
24562
Pettersen
Kari
77895
Pettersen
Kari
44678
The INNER JOIN keyword return rows when there is at least one match in both tables. If there are rows in
"Persons" that do not have matches in "Orders", those rows will NOT be listed.
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
OrderNo
P_Id
77895
44678
22456
24562
34764
15
Now we want to list all the persons and their orders - if any, from the tables above.
We use the following SELECT statement:
FirstName
OrderNo
Hansen
Ola
22456
Hansen
Ola
24562
Pettersen
Kari
77895
Pettersen
Kari
44678
Svendson
Tove
The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in
the right table (Orders).
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
O_Id
OrderNo
P_Id
77895
44678
22456
24562
34764
15
Now we want to list all the orders with containing persons - if any, from the tables above.
We use the following SELECT statement:
FirstName
OrderNo
Hansen
Ola
22456
Hansen
Ola
24562
Pettersen
Kari
77895
Pettersen
Kari
44678
34764
The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in
the left table (Persons).
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
OrderNo
P_Id
77895
44678
22456
24562
34764
15
Now we want to list all the persons and their orders, and all the orders with their persons.
We use the following SELECT statement:
FirstName
OrderNo
Hansen
Ola
22456
Hansen
Ola
24562
Pettersen
Kari
77895
Pettersen
Kari
44678
Svendson
Tove
34764
The FULL JOIN keyword returns all the rows from the left table (Persons), and all the rows from the right
table (Orders). If there are rows in "Persons" that do not have matches in "Orders", or if there are rows in
"Orders" that do not have matches in "Persons", those rows will be listed as well.
The SQL UNION operator combines the result-set of two or more SELECT statements.
E_Name
01
Hansen, Ola
02
Svendson, Tove
03
Svendson, Stephen
04
Pettersen, Kari
"Employees_USA":
E_ID
E_Name
01
Turner, Sally
02
Kent, Clark
03
Svendson, Stephen
04
Scott, Stephen
Note: This command cannot be used to list all employees in Norway and USA. In the example above we
have two employees with equal names, and only one of them will be listed. The UNION command only
selects distinct values.
Create a Database
To create a database:
Create a Table
To create a table in a database:
Create Index
Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an
index on one or more columns of a table, and each index is given a name. The users cannot see the indexes,
they are just used to speed up queries.
Note: Updating a table containing indexes takes more time than updating a table without, this is because
the indexes also need an update. So, it is a good idea to create indexes only on columns that are often used
for a search.
A Unique Index
Creates a unique index on a table. A unique index means that two rows cannot have the same index value.
Example
This example creates a simple index, named "PersonIndex", on the LastName field of the Person table:
Drop Index
You can delete an existing index in a table with the DROP INDEX statement.
Syntax for Microsoft SQLJet (and Microsoft Access):
Truncate a Table
What if we only want to get rid of the data inside a table, and not the table itself? Use the TRUNCATE TABLE
command (deletes only the data inside the table):
ALTER TABLE
The ALTER TABLE statement is used to add or drop columns in an existing table.
Person:
LastName
FirstName
Address
Pettersen
Kari
Storgt 20
Example
To add a column named "City" in the "Person" table:
FirstName
Address
Pettersen
Kari
Storgt 20
City
Example
To drop the "Address" column in the "Person" table:
FirstName
Pettersen
Kari
City
Syntax
INSERT INTO table_name
VALUES (value1, value2,....)
You can also specify the columns for which you want to insert data:
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
Hetland
Camilla
Hagabakka 24
Sandnes
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
Hetland
Camilla
Hagabakka 24
Sandnes
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
Hetland
Camilla
Hagabakka 24
Sandnes
Rasmussen
Storgt 67
Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value
Person:
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Storgt 67
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
Storgt 67
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
Result:
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
Stien 12
Stavanger
Syntax
DELETE FROM table_name
WHERE column_name = some_value
Person:
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
Stien 12
Stavanger
Delete a Row
"Nina Rasmussen" is going to be deleted:
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Function Syntax
The syntax for built-in SQL functions is:
Types of Functions
There are several basic types and categories of functions in SQL. The basic types of functions are:
Aggregate Functions
Scalar functions
Aggregate functions
Aggregate functions operate against a collection of values, but return a single value.
Note: If used among many other expressions in the item list of a SELECT statement, the SELECT must have
a GROUP BY clause!!
Age
Hansen, Ola
34
Svendson, Tove
45
Pettersen, Kari
19
Description
AVG(column)
COUNT(column)
COUNT(*)
FIRST(column)
LAST(column)
MAX(column)
MIN(column)
STDEV(column)
STDEVP(column)
SUM(column)
VAR(column)
VARP(column)
Description
AVG(column)
BINARY_CHECKSUM
CHECKSUM
CHECKSUM_AGG
COUNT(column)
COUNT(*)
COUNT(DISTINCT column)
FIRST(column)
Returns the value of the first record in a specified field (not supported
in SQLServer2K)
LAST(column)
Returns the value of the last record in a specified field (not supported
in SQLServer2K)
MAX(column)
MIN(column)
STDEV(column)
STDEVP(column)
SUM(column)
VAR(column)
VARP(column)
Scalar functions
Scalar functions operate against a single value, and return a single value based on the input value.
Description
UCASE(c)
LCASE(c)
MID(c,start[,end])
LEN(c)
INSTR(c,char)
LEFT(c,number_of_char)
RIGHT(c,number_of_char)
ROUND(c,decimals)
MOD(x,y)
NOW()
FORMAT(c,format)
DATEDIFF(d,date1,date2)
GROUP BY...
GROUP BY... was added to SQL because aggregate functions (like SUM) return the aggregate of all column
values every time they are called, and without the GROUP BY function it was impossible to find the sum for
each individual group of column values.
The syntax for the GROUP BY function is:
GROUP BY Example
This "Sales" Table:
Company
Amount
W3Schools
5500
IBM
4500
W3Schools
7100
SUM(Amount)
W3Schools
17100
IBM
17100
W3Schools
17100
The above code is invalid because the column returned is not part of an aggregate. A GROUP BY clause will
solve this problem:
SUM(Amount)
W3Schools
12600
IBM
4500
HAVING...
HAVING... was added to SQL because the WHERE keyword could not be used against aggregate functions
(like SUM), and without HAVING... it would be impossible to test for result conditions.
The syntax for the HAVING function is:
Amount
W3Schools
5500
IBM
4500
W3Schools
7100
This SQL:
SUM(Amount)
W3Schools
12600
Syntax
SELECT column_name(s) INTO newtable [IN externaldatabase]
FROM source
If you only want to copy a few fields, you can do so by listing them after the SELECT statement:
SELECT Employees.Name,Orders.Product
INTO Empl_Ord_backup
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
A view is a virtual table based on the result-set of a SELECT statement.
What is a View?
In SQL, a VIEW is a virtual table based on the result-set of a SELECT statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real
tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the
data as if the data were coming from a single table.
Note: The database design and structure will NOT be affected by the functions, where, or join statements in
a view.
Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Note: The database does not store the view data! The database engine recreates the data, using the view's
SELECT statement, every time a user queries a view.
Using Views
A view could be used from inside a query, a stored procedure, or from inside another view. By adding
functions, joins, etc., to a view, it allows you to present exactly the data you want to the user.
The sample database Northwind has some views installed by default. The view "Current Product List" lists all
active products (products that are not discontinued) from the Products table. The view is created with the
following SQL:
Data types and ranges for Microsoft Access, MySQL and SQL Server.
Description
Storage
Text
Use for text or combinations of text and numbers. 255 characters maximum
Memo
Byte
1 byte
Integer
2 bytes
Long
4 bytes
Single
4 bytes
Double
8 bytes
Currency
8 bytes
AutoNumber
AutoNumber fields automatically give each record its own number, usually
starting at 1
4 bytes
Date/Time
8 bytes
Yes/No
Ole Object
Can store pictures, audio, video, or other BLOBs (Binary Large OBjects)
Hyperlink
Lookup Wizard
Let you type a list of options, which can then be chosen from a drop-down
list
up to 1GB
4 bytes
Description
CHAR(size)
Holds a fixed length string (can contain letters, numbers, and special characters). The
fixed size is specified in parenthesis. Can store up to 255 characters
VARCHAR(size)
Holds a variable length string (can contain letters, numbers, and special characters).
The maximum size is specified in parenthesis. Can store up to 255 characters. Note: If
you put a greater value than 255 it will be converted to a TEXT type
TINYTEXT
TEXT
BLOB
MEDIUMTEXT
MEDIUMBLOB
LONGTEXT
LONGBLOB
ENUM(x,y,z,etc.)
Let you enter a list of possible values. You can list up to 65535 values in an ENUM list.
If a value is inserted that is not in the list, a blank value will be inserted.
Note: The values are sorted in the order you enter them.
You enter the possible values in this format: ENUM('X','Y','Z')
SET
Number types:
Similar to ENUM except that SET may contain up to 64 list items and can store more
than one choice
Data type
Description
TINYINT(size)
-128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits may be
specified in parenthesis
SMALLINT(size)
-32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number of digits may
be specified in parenthesis
MEDIUMINT(size)
INT(size)
BIGINT(size)
FLOAT(size,d)
A small number with a floating decimal point. The maximum number of digits may be
specified in the size parameter. The maximum number of digits to the right of the
decimal point is specified in the d parameter
DOUBLE(size,d)
A large number with a floating decimal point. The maximum number of digits may be
specified in the size parameter. The maximum number of digits to the right of the
decimal point is specified in the d parameter
DECIMAL(size,d)
A DOUBLE stored as a string , allowing for a fixed decimal point. The maximum
number of digits may be specified in the size parameter. The maximum number of
digits to the right of the decimal point is specified in the d parameter
*The integer types have an extra option called UNSIGNED. Normally, the integer goes from an negative to
positive value. Adding the UNSIGNED attribute will move that range up so it starts at zero instead of a
negative number.
Date types:
Data type
Description
DATE()
DATETIME()
TIMESTAMP()
*A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix
epoch ('1970-01-01 00:00:00' UTC). Format: YYYY-MM-DD HH:MM:SS
Note: The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09
03:14:07' UTC
TIME()
YEAR()
*Even if DATETIME and TIMESTAMP return the same format, they work very differently. In an INSERT or
UPDATE query, the TIMESTAMP automatically set itself to the current date and time. TIMESTAMP also
accepts various formats, like YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or YYMMDD.
Description
Storage
char(n)
varchar(n)
varchar(max)
text
Unicode strings:
Data type
Description
nchar(n)
nvarchar(n)
nvarchar(max)
ntext
Storage
Binary types:
Data type
Description
bit
Allows 0, 1, or NULL
binary(n)
varbinary(n)
varbinary(max)
image
Storage
Number types:
Data type
Description
Storage
tinyint
1 byte
smallint
2 bytes
int
4 bytes
bigint
8 bytes
decimal(p,s)
5-17 bytes
5-17 bytes
4 bytes
money
8 bytes
float(n)
4 or 8
bytes
real
4 bytes
Data type
Description
Storage
datetime
8 bytes
datetime2
From January 1, 0001 and December 31, 9999 with an accuracy of 100
nanoseconds
6-8 bytes
smalldatetime
4 bytes
date
3 bytes
time
3-5 bytes
datetimeoffset
8-10 bytes
timestamp
Stores a unique number that gets updated every time a row gets created or
modified. The timestamp value is based upon an internal clock and does not
correspond to real time. Each table may have only one timestamp variable
Date types:
Description
sql_variant
Stores up to 8,000 bytes of data of various data types, except text, ntext, and
timestamp
uniqueidentifier
xml
cursor
table
SQL Quick Reference from W3Schools. Print it, and fold it in your pocket.
SQL Syntax
Statement
Syntax
AND / OR
SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
SELECT column_name
FROM table_name AS table_alias
BETWEEN
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
CREATE DATABASE
CREATE INDEX
CREATE TABLE
CREATE VIEW
DELETE FROM
DROP DATABASE
DROP INDEX
DROP TABLE
GROUP BY
SELECT column_name1,SUM(column_name2)
FROM table_name
GROUP BY column_name1
HAVING
SELECT column_name1,SUM(column_name2)
FROM table_name
GROUP BY column_name1
SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)
INSERT INTO
LIKE
SELECT column_name(s)
FROM table_name
WHERE column_name
LIKE pattern
ORDER BY
SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]
SELECT
SELECT column_name(s)
FROM table_name
SELECT *
SELECT *
FROM table_name
SELECT DISTINCT
SELECT INTO
(used to create backup copies of
tables)
SELECT *
INTO new_table_name
FROM original_table_name
or
SELECT column_name(s)
INTO new_table_name
FROM original_table_name
TRUNCATE TABLE
(deletes only the data inside the
table)
UPDATE
UPDATE table_name
SET column_name=new_value
[, column_name=new_value]
WHERE column_name=some_value
WHERE
SELECT column_name(s)
FROM table_name
WHERE condition
Source : http://www.w3schools.com/sql/sql_quickref.asp