Writing SELECT Queries: Module Overview
Writing SELECT Queries: Module Overview
Module 3
Writing SELECT Queries
Contents:
Module Overview 3-1
Module Overview
The SELECT statement is used to query tables and views. You may also perform some manipulation of the
data with SELECT before returning the results. It is likely that you will use the SELECT statement more than
any other single statement in T-SQL. This module introduces you to the fundamentals of the SELECT
statement, focusing on queries against a single table.
Objectives
After completing this module, you will be able to:
Lesson 1
Writing Simple SELECT Statements
In this lesson, you will learn the structure and format of the SELECT statement, as well as enhancements
that will add functionality and readability to your queries.
Lesson Objectives
After completing this lesson, you will be able to:
SELECT (Transact-SQL)
http://go.microsoft.com/fwlink/?LinkID=402716
Querying Microsoft® SQL Server® 3-3
End all statements with a semicolon (;) character. In SQL Server 2014, semicolons are an optional
terminator for most statements. However, future versions will require its use. For those current usages
when a semicolon is required, such as some common table expressions (CTEs) and some Service Broker
statements, the error messages returned for a missing semicolon are often cryptic. Therefore, you should
adopt the practice of terminating all statements with a semicolon.
Displaying Columns
To display columns in a query, you need to create a
comma-delimited column list. The order of the
columns in your list will determine their display in
the output, regardless of the order in which they
are defined in the source table. This gives your
queries the ability to absorb changes that others
may make to the structure of the table, such as
adding or reordering the columns.
T-SQL supports the use of the asterisk, or “star”
character (*) to substitute for an explicit column list.
This will retrieve all columns from the source table.
While suitable for a quick test, avoid using the * in
production work, as changes made to the table will cause the query to retrieve all current columns in the
table’s current defined order. This could cause bugs or other failures in reports or applications expecting a
known number of columns returned in a defined order.
By using an explicit column list in your SELECT clause, you will always get the desired results, as long as
the columns exist in the table. If a column is dropped, you will receive an error that will help identify the
problem and fix your query.
3-4 Writing SELECT Queries
Calculated Expression
SELECT unitprice, qty, (unitprice * qty)
FROM Sales.OrderDetails;
unitprice qty
--------------------- ------ ---------------------
14.00 12 168.00
9.80 10 98.00
34.80 5 174.00
18.60 9 167.40
Note that the new calculated column does not have a name returned with the results. To provide a name,
you use a column alias, which you will learn about later in this module.
To use a built-in T-SQL function on a column in the SELECT list, you pass the name of the column to the
function as an input:
Passing a Column
SELECT empid, lastname, hiredate, YEAR(hiredate)
FROM HR.Employees;
The results:
You will learn more about date functions, as well as others, later in this course. The use of YEAR in this
example is provided only to illustrate calculated columns.
Note: Not all calculations will be recalculated for each row. SQL Server may calculate a
function’s result once at the time of query execution, and reuse the value for each row. This will
be discussed later in the course.
Querying Microsoft® SQL Server® 3-5
Demonstration Steps
Use Simple SELECT Queries
1. Ensure that the 20461C-MIA-DC and 20461C-MIA-SQL virtual machines are both running, and then
log on to 20461C-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd.
3. Start SQL Server Management Studio and connect to the MIA-SQL database engine instance using
Windows authentication.
4. Open the Demo.ssmssln solution in the D:\Demofiles\Mod03\Demo folder.
5. If the Solution Explorer pane is not visible, on the View menu, click Solution Explorer.
7. Follow the instructions contained within the comments of the script file.
8. Keep SQL Server Management Studio open for the next demonstration.
3-6 Writing SELECT Queries
Lesson 2
Eliminating Duplicates with DISTINCT
T-SQL queries may display duplicate rows, even if the source table has a key column enforcing
uniqueness. Typically, this is the case when you retrieve only a few of the columns in a table. In this lesson,
you will learn how to eliminate duplicates using the DISTINCT clause.
Lesson Objectives
In this lesson, you will learn to:
• Understand how T-SQL query results are not true sets and may include duplicates.
• Understand how DISTINCT may be used to remove duplicate rows from the SELECT results.
For example, consider a query that retrieves country names from the Sales.Customers table:
Select Query
SELECT country
FROM Sales.Customers;
A partial result shows many duplicate country names, which at best is too long to be easy to interpret. At
worst, it gives a wrong answer to the question: “How many countries are represented among our
customers?”
country
-------
Germany
Mexico
Mexico
UK
Sweden
Germany
Germany
France
UK
Querying Microsoft® SQL Server® 3-7
Austria
Brazil
Spain
France
Sweden
...
Germany
France
Finland
Poland
(91 row(s) affected)
The reason for this output is that, by default, a SELECT clause contains a hidden default ALL statement:
ALL Statement
SELECT ALL country
FROM Sales.Customers;
In the absence of further instruction, the query will return one result for each row in the Sales.Customers
table, but since only the country column is specified, you will see that column alone for all 91 rows.
Understanding DISTINCT
Replacing the default SELECT ALL clause with
SELECT DISTINCT will filter out duplicates in the
result set. SELECT DISTINCT specifies that the result
set must contain only unique rows. However, it is
important to understand that the DISTINCT option
operates only on the set of columns returned by the
SELECT clause. It does not take into account any
other unique columns in the source table. DISTINCT
also operates on all the columns in the SELECT list,
not just the first one.
Continuing the previous example of countries from the Sales.Customers table, to eliminate the duplicate
values, replace the silent ALL default with DISTINCT:
DISTINCT Statement
SELECT DISTINCT country
FROM Sales.Customers;
This will yield the desired results. Note that, while the results appear to be sorted, this is not guaranteed
by SQL Server. The result set now contains only one instance of each unique output row:
Country
---------
Argentina
Austria
Belgium
Brazil
Canada
Denmark
3-8 Writing SELECT Queries
Finland
France
Germany
Ireland
Italy
Mexico
Norway
Poland
Portugal
Spain
Sweden
Switzerland
UK
USA
Venezuela
(21 row(s) affected)
Note: You will learn additional methods for filtering out duplicate values later in this
course. Once you have learned them, you may wish to consider the relative performance costs of
filtering with SELECT DISTINCT versus those other means.
SELECT Statement
SELECT firstname, lastname
FROM Sales.Customers;
The results:
firstname lastname
---------- --------------------
Sara Davis
Don Funk
Sara Lew
Don Davis
Judy Lew
Judy Funk
Yael Peled
However, a SELECT DISTINCT query against both columns will retrieve all unique combinations of the two
which, in this case, is the same seven employees.
For a list of unique first names only, execute a SELECT DISTINCT only against the firstname column:
DISTINCT Syntax
SELECT DISTINCT firstname
Querying Microsoft® SQL Server® 3-9
FROM Sales.Customers;
The results:
firstname
----------
Don
Judy
Sara
Yael
(4 row(s) affected)
A challenge in designing such queries is that, while you may need to retrieve a distinct list of values from
one column, you might want to see additional attributes (columns) from others. Later in this course, you
will see how to combine DISTINCT with the GROUP BY clause as a way of further processing and
displaying information about distinct lists of values.
Demonstration Steps
Eliminate Duplicate Rows
1. Ensure that you have completed the previous demonstration in this module. Alternatively, start the
20461C-MIA-DC and 20461C-MIA-SQL virtual machines, log on to 20461C-MIA-SQL as
ADVENTUREWORKS\Student with the password Pa$$w0rd, and run
D:\Demofiles\Mod03\Setup.cmd as an administrator.
2. If SQL Server Management Studio is not already open, start it and connect to the MIA-SQL database
engine instance using Windows authentication, and then open the Demo.ssmssln solution in the
D:\Demofiles\Mod03\Demo folder.
4. Follow the instructions contained within the comments of the script file.
5. Keep SQL Server Management Studio open for the next demonstration.
3-10 Writing SELECT Queries
Lesson 3
Using Column and Table Aliases
When retrieving data from a table or view, a T-SQL query will name each column after its source. If
desired, columns may be relabeled by the use of aliases in the SELECT clause. However, columns created
with expressions will not be named automatically. Column aliases may be used to provide custom column
headers. At the table level, aliases may be used in the FROM clause to provide a convenient way of
referring to a table elsewhere in the query, enhancing readability.
Lesson Objectives
In this lesson you will learn how to:
One method is to use the AS keyword to separate the column or expression from the alias:
AS Keyword
SELECT orderid, unitprice, qty AS quantity
FROM Sales.OrderDetails;
Another method is to assign the alias before the column or expression, using the equals sign as the
separator:
Finally, you can simply assign the alias immediately following the column name, although this is not a
recommended method:
FROM Sales.OrderDetails;
While there is no difference in performance or execution, a variance in readability may cause you to
choose one or the other as a convention.
Warning: Column aliases can also be accidentally created, by omitting a comma between two column
names in the SELECT list.
For example, the following creates an alias for the unitprice column deceptively labeled quantity:
Accidental Alias
SELECT orderid, unitprice quantity
FROM Sales.OrderDetails;
The results:
orderid quantity
----------- ---------------------
10248 14.00
10248 9.80
10248 34.80
10249 18.60
As you can see, this could be difficult to identify and fix in a client application. The only way to avoid this
problem is to carefully list your columns, separating them properly with commas and adopting the AS
style of aliases to make it easier to spot mistakes.
You may omit the keyword AS and simply follow the table name with the alias:
3-12 Writing SELECT Queries
To combine table and column aliases in the same SELECT statement, use the following approach:
Note: There is no table alias equivalent to the use of the equals sign (=) in a column alias.
Since this module focuses on single-table queries, you might not yet see a benefit to using table aliases. In
the next module, you will learn how to retrieve data from multiple tables in a single SELECT statement. In
those queries, the use of table aliases to represent table names will become quite useful.
However, the following example will return an error, as the WHERE clause has been processed before the
SELECT clause defines the alias:
As a result, you will often need to repeat an expression more than once—in the SELECT clause, where you
may create an alias to name the column, and in the WHERE or HAVING clause:
Querying Microsoft® SQL Server® 3-13
Additionally, within the SELECT clause, you may not refer to a column alias that was defined in the same
SELECT statement, regardless of column order.
Demonstration Steps
Use Column and Table Aliases
1. Ensure that you have completed the previous demonstration in this module. Alternatively, start the
20461C-MIA-DC and 20461C-MIA-SQL virtual machines, log on to 20461C-MIA-SQL as
ADVENTUREWORKS\Student with the password Pa$$w0rd, and run
D:\Demofiles\Mod03\Setup.cmd as an administrator.
2. If SQL Server Management Studio is not already open, start it and connect to the MIA-SQL database
engine instance using Windows authentication, and then open the Demo.ssmssln solution in the
D:\Demofiles\Mod03\Demo folder.
4. Follow the instructions contained within the comments of the script file.
5. Keep SQL Server Management Studio open for the next demonstration.
3-14 Writing SELECT Queries
Lesson 4
Writing Simple CASE Expressions
A CASE expression extends the ability of a SELECT clause to manipulate data as it is retrieved. Often when
writing a query, you need to substitute a value from a column of a table with another value. While you
will learn how to perform this kind of lookup from another table later in this course, you can also perform
basic substitutions using simple CASE expressions in the SELECT clause. In real-world environments, CASE
is often used to help make cryptic data held in a column more meaningful.
A CASE expression returns a scalar (single-valued) value based on conditional logic, often with multiple
conditions. As a scalar value, it may be used wherever single values can be used. Besides the SELECT
statement, CASE expressions can be used in WHERE, HAVING, and ORDER BY clauses.
Lesson Objectives
In this lesson you will learn how to:
• Understand the use of CASE expressions in SELECT clauses.
For example, the following CASE expression substitutes a descriptive category name for the categoryid
value stored in the Production.Categories table. Note that this is not a JOIN operation, just a simple
substitution using a single table:
CASE Expression
SELECT productid, productname, categoryid,
CASE categoryid
WHEN 1 THEN 'Beverages'
WHEN 2 THEN 'Condiments'
WHEN 2 THEN 'Confections'
ELSE 'Unknown Category'
END AS categoryname
FROM Production.Categories
The results:
Note: The preceding example is presented for illustration only and will not run against the
sample databases provided with the course.
Searched (Boolean) CASE expressions compare an input value to a set of logical predicates or expressions.
The expression can contain a range of values to match against. Like a simple CASE expression, the return
value is found in the THEN clause of the matching value.
Due to their dependence on predicate expressions, which will not be covered until later in this course,
further discussion of searched CASE expressions is beyond the scope of this lesson. See CASE (Transact-
SQL) in Books Online:
CASE (Transact-SQL)
http://go.microsoft.com/fwlink/?LinkID=402717
3-16 Writing SELECT Queries
Demonstration Steps
Use a Simple CASE Expression
1. Ensure that you have completed the previous demonstration in this module. Alternatively, start the
20461C-MIA-DC and 20461C-MIA-SQL virtual machines, log on to 20461C-MIA-SQL as
ADVENTUREWORKS\Student with the password Pa$$w0rd, and run
D:\Demofiles\Mod03\Setup.cmd as an administrator.
2. If SQL Server Management Studio is not already open, start it and connect to the MIA-SQL database
engine instance using Windows authentication, and then open the Demo.ssmssln solution in the
D:\Demofiles\Mod03\Demo folder.
Objectives
After completing this lab, you will be able to:
Password: Pa$$w0rd
Task 2: View all the Tables in the TSQL Database in Object Explorer
1. Using SSMS, connect to MIA-SQL using Windows authentication (if you are connecting to an on-
premises instance of SQL Server) or SQL Server authentication.
2. In Object Explorer, expand the TSQL database and expand the Tables folder.
2. Write a SELECT statement that will return all rows and all columns from the Sales.Customers table.
Note: You can use drag-and-drop functionality to drag items like table and column names
from Object Explorer to the query window. Write the same SELECT statement using the drag-
and-drop functionality.
3. Execute the written statement and compare the results that you achieved with the desired results
shown in the file D:\Labfiles\Lab03\Solution\52 - Lab Exercise 1 - Task 2 Result.txt.
3. Execute the written statement and compare the results that you achieved with the desired results
shown in the file D:\Labfiles\Lab03\Solution\53 - Lab Exercise 1 - Task 3 Result.txt.
4. What is the number of rows affected by the last query? (Tip: Because you are issuing a SELECT
statement against the whole table, the number of rows will be the same as that for the whole
Sales.Customers table).
Results: After this exercise, you should know how to create simple SELECT statements to analyze existing
tables.
2. Write a SELECT statement against the Sales.Customers table showing only the country column.
3. Execute the written statement and compare the results that you achieved with the desired results
shown in the file D:\Labfiles\Lab03\Solution\62 - Lab Exercise 2 - Task 1 Result.txt.
2. Execute the written statement and compare the results that you achieved with the desired results
shown in file D:\Labfiles\Lab03\Solution\63 - Lab Exercise 2 - Task 2 Result.txt.
5. Under which circumstances do the following queries against the Sales.Customers table return the
same result?
6. Is the DISTINCT clause being applied to all columns specified in the query or just the first column?
Results: After this exercise, you should have an understanding of how to return only the different
(distinct) rows in the result set of a query.
3. Write a SELECT Statement that Uses a Table Alias and a Column Alias
2. Write a SELECT statement to return the contactname and contacttitle columns from the
Sales.Customers table, assigning “C” as the table alias. Use the table alias C to prefix the names of the
two needed columns in the SELECT list. The benefit of using table aliases will become clearer in future
modules when topics such as joins and subqueries will be introduced.
3. Execute the written statement and compare the results that you achieved with the recommended
results shown in the file D:\Labfiles\Lab03\Solution\72 - Lab Exercise 3 - Task 1 Result.txt.
2. Execute the written statement and compare the results that you achieved with the desired results
shown in the file D:\Labfiles\Lab03\Solution\73 - Lab Exercise 3 - Task 2 Result.txt. Notice specifically
the titles of the columns in the desired output.
3-20 Writing SELECT Queries
Task 3: Write a SELECT Statement that Uses a Table Alias and a Column Alias
1. Write a query to display the productname column from the Production.Products table using “P” as
the table alias and Product Name as the column alias.
2. Execute the written statement and compare the results that you achieved with the desired results
shown in the file D:\Labfiles\Lab03\Solution\74 - Lab Exercise 3 - Task 3 Result.txt.
2. Execute the query exactly as written inside a query window and observe the result.
3. Correct the query to return the city and country columns from the Sales.Customers table.
Why did the query return only one column? What was the title of the column in the output? What is the
best practice when using aliases for columns to avoid such errors?
Results: After this exercise, you will know how to use aliases for table and column names.
categoryid Categoryname
1 Beverages
2 Condiments
3 Confections
4 Dairy Products
5 Grains/Cereals
6 Meat/Poultry
7 Produce
8 Seafood
They have an active marketing campaign, and would like to include product category information in their
reports.
3. Write a SELECT Statement that Uses a CASE Expression to differentiate Campaign-Focused Products
2. Write a SELECT statement to display the categoryid and productname columns from the
Production.Products table.
3. Execute the written statement and compare the results that you achieved with the desired results
shown in the file D:\Labfiles\Lab03\Solution\82 - Lab Exercise 4 - Task 1 Result.txt.
2. Execute the written statement and compare the results that you achieved with the desired output
shown in the file D:\Labfiles\Lab03\Solution\83 - Lab Exercise 4 - Task 2 Result.txt.
Results: After this exercise, you should know how to use CASE expressions to write simple conditional
logic.
3-22 Writing SELECT Queries
Review Question(s)
Question: Why is the use of SELECT * not a recommended practice?
Question: What will happen if you omit a comma between column names in a SELECT
clause?