2. SELECT Queries
• Used to display data and calculations
• Main tool for making use of the data that‟s
stored in tables
– SELECT can be used as part of other commands
3. Key Words
• SELECT {field list}: What‟s returned
• FROM {data source}: Which table to use
• WHERE {criteria}: Which rows to include
• GROUP BY {field list}: How to summarize
• HAVING {field list}: Which groups to include
when grouping
• ORDER BY {field list}: How to sort
4. How Process
1. FROM – get data from original source(s)
2. WHERE – limit rows to work with
3. GROUP BY – create groups
4. HAVING – limit groups
5. SELECT – specify what to return
6. ORDER BY – specify sorting
5. SELECT
• SELECT starts statement
• Followed by field list
– Fields can refer to table fields or calculations created
as part of query
• SELECT can be used by itself:
SELECT GetDate()
6. FROM
• Describes where to find the data
• Can refer to a table or view
– View is a pre-defined select query, considered a
“virtual” table
• Basic query typically has SELECT and FROM
SELECT * FROM Titles
7. Using SELECT
• INTO – write results into table
• DISTINCT – eliminate repeating value(s) or
rows
• AS – name or rename a table or column
• * - include all fields from source
8. INTO
• Optional clause can write data from one or more
tables into another (new) table
• Typically used for
– Archiving
– To speed processing by creating temporary or
summary tables
– To organize for reporting
SELECT pub_state
INTO PublisherStates
FROM Publishers
9. DISTINCT
• Distinct eliminates repeating rows or values
– Used in SELECT clause
SELECT DISTINCT Title FROM Titles
Ensures that a title only is returned once, regardless of how
may times a title is found
SELECT DISTINCT pubid FROM Titles
Lists each publisher in the titles table just once, regardless of
number of books published
10. AS
• AS is used to create a field name for a calculation
or to rename an existing field
– Also referred to as an alias
– Use is optional
Rename fields in authors:
SELECT au_lname AS LastName FROM Authors
SELECT au_fname FirstName FROM Authors
11. *
• * represents all fields in data source
– Results may differ from what‟s expected if fields are
added, deleted or order changed
SELECT * FROM Publishers
12. Where
• WHERE limits which rows are included in the
result
• Criteria evaluate to True/False
– True means row will be used for result, false means
row will be excluded
– Sometimes „Predicates‟ is used in place of criteria (a
predicate is a characteristic that‟s true about a row)
13. Comparison Operators
• Relational (=, >, <,<>,>=,<=)
• Like – compares text patterns
– Wildcards
• One Character: _ (underscore)
• Any combination: %
• Between – compares to range
• Is – compares true/false/null
• In – value is in a list
• Exists – whether there‟s a value
15. Logical Operators
• If have more than one test use AND/OR to tie
tests together
– AND: All parts must evaluate to True for a row to be
included
– OR: Any part evaluating to True means a row is
included
– Precedence: NOT then AND then OR
• NOT returns inverse
– Not False returns True
17. Group By
• Allows data from tables to be summarized
– Find rows that share a common value
• Can discover information about the group using
aggregate functions
– Count
– Average
– Max
18. Sample Grouping
• Count number of books by publisher:
SELECT Count(*) BookCount, pub_id
FROM titles
GROUP BY pub_id
• Count books by price
SELECT Price, Count(*) AS BookCount
FROM Titles
GROUP BY Price
19. Having
• Determines which groups to include
• Use Criteria similar to Where clause
– Test characteristics of group using HAVING
– Use WHERE to determine which rows to include
20. Sample Having
• Display publisher ID‟s of those publishers
having more than 5 books published:
SELECT pub_id
FROM Titles
GROUP BY pub_id
HAVING Count(*) > 5
21. Order By
• Provides means to sort results by a column
(actual or calculated)
• Default sort order is Ascending
ORDER BY pub_date
ORDER BY au_lname DESC