Friday, November 07, 2014

TSQL Tutorial: Querying Data

The SELECT command is used to query tables and views for data. You specify what you want via a SELECT
statement, and the server "serves" it to you via a result set—a collection of rows containing the data you
requested. SELECT is the Swiss Army knife of basic SQL. 
It can join tables, retrieve data you request, assign local variables, and even create other tables. It's a fair guess that you'll use the SELECT statement more than any other single command in Transact-SQL.

We'll begin exploring SELECT by listing the contents of the tables you just built. Execute
SELECT * FROM tablename
in Query Analyzer, replacing tablename with the name of each of the three tables. You should find that the
CUSTOMER and items tables have three rows each, while orders has four.
SELECT * FROM customers
(Results abridged)
CustomerNumber LastName FirstName StreetAddress
-------------- -------- --------- -------------
1 Doe John 123 Joshua Tree
2 Doe Jane 123 Joshua Tree
3 Citizen John 57 Riverside
SELECT * FROM orders
OrderNumber OrderDate CustomerNumber ItemNumber Amount
----------- ----------------------- -------------- ---------- --------
101 1990-10-18 00:00:00.000 1 1001 123.45
102 1992-02-27 00:00:00.000 2 1002 678.90
103 1995-05-20 00:00:00.000 3 1003 86753.09
104 1997-11-21 00:00:00.000 1 1002 678.90
SELECT * FROM items
ItemNumber Description Price
---------- ----------- --------
1001 WIDGET A 123.45
1002 WIDGET B 678.90

1003 WIDGET C 86753.09

TSQL Tutorial..

No comments:

Post a Comment