Chapter 2
Retrieving Data from a Single
Table
Overview of the SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
How to Use the SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
SELECT and FROM Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
ORDER BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
GROUP BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
HAVING Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Ordering the SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Selecting Columns in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Selecting All Columns in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Selecting Specific Columns in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Eliminating Duplicate Rows from the Query Results . . . . . . . . . . . . . . . . . . . . . . . 25
Determining the Structure of a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Creating New Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Adding Text to Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Calculating Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Assigning a Column Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Referring to a Calculated Column by Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Assigning Values Conditionally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Replacing Missing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Specifying Column Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Sorting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Overview of Sorting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Sorting by Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Sorting by Multiple Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Specifying a Sort Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Sorting by Calculated Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Sorting by Column Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Sorting by Columns That Are Not Selected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Specifying a Different Sorting Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Sorting Columns That Contain Missing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Retrieving Rows That Satisfy a Condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Using a Simple WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Retrieving Rows Based on a Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Retrieving Rows That Satisfy Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . 47
Using Other Conditional Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Using Truncated String Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Using a WHERE Clause with Missing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
19

Get SAS 9.4 SQL Procedure User's Guide, Fourth Edition, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.