0% found this document useful (0 votes)
139 views22 pages

Writing SELECT Queries: Module Overview

Uploaded by

Bobe Danut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views22 pages

Writing SELECT Queries: Module Overview

Uploaded by

Bobe Danut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

3-1

Module 3
Writing SELECT Queries
Contents:
Module Overview 3-1

Lesson 1: Writing Simple SELECT Statements 3-2

Lesson 2: Eliminating Duplicates with DISTINCT 3-6

Lesson 3: Using Column and Table Aliases 3-10

Lesson 4: Writing Simple CASE Expressions 3-14

Lab: Writing Basic SELECT Statements 3-17

Module Review and Takeaways 3-22

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:

• Write simple SELECT statements.


• Eliminate duplicates using the DISTINCT clause.

• Use column and table aliases.

• Write simple CASE expressions.


3-2 Writing SELECT Queries

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:

• Understand the elements of the SELECT statement.

• Write simple single-table SELECT queries.

• Eliminate duplicate rows using the DISTINCT clause.


• Add calculated columns to a SELECT clause.

Elements of the SELECT Statement


The SELECT and FROM clauses are the primary
focus of this module. You will learn more about the
other clauses in later modules of this course.
However, your understanding of the order of
operations in logical query processing, from earlier
in the course, will remain important for how you
understand the proper way to write SELECT queries.

Remember that the FROM, WHERE, GROUP BY, and


HAVING clauses will have been evaluated before
the contents of the SELECT clause are processed.
Therefore, elements you write in the SELECT clause,
especially calculated columns and aliases, will not
be visible to the other clauses.

Additional information on SELECT elements can be found at:

SELECT (Transact-SQL)
http://go.microsoft.com/fwlink/?LinkID=402716
Querying Microsoft® SQL Server® 3-3

Retrieving Columns from a Table or View


The SELECT clause specifies the columns from the
source table(s) or view(s) that you want to return as
the result set of the query. In addition to columns
from the source table, you may add others in the
form of calculated expressions.

The FROM clause specifies the name of the table or


view that is the source of the columns in the SELECT
clause. To avoid errors in name resolution, it is a
best practice to specify both the schema and object
name of the table, in the form SCHEMA.OBJECT,
such as Sales.Customers.

If the table or view name contains irregular


characters, such as spaces or other special characters, you will need to delimit, or enclose, the name. T-
SQL supports the use of the ANSI standard double quotes "Sales Order Details" and the SQL Server-
specific square brackets [Sales Order Details].

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

Using Calculations in the SELECT Clause


In addition to retrieving columns stored in the
source table, a SELECT statement can perform
calculations and manipulations. Calculations can
manipulate the source column data and use built-in
T-SQL functions, which you will learn about later in
this course.

Since the results will appear in a new column,


repeated once per row of the result set, calculated
expressions in a SELECT clause must be scalar. In
other words, they must return only a single value.

Calculated expressions may operate on other


columns in the same row, on built-in functions, or a
combination of the two:

Calculated Expression
SELECT unitprice, qty, (unitprice * qty)
FROM Sales.OrderDetails;

The results appear as follows:

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:

empid lastname hiredate


----------- -------------------- ----------------------- -------
1 Davis 2002-05-01 00:00:00.000 2002
2 Funk 2002-08-14 00:00:00.000 2002
3 Lew 2002-04-01 00:00:00.000 2002

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: Writing Simple SELECT Statements


In this demonstration, you will see how to:

• Use simple SELECT queries

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.

2. Run D:\Demofiles\Mod03\Setup.cmd as an administrator.

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.

6. Open the 11 – Demonstration A.sql script file.

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.

• Write SELECT DISTINCT clauses.

SQL Sets and Duplicate Rows


While the theory of relational databases calls for
unique rows in a table, in practice T-SQL query
results are not true sets. The rows retrieved by a
query are not guaranteed to be unique, even when
they come from a source table that uses a primary
key to differentiate each row. Nor are the rows
guaranteed to be returned in any particular order.
You will learn how to address this with ORDER BY
later in this course.
Add to this the fact that the default behavior of a
SELECT statement is to include the keyword ALL,
and you can begin to see why duplicate values
might be returned by a query, especially when you include only some of the columns in a table (and omit
the unique columns).

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.

