MS SQL Basic commands

SELECT … FROM
Most basic SELECT statement is only consisting of 2 parts: (1) what columns & (2) what table(s) those columns should be picked up from.
Example:

[code language="sql"]
SELECT * FROM testTable
[/code]

Usually we are not interested in everything (* stands for all columns) & would only like specific columns:
[code language="sql"]
SELECT testColumn1, testColumn2, testColumn3 FROM testTable
[/code]

WHERE
WHERE statement is used to filter the data and narrow the amount of data we receive. For example in the following example only data are picked that are “Berlin” in their city Column.

[code language="sql"]

[/code]

SELECT nameColumn, IDcolumn,city FROM testTable
WHERE city = ‘Berlin’
It’s important to know that the condition could be expanded by using AND / OR statements. Look at the following extended WHERE examples.

[code language="sql"]
WHERE city <> 'Berlin'
WHERE Date <= '1-july-1993'
WHERE Date BETWEEN '1-june-1992' AND '15-december-1993'
[/code]

LIKE operator allows us to perform basic pattern-matching using wildcard characters. For Microsoft SQL Server, the wildcard characters are defined as follows:

% – matches a string of one or more characters
[ ] – matches any single character within the specified range (e.g. [a-f]) or set (e.g. [abcdef]).
[^] – matches any single character not within the specified range (e.g. [^a-f]) or set (e.g. [^abcdef]).

[code language="sql"]
WHERE FirstName LIKE '_im' finds all three-letter first names that end with 'im' (e.g. Jim, Tim).
WHERE LastName LIKE '%stein' - finds all employees whose last name ends with 'stein'
WHERE LastName LIKE '%stein%' - finds all employees whose last name includes 'stein' anywhere in the name.
WHERE FirstName LIKE '[JT]im' - finds three-letter first names that end with 'im' and begin with either 'J' or 'T' (that is, only Jim and Tim)
WHERE LastName LIKE 'm[^c]%' - finds all last names beginning with 'm' where the following (second) letter is not 'c'.
WHERE (FirstName NOT LIKE 'M%') AND (FirstName NOT LIKE 'A%')
[/code]

ORDER BY
Up to now we went through how to select values and limit the select by setting conditions. Now it’s time to sort the data. ORDER BY command simply sort the data based on the specified column. By default the sort is ascending, unless mentioned otherwise.

Example:

[code language="sql"]
SELECT nameColumn, IDcolumn,city FROM testTable
ORDER BY city
[/code]

Other examples:

[code language="sql"]
ORDER BY city DESC
ORDER BY Country, City DESC
ORDER BY Country ASC, City DESC
[/code]

Source : http://msdn.microsoft.com/en-us/library/bb264565(v=sql.90).aspx