Transact SQL
Also
This SQL is used to execute queries on
databases.
Join Type |
Description |
Syntax |
Inner Join |
Joins the columns of one table to the columns of another table only for
rows that meet the specified condition |
Select [Colums] from [Table1]
INNER JOIN [Table2]
ON [Join
Condition] |
Left Outer Join |
Includes all rows from the left table and joins table2 column where the
condition is meet, or returns NULLS in place of the unmatched column if the
condiditon isn't met |
Select [Colums] from [Table1]
LEFT OUTER JOIN [Table2]
ON [Join
Condition]
The 'outer' key word can be ommited |
Right Outer Join |
All the rows from the right hand table are included, with nulls if the
condition isn't meet |
Select [Colums] from [Table1]
RIGHT OUTER JOIN [Table2]
ON [Join
Condition]
The 'outer' key word can be ommited |
Full Outer Join |
Every Rows from both tables will have a corresponding row in the
results. Where the condition is not met, the unmatched rows will be
NULL |
Select [Colums] from [Table1]
RIGHT OUTER JOIN [Table2]
ON [Join
Condition]
The 'outer' key word can be ommited |
Cross Join |
Every row of the left table will be combined with every row of the right
table. No join condition is required however the results can be filtered
using the select's where clause to reduce the row count. |
Select [Colums] from [Table1]
CROSS JOIN [Table2]
-or-
Select [Columns] From
Table1,Table2 |
INSERT statements are used to add rows
to a table. Identity fields in the inserted record and NOT returned
(which is just stupid as all hell) but can be retrieved by using SELECT
@@IDENTITY; or SELECT SCOPE_IDENTITY() in 2005. Watch out for
automatic triggers as they may cause the wrong identity to be returned. See
Inserting records in ASP with
ADO
-
The following form inserts a single
row:
INSERT INTO tablename
(insertcol1,insertcol2) VALUES
(value1,value2)
-
The following form inserts multiple rows from
another table:
INSERT INTO table1
(insertcol1,insertcol2) SELECT
col1,col2 FROM table2
DELETE is used to remove rows from a
table
DELETE FROM table WHERE
deletefilter
The UPDATE statement is used to change
rows already entered into a table
UPDATE table SET
column = expression,column =
expression WHERE updatefilter
The update filter can specifiy a single row
(e.g where id=10034) or multiple rows (e.g. where id > 10000) to be
updated
Search Condition (T-SQL)
Is a combination of one or more predicates using the logical operators AND,
OR, and NOT.
Syntax
<search_condition> ::=
{ [ NOT ] <predicate>
| ( <search_condition> ) }
[ {AND | OR} [NOT]
{<predicate> | ( <search_condition> ) } ]
} [,...n]
<predicate> ::=
{
expression { = |
<> | != | > | >= | !> | < | <= | !< }
expression
| string_expression
[NOT] LIKE string_expression
[ESCAPE
'escape_character']
| expression [NOT]
BETWEEN expression AND expression
| expression IS [NOT]
NULL
| CONTAINS
( {column
| * }, '<contains_search_condition>' )
| FREETEXT (
{column | * }, 'freetext_string' )
| expression [NOT]
IN (subquery | expression
[,...n])
| expression { = |
<> | != | > | >= | !> | < | <= | !< }
{ALL
| SOME | ANY} (subquery)
| EXISTS
(subquery)
}
Arguments
-
<search_condition>
-
Specifies the conditions for the rows returned in the result set for a SELECT
statement, query expression, or subquery. For an UPDATE statement, specifies
the rows to be updated. For a DELETE statement, specifies the rows to be
deleted. There is no limit to the number of predicates that can be included
in a Transact-SQL statement search condition.
-
NOT
-
Negates the Boolean expression specified by the predicate. For more information,
see NOT.
-
AND
-
Combines two conditions and evaluates to TRUE when both of the conditions
are TRUE. For more information, see AND.
-
OR
-
Combines two conditions and evaluates to TRUE when either condition is TRUE.
For more information, see OR.
-
<predicate>
-
Is an expression that returns TRUE, FALSE, or UNKNOWN.
-
expression
-
Is a column name, a constant, a function, a variable, a scalar subquery,
or any combination of column names, constants, and functions connected by
an operator(s) or a subquery. The expression can also contain the CASE function.
-
=
-
Is the operator used to test the equality between two expressions.
-
<>
-
Is the operator used to test the condition of two expressions not being equal
to each other.
-
!=
-
Is the operator used to test the condition of two expressions not being equal
to each other.
-
>
-
Is the operator used to test the condition of one expression being greater
than the other.
-
>=
-
Is the operator used to test the condition of one expression being greater
than or equal to the other expression.
-
!>
-
Is the operator used to test the condition of one expression not being greater
than the other expression.
-
<
-
Is the operator used to test the condition of one expression being less than
the other.
-
<=
-
Is the operator used to test the condition of one expression being less than
or equal to the other expression.
-
!<
-
Is the operator used to test the condition of one expression not being less
than the other expression.
-
string_expression
-
Is a string of characters and wildcard characters.
-
[NOT] LIKE
-
Indicates that the subsequent character string is to be used with pattern
matching. For more information, see LIKE.
-
ESCAPE 'escape_character'
-
Allows a wildcard character to be searched for in a character string instead
of functioning as a wildcard. escape_character is the character that
is placed in front of the wildcard character to denote this special use.
-
[NOT] BETWEEN
-
Specifies an inclusive range of values. Use AND to separate the beginning
and ending values. For more information, see BETWEEN.
-
IS [NOT] NULL
-
Specifies a search for null values, or for values that are not null, depending
on the keywords used. An expression with a bitwise or arithmetic operator
evaluates to NULL if any of the operands is NULL.
-
CONTAINS
-
Searches columns containing character-based data for precise or
fuzzy (less precise) matches to single words and phrases, the
proximity of words within a certain distance of one another, and weighted
matches. Can only be used with SELECT statements. For more information, see
CONTAINS.
-
FREETEXT
-
Provides a simple form of natural language query by searching columns containing
character-based data for values that match the meaning rather than the exact
words in the predicate. Can only be used with SELECT statements. For more
information, see FREETEXT.
-
[NOT] IN
-
Specifies the search for an expression, based on the expression's inclusion
in or exclusion from a list. The search expression can be a constant
or a column name, and the list can be a set of constants or, more commonly,
a subquery. Enclose the list of values in parentheses. For more information,
see IN.
-
subquery
-
Can be considered a restricted SELECT statement and is similar to
<query_expresssion> in the SELECT statement. The ORDER BY clause, the
COMPUTE clause, and the INTO keyword are not allowed. For more information
see SELECT.
-
ALL
-
Used with a comparison operator and a subquery. Returns TRUE for
<predicate> if all values retrieved for the subquery satisfy the comparison
operation, or FALSE if not all values satisfy the comparison or if the subquery
returns no rows to the outer statement. For more information, see
ALL.
-
{SOME | ANY}
-
Used with a comparison operator and a subquery. Returns TRUE for
<predicate> if any value retrieved for the subquery satisfies the
comparison operation, or FALSE if no values in the subquery satisfy the
comparison or if the subquery returns no rows to the outer statement. Otherwise,
the expression is unknown. For more information, see SOME
| ANY.
-
EXISTS
-
Used with a subquery to test for the existence of rows returned by the subquery.
For more information, see EXISTS.
Remarks
The order of precedence for the logical operators is NOT (highest), followed
by AND, followed by OR. The order of evaluation at the same precedence level
is from left to right. Parentheses can be used to override this order in
a search condition. For information about how the logical operators operate
on truth values, see AND,
OR, and NOT.
Examples
A. Use WHERE with LIKE and ESCAPE syntax
This example assumes a description column exists in finances
table. To search for the rows in which the description column contains
the exact characters g_, use the ESCAPE option because _ is a wildcard character.
Without specifying the ESCAPE option, the query would search for any description
values containing the letter g followed by any single character other than
the _ character.
SELECT *
FROM finances
WHERE description LIKE 'gs_' ESCAPE 'S'
GO
B. Use WHERE and LIKE syntax with Unicode data
This example uses the WHERE clause to retrieve the contact name, telephone,
and fax numbers for any companies containing the string snabbköp at
the end of the company name.
USE Northwind
SELECT CompanyName, ContactName, Phone, Fax
FROM Customers
WHERE CompanyName LIKE N'%snabbköp'
ORDER BY CompanyName ASC, ContactName ASC