0% found this document useful (0 votes)
30 views24 pages

Modulo 3 Pdf6

This document provides a summary of a training module on writing SELECT statements in T-SQL. The module covers writing simple SELECT statements, using the DISTINCT clause to eliminate duplicates, using column and table aliases, and writing simple CASE expressions. It includes lessons on each of these topics with examples and explains how to use the various clauses and functions in SELECT statements. Laboratories accompany each lesson to provide hands-on practice for the concepts.
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)
30 views24 pages

Modulo 3 Pdf6

This document provides a summary of a training module on writing SELECT statements in T-SQL. The module covers writing simple SELECT statements, using the DISTINCT clause to eliminate duplicates, using column and table aliases, and writing simple CASE expressions. It includes lessons on each of these topics with examples and explains how to use the various clauses and functions in SELECT statements. Laboratories accompany each lesson to provide hands-on practice for the concepts.
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/ 24

Modulo 3

Writing Select statements

Zeila Benavidez

Module Overview

Write Simple SELECT statements


Eliminate duplicates with DISTINCT
Using column and tables aliases

Writing Simple CASE expressions

Lesson 1: Introduction T-SQL


In this lesson you will learn :
Elements of the SELECT Statement
Retrieving Columns from a Table or View
Displaying Columns
Using Calculated Columns in the SELECT
Clause

Confidencial

Lesson 1- Elements of the SELECT


Statement
Main elements of SELECT statement
SELECT < select list>
FROM <table source>
WHERE <search condition>
GROUP BY <group by list>
ORDER BY <order by list>

Confidencial

Lesson1- Retrieving Columns from a Table


or View
Use SELECT with column list to display
columns
Use FROM to specify a source table or
view
Specify both schema and table names

Delimit names if necessary


End all statements with a semicolon
SELECT companyname, country
FROM Sales.Customers;
Confidencial

Lesson 1- Displaying Columns

Displaying all columns


SELECT *
FROM Sales.Customers ;
This is not the best practice in production
code.
Best Practice only specified columns.

Confidencial

Lesson 1- Using Calculations in the SELECT


Clause
You can use calculations in the SELECT
clause the calculations are scalar, returning
one value per row

Confidencial

Laboratory 1
In this lab, you will work with SELECT clause.

Confidencial

Lesson 2 Eliminating Duplicates with


DISTINCT
In this lesson you will learn:
SQL Sets and Duplicate Rows
Understanding DISTINCT
SELECT DISTINCT Syntax

Confidencial

Lesson 2- SQL Sets and Duplicate Output


Rows

SQL query results are not truly relational


Rows are not guaranteed to be unique, no
guaranteed order

Even unique rows in source table can


return duplicate values for some columns:
Select country

Country

From Sales.Customer

Argentina

Argentina
Austria
Austria

Confidencial

Lesson 2- Understanding DISTINCT

Specifies that only unique rows can appear in


the
result set
Removes duplicates based on column list
results, not source table
Provides uniqueness across set of selected
columns
Removes rows already operated on by
WHERE, HAVING, and GROUP BY clauses
Some queries may improve performance by
filtering out duplicates prior to execution of
SELECT clause
Confidencial

Lesson 2- SELECT DISTINCT Syntax

SELECT DISTINCT <column list>


FROM <table or view>
SELECT DISTINCT companyname, country
FROM Sales.Customers;

CompanyName

Country

Customer1

UK

Customer2

Mexico

Customer3

Germany

Customer3

France

Customer 4

Poland

Confidencial

Laboratory 2
In this lab, you will learn the use of the
DISTINCT clause.

Confidencial

Lesson 3 Using Column and Table


Aliases
In this lesson you will learn:
Using Aliases to Refer to Columns
Using Aliases to Refer to Tables
The Impact of Logical Processing Order on
Aliases

Confidencial

Lesson 3 Using Aliases to Refer to


Columns
COLUMN aliases using AS
SELECT orderid, unitprice, qty AS quantity
FROM

Sales.OrderDetails;

COLUMN aliases using =


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

Accidental column aliases


SELECT orderid, unitprice quantity
FROM Sales.OrderDetails;

Confidencial

Lesson 3 Using Aliases to Refer to


Tables
Create table aliases in the FROM clause
Table aliases with AS
SELECT custid, orderdate
FROM Sales.Orders AS SO;
Table aliases without AS
SELECT custid, orderdate
FROM Sales.Orders SO;
Using table aliases in the SELECT clause
SELECT SO.custid, SO.orderdate
FROM Sales.Orders AS SO;
Confidencial

Lesson 3 The Impact of Logical


Processing Order on Aliases
FROM, WHERE, and HAVING clauses
processed before SELECT
Aliases created in SELECT clause only visible
to ORDER BY
Expressions aliased in SELECT clause may be
repeated elsewhere in query

Confidencial

Laboratory 3
In this lab, you will work review the types of
aliases.

Confidencial

Lesson 4 Writing Simple CASE


Expressions
In this lesson you will learn:
Using CASE Expressions in SELECT Clauses
Forms of CASE Expressions

Confidencial

Lesson 4 Using CASE Expressions in


SELECT clauses
CASE expressions may be used in:
SELECT column list
WHERE or HAVING clauses
ORDER BY clause

CASE returns result of expression


Not a control-of-flow mechanism

In SELECT clause, CASE behaves as


calculated column requiring an alias

Confidencial

Lesson 4 Forms of CASE Expressions


Simple CASE
Compares one value to a list of possible values
Returns first match
If no match, returns value found in optional
ELSE clause
If no match and no ELSE, returns NULL

Searched CASE
Evaluates a set of predicates, or logical
expressions
Returns value found in THEN clause matching
first expression that evaluates to TRUE

Confidencial

Lesson 4 Writing Simple CASE


Expressions
SELECT productid, productname,categoryid,
CASE categoryid
WHEN 1 THEN 'Beverages'
ELSE 'Unknown Category'
END
FROM Production.Categories;

Confidencial

Laboratory 4
In this lab, you will work with CASE clauses

Confidencial

Resumen
In this module you learned the structure and
format of SELECT statement in order to add
functionality and readability to your queries.

Confidential

You might also like