The logical order of operations also ensures that the


DISTINCT operator will remove rows that may have
already been processed by WHERE, HAVING, and GROUP BY clauses.

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 DISTINCT Syntax


Remember that DISTINCT looks at rows in the
output set, created by the SELECT clause. Therefore,
only unique combinations of column values will be
returned by a SELECT DISTINCT clause.

For example, if you query a table with the following


data in it, you might observe that there are only
four unique first names and four unique last names:

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: Eliminating Duplicates with DISTINCT


In this demonstration, you will see how to:
• Eliminate duplicate rows.

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.

3. In Solution Explorer, open the 21 – Demonstration B.sql script file.

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:

• Use aliases to refer to columns in a SELECT list.

• Use aliases to refer to columns in a FROM clause.

• Understand the impact of the logical order of query processing on aliases.

Using Aliases to Refer to Columns


Column aliases can be used to relabel columns
when returning the results of a query. For example,
cryptic names of columns in a table such as qty may
be replaced with quantity.
Expressions that are not based on a source column
in the table will not have a name provided in the
result set. This includes calculated expressions and
function calls. While T-SQL doesn’t require that a
column in a result set have a name, it’s a good idea
to provide one.
In T-SQL, there are multiple methods of creating a
column alias, with identical output results.

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:

Alias With an Equals Sign


SELECT orderid, unitprice, quantity = qty
FROM Sales.OrderDetails;

Finally, you can simply assign the alias immediately following the column name, although this is not a
recommended method:

Alias Following Column Name


SELECT orderid, unitprice, qty quantity
Querying Microsoft® SQL Server® 3-11

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.

Question: Which style of column aliases do you prefer? Why?

Using Aliases to Refer to Tables


Aliases may also be used in the FROM clause to
refer to a table, which can improve readability and
save redundancy when referencing the table
elsewhere in the query. While this module has
focused on single-table queries, which don’t
necessarily benefit from table aliases, this technique
will prove useful as you learn more complex queries
in subsequent modules.

To create a table alias in a FROM clause, you will


use syntax similar to several of the column alias
techniques.

You may use the keyword AS to separate the table


name from the alias. This style is preferred:

Table Alias Using AS


SELECT orderid, unitprice, qty
FROM Sales.OrderDetails AS OD;

You may omit the keyword AS and simply follow the table name with the alias:
3-12 Writing SELECT Queries

Table Alias Without AS


SELECT orderid, unitprice, qty
FROM Sales.OrderDetails OD;

To combine table and column aliases in the same SELECT statement, use the following approach:

Table and Column Aliases Combined


SELECT OD.orderid, OD.unitprice, OD.qty AS Quantity
FROM Sales.OrderDetails AS OD;

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.

The Impact of Logical Processing Order on Aliases


An issue may arise in the use of column aliases.
Aliases created in the SELECT clause may not be
referred to in others in the query, such as a WHERE
or HAVING clause. This is due to the logical order
query processing. The WHERE and HAVING clauses
are processed before the SELECT clause and its
aliases are evaluated. An exception to this is the
ORDER BY clause.
An example is provided here for illustration and will
run without error:

ORDER BY With Alias


SELECT orderid, unitprice, qty AS quantity
FROM Sales.OrderDetails
ORDER BY quantity;

However, the following example will return an error, as the WHERE clause has been processed before the
SELECT clause defines the alias:

Incorrect WHERE With Alias


SELECT orderid, unitprice, qty AS quantity
FROM Sales.OrderDetails
WHERE quantity > 10;

The resulting error message:

Msg 207, Level 16, State 1, Line 1


Invalid column name 'quantity'.

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

Correct WHERE With Alias


SELECT orderid, YEAR(orderdate) AS orderyear
FROM Sales.Orders
WHERE YEAR(orderdate) = '2008'

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.

The following statement will return an error:

Column Alias used in SELECT Clause


SELECT productid, unitprice AS price, price * qty AS total
FROM Sales.OrderDetails;

The resulting error:

Msg 207, Level 16, State 1, Line 1


Invalid column name 'price'.

Demonstration: Using Column and Table Aliases


In this demonstration, you will see how to:

