SQLConfiguration SQLConfiguration


Select Data  «Prev  Next»
Lesson 2 Overview of selecting data
Objective Describe how to select data with Transact-SQL.

Overview of Selecting Data with Transact-SQL

To retrieve data from a table, you use the SELECT Transact-SQL statement. This module teaches you how to retrieve data from a single table. The following two lessons show you how to use joins to retrieve data from multiple tables.
You can specify the names of specific columns from which to request data, or you can request data from all columns in the table.
The SELECT statement uses the following general syntax:

Select Statement Syntax
For example, to select all data from the employees table for everyone with the last name Smith, you would specify that you want every column returned by using the * wildcard with the following Transact-SQL statement:

SELECT *
FROM employees
WHERE LastName = 'Smith'

Alternatively, you could achieve the same result by explicitly specifying every column name in the table, like this:
SELECT EmployeeID, LastName, FirstName, 
AddressLine1, AddressLine2, City, 
State, Zip, HomePhone, HomeFax  
FROM employees
WHERE LastName = 'Smith'

What if you only wanted to return only specific columns?
You explicitly list the columns that you wish to see. For example, if you want to return only the FirstName, and HomePhone for anyone with the last name of Smith, you would use this Transact-SQL statement:

SELECT FirstName, HomePhone  
FROM employees
WHERE LastName = 'Smith'

Interacting Through the Query Window

This part of the Management Studio takes the place of what was, at one time, a separate tool that was called Query Analyzer.
The Query window is your tool for interactive sessions with a given SQL Server. It is where you can execute statements using Transact-SQL (T-SQL). I pronounce it Tee-Squeal, but it is supposed to be Tee-Sequel. T-SQL is the native language of SQL Server and its a dialect of Structured Query Language (SQL), and is largely compliant withmodern ANSI/ISO SQL standards. You will find that most RDBMS products support basic ANSI/ISO SQL compatibility. Because the Query window is where you will spend a fair amount of time, let us take a more in-depth look at this tool and get familiar with how to use it.
In the next lesson, you will learn how joins are used to retrieve data from multiple tables.