Professional Documents
Culture Documents
• Filtering Data
SELECT select_list
[INTO new_table_name]
FROM table_source
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
USE AdventureWorks2008;
GO
SELECT *
FROM HumanResources.Employee
USE AdventureWorks2008;
GO
SELECT FirstName, LastName, JobTitle
FROM HumanResources.Employee
Lesson 2: Filtering Data
• Retrieving Specific Rows in a Table
Simple
WHERE WHERE clausea Predicate
Clauseclause
Simple WHERE Using
USE
USEAdventureWorks2008;
USE AdventureWorks2008;
AdventureWorks2008;
GO
GOGO
SELECTBusinessEntityID
SELECT
SELECT BusinessEntityID
FirstName, ASAS‘Employee
LastName,‘Employee Identification
Identification
MiddleName Number’,
Number’
HireDate,
FROM
FROM VacationHours, SickLeaveHours
HumanResources.Employee
Person.Person
FROM HumanResources.Employee
WHERE MiddleName IS =
WHERE BusinessEntityID 227;
NULL;
WHERE HireDate > ‘01/01/2000’
USE AdventureWorks2008;
GO
SELECT FirstName, LastName, MiddleName
FROM Person.Person
WHERE ModifiedDate >= ‘01/01/2004’
Filtering Data by Using String Comparisons
• String Comparisons are used for data types of text, ntext,
char, nchar, varchar, and nvarchar
• Predicates are available for full or partial match comparisons
Returns
•Logical all rows
Returnsoperators
only rowswith first
with
are name
first
used of of
toname ‘John’
combine and
‘John’ alllast
last
and
conditions in a
rows
namewith
name not last name
equal
of ‘Smith’
statement of ‘Smith’
to ‘Smith’
WHERE FirstName = ‘John’ AND LastName = ‘Smith’
WHERE FirstName = ‘John’ AND NOT LastName
Returns only rows with firstOR LastName
name and= last
= ‘Smith’
of ‘John’ ‘Smith’
name of
‘Smith’
WHERE FirstName = ‘John’ AND LastName = ‘Smith’
Returns all rows with first name of ‘John’ and all rows with last
name of ‘Smith’
WHERE FirstName = ‘John’ OR LastName = ‘Smith’
Returns all rows with first name of ‘John’ and last name not
equal to ‘Smith’
WHERE FirstName = ‘John’ AND NOT LastName = ‘Smith’
Retrieving a Range of Values
• BETWEEN tests for data values within a range of values.
SELECT OrderDate, AccountNumber, SubTotal, TaxAmt
FROM Sales.SalesOrderHeader
WHERE OrderDate BETWEEN ’08/01/2001’ AND
’08/31/2001’
t e
d a
Retrieving a Range of Values
• BETWEEN tests for data values within a range of values.
SELECT OrderDate, AccountNumber, SubTotal, TaxAmt
FROM Sales.SalesOrderHeader
WHERE OrderDate BETWEEN ’08/01/2001 00:00:00.000’
AND ’08/31/2001 23:59:59.999’
m e
ti
t e
d a
Retrieving a List of Values
• IN tests a column’s values against a list of possible
values.
SELECT SalesOrderID, OrderQty, ProductID, UnitPrice
FROM Sales.SalesOrderDetail
WHERE ProductID IN (750, 753, 765, 770)
~ (Bitwise Not)
Comparisons
=, >, <, >=, <=, <>, !=, !>, !<
NOT
AND
=(Assignment)
Demonstration: Filter Data by Using Different
Search Conditions
In this demonstration you will learn some of the ways that
data can be filtered to show only the rows that meet
specific criteria.
You will use:
• Comparison operators
• Range operators
Lesson 3: Working with NULL Values
• Considerations for Working with NULL Values
COALESCE()
NULLIF()
ISNULL() returns
ISNULL() returns
returns NULLtheiffirst
aa given
given both non
value
value NULL
specified
ifif the expression
the column
column valueamong
expressions
value are
is
is its
equal
NULL
NULL
arguments, similar to a CASE statement
SELECT
SELECT Description, DiscountPct,
ProductID, MakeFlag, MinQty, ISNULL(MaxQty, 0) AS
FinishedGoodsFlag,
'Max
SELECT Quantity'
CAST(COALESCE(hourly_wage *AS
NULLIF(MakeFlag,FinishedGoodsFlag) 40'Null
* 52, salary,
if Equal'
FROM
FROM Sales.SpecialOffer;
commission * num_sales) AS money) AS 'Total Salary'
Production.Product
FROM
WHEREwages
NULLIF() ProductID < 10;
returns NULL if both specified expressions are equal
• Using Expressions
Sorting Data
SELECT e.BusinessEntityID
AS ‘Employee Identification Number’
FROM HumanResources.Employee AS e
SELECT e.BusinessEntityID
‘Employee Identification Number’
FROM HumanResources.Employee e;
Using String Literals
String Literals:
• Are constant values.
Logon information
Virtual machine NY-SQL-01
User name Administrator
Password Pa$$w0rd