• Use column and table aliases.

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.

3. In Solution Explorer, open the 31 – Demonstration C.sql script file.

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.

• Understand the simple form of a CASE expression.

Using CASE Expressions in SELECT Clauses


In T-SQL, CASE expressions return a single, or scalar,
value. Unlike some other programming languages,
in T-SQL CASE, expressions are not statements, nor
do they specify the control of programmatic flow.
Instead, they are used in SELECT clauses (and other
clauses) to return the result of an expression. The
results appear as a calculated column and should
be aliased for clarity.
In T-SQL queries, CASE expressions are often used
to provide an alternative value for one stored in the
source table. For example, a CASE expression might
be used to provide a friendly text name for
something stored as a compact numeric code.
Querying Microsoft® SQL Server® 3-15

Forms of CASE Expressions


In T-SQL, CASE expressions may take one of two
forms – simple CASE or searched (Boolean) CASE.

Simple CASE expressions, the subject of this lesson,


compare an input value to a list of possible
matching values:

1. If a match is found, the first matching value is


returned as the result of the CASE expression.
Multiple matches are not permitted.

2. If no match is found, a CASE expression returns


the value found in an ELSE clause, if one exists.

3. If no match is found and no ELSE clause is


present, the CASE expression returns a NULL.

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:

productid productname categoryid categoryname


--------- ------------ ---------- ---------------------
101 Tea 1 Beverages
102 Mustard 2 Condiments
103 Dinner Rolls 9 Unknown Category

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: Using a Simple CASE Expression


In this demonstration, you will see how to:

• Use a simple CASE expression.

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.

3. In Solution Explorer, open the 41 – Demonstration D.sql script file.


4. Follow the instructions contained within the comments of the script file.

5. Close SQL Server Management Studio without saving any files.


Querying Microsoft® SQL Server® 3-17

Lab: Writing Basic SELECT Statements


Scenario
You are a business analyst for Adventure Works who will be writing reports using corporate databases
stored in SQL Server 2014. You have been provided with a set of business requirements for data and will
write basic T-SQL queries to retrieve the specified data from the databases.

Objectives
After completing this lab, you will be able to:

• Write simple SELECT statements.

• Eliminate duplicate rows by using the DISTINCT keyword.

• Use table and column aliases.


• Use a simple CASE expression.

Estimated Time: 40 minutes

Virtual machine: 20461C-MIA-SQL


User name: ADVENTUREWORKS\Student

Password: Pa$$w0rd

Exercise 1: Writing Simple SELECT Statements


Scenario
As a business analyst, you want a better understanding of your corporate data. Usually the best approach
for an initial project is to get an overview of the main tables and columns involved so you can better
understand different business requirements. After an initial overview, you will have to provide a report for
the marketing department because staff there would like to send invitation letters for a new campaign.
You will use the TSQL sample database.

The main tasks for this exercise are as follows:

1. Prepare the Lab Environment

2. View all the Tables in the TSQL Database in Object Explorer

3. Write a Simple SELECT Statement

4. Write a SELECT Statement that Includes Specific Columns

 Task 1: Prepare the Lab Environment


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.

2. Run Setup.cmd in the D:\Labfiles\Lab03\Starter folder as Administrator.

 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.

3. Take a look at the names of the tables in the Sales schema.


3-18 Writing SELECT Queries

 Task 3: Write a Simple SELECT Statement


1. Open the project file D:\Labfiles\Lab03\Starter\Project\Project.ssmssln and the T-SQL script 51 - Lab
Exercise 1.sql. Ensure that you are connected to the TSQL database.

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.

 Task 4: Write a SELECT Statement that Includes Specific Columns


1. Expand the Sales.Customers table in Object Explorer and expand the Columns folder. Observe all
columns in the table.
2. Write a SELECT statement to return the contactname, address, postalcode, city, and country columns
from the Sales.Customers 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\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.

Exercise 2: Eliminating Duplicates Using DISTINCT


Scenario
After supplying the marketing department with a list of all customers for a new campaign, you are asked
to provide a list of all the different countries that the customers come from.

The main tasks for this exercise are as follows:


1. Write a SELECT Statement that Includes a Specific Column

