Learn how to write (build) simple SQL query statements in SQL Server 2005 using the SELECT statement.
1
Displaying or Selecting Columns from a Table
Using a SELECT statement, we do not have to display or return all the columns from a table.
SELECT Name --呈现表中名字一列的值
FROM Dependent
SELECT * --呈现所有的列项数据
FROM Dependent
2
ORDER BY
we can use the OROER BY clause in the SELECT satement to display the contents
of a table in a predictate manner.
for example
SELECT dname, ageFROM DependentORDER BY age --以年龄的大小来呈现所输出的数据If the field that you choose to ORDER BY contains nulls, the fields thathave null values assigned to them are placed at ((the top of)) the displayedlist of output. This is because of the way SQL Server stores null values internally. Look at the output of the following query
2.1
Ordering Within an Order
There will be times when you will want to sort groups within
an order by another order. SQL Server syntax allows you to do this.
For example, using the Dependent table, if you want to order all
the dependents by sex, and within sex you want to order by age in
descending order, would you type the following query:
for example
SELECT dname, sex, age
FROM Dependent
ORDER BY sex, age DESC
if you want to order by descending order of sex, and descending order of age
as follows;
SELECT dname, sex, age
FROM Dependent
ORDER BY sex DESC, age DESC
3
WHERE
The WHERE clause is a row filter that is used to restrict the
output of rows (or tuples )in a result set. When the WHERE clause
is used, the SQL Server database engine selects the rows from the
table for the result set that meet the conditions listed in the
WHERE clause.
for example
SELECT dname, ageFROM DependentWHERE age <= 5ORDER BY ageat lase or and between operator is simple,don't give some examples.4 COUNTThe COUNT function is used to return a count of the number of rowsfor exampleSELECT COUNT(*)FROM Dependent5 ROWCOUNTlimiting the number of rows that can be the returned by a SELECT statement
for example
SET ROWCOUNT 10 --输出前10行数据
SELECT *
FROM Dependent
After using ROWCOUNT, you should reset the ROWCOUNT property by:
SET ROWCOUNT 0
6 注意2。9节内容