2. Write a SELECT Statement that Uses the DISTINCT Clause

 Task 1: Write a SELECT Statement that Includes a Specific Column


1. Open the project file D:\Labfiles\Lab03\Starter\Project\Project.ssmssln and T-SQL script 61 - Lab
Exercise 2.sql. Ensure that you are connected to the TSQL database.

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.

 Task 2: Write a SELECT Statement that Uses the DISTINCT Clause


1. Copy the SELECT statement in Task 1 and modify it to return only distinct values.
Querying Microsoft® SQL Server® 3-19

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.

3. How many rows did the query in Task 1 return?

4. How many rows did the query in Task 2 return?

5. Under which circumstances do the following queries against the Sales.Customers table return the
same result?

SELECT city, region FROM Sales.Customers;


SELECT DISTINCT city, region FROM Sales.Customers;

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.

Exercise 3: Using Table and Column Aliases


Scenario
After getting the initial list of customers, the marketing department would like to have more readable
titles for the columns and a list of all products in the TSQL database.

The main tasks for this exercise are as follows:

1. Write a SELECT Statement that Uses a Table Alias

2. Write A SELECT Statement That Uses Column Aliases

3. Write a SELECT Statement that Uses a Table Alias and a Column Alias

4. Analyze and Correct the Query

 Task 1: Write a SELECT Statement that Uses a Table Alias


1. Open the project file D:\Labfiles\Lab03\Starter\Project\Project.ssmssln and T-SQL script 71 - Lab
Exercise 3.sql. Ensure that you are connected to the TSQL database.

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.

 Task 2: Write A SELECT Statement That Uses Column Aliases


1. Write a SELECT statement to return the contactname, contacttitle, and companyname columns.
Assign these with the aliases Name, Title, and Company Name, respectively, in order to return more
human-friendly column titles for reporting purposes.

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.

 Task 4: Analyze and Correct the Query


1. A developer has written a query to retrieve two columns (city and region) from the Sales.Customers
table. When the query is executed, it returns only one column. Your task is to analyze the query,
correct it to return two columns, and explain why the query returned only one.

SELECT city country


FROM Sales.Customers;

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.

Exercise 4: Using a Simple CASE Expression


Scenario
Your company has a long list of products, and the members of the marketing department would like to
have product category information in their reports. They have supplied you with a document containing
the following mapping between the product category IDs and their 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.

The main tasks for this exercise are as follows:

1. Write a SELECT Statement


Querying Microsoft® SQL Server® 3-21

2. Write a SELECT Statement that Uses a CASE Expression

3. Write a SELECT Statement that Uses a CASE Expression to differentiate Campaign-Focused Products

 Task 1: Write a SELECT Statement


1. Open the project file D:\Labfiles\Lab03\Starter\Project\Project.ssmssln and T-SQL script 81 Lab
Exercise 4.sql. Ensure that you are connected to the TSQL database.

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.

 Task 2: Write a SELECT Statement that Uses a CASE Expression


1. Enhance the SELECT statement in task 1 by adding a CASE expression that generates a result column
named categoryname. The new column should hold the translation of the category ID to its
respective category name, based on the mapping table supplied earlier. Use the value “Other” for any
category IDs not found in the mapping table.

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.

 Task 3: Write a SELECT Statement that Uses a CASE Expression to differentiate


Campaign-Focused Products
1. Modify the SELECT statement in task 2 by adding a new column named iscampaign. This will show
the description “Campaign Products” for the categories Beverages, Produce, and Seafood and the
description “Non-Campaign Products” for all other categories.
2. Execute the written statement and compare the results that you achieved with the desired results
shown in the file D:\Labfiles\Lab03\Solution\84 - Lab Exercise 4 - Task 3 Result.txt.

Results: After this exercise, you should know how to use CASE expressions to write simple conditional
logic.
3-22 Writing SELECT Queries

Module Review and Takeaways


Best Practice: Terminate all T-SQL statements with a semicolon. This will make your code
more readable, avoid certain parsing errors, and protect your code against changes in future
versions of SQL Server.
Consider standardizing your code on the AS keyword for labeling column and table aliases. This
will make it easier to read and avoids accidental aliases.

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?

Question: What kind of result does a simple CASE statement return?

You might also like