View
127
Download
3
Embed Size (px)
DESCRIPTION
Citation preview
DML: SELECT
CIS-182
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
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
How Process
1. FROM – get data from original source(s)2. WHERE – limit rows to work with3. GROUP BY – create groups4. HAVING – limit groups5. SELECT – specify what to return6. ORDER BY – specify sorting
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()
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
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
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
DISTINCT
• Distinct eliminates repeating rows or values– Used in SELECT clause
SELECT DISTINCT Title FROM TitlesEnsures that a title only is returned once, regardless of how may times a title is found
SELECT DISTINCT pubid FROM TitlesLists each publisher in the titles table just once, regardless of number of books published
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 AuthorsSELECT au_fname FirstName FROM Authors
*
• * represents all fields in data source– Results may differ from what’s expected if
fields are added, deleted or order changed
SELECT * FROM Publishers
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)
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
Sample WHERE Clauses
SELECT *FROM TitlesWHERE Price > 20
SELECT au_LnameFROM AuthorsWHERE State <> ‘wa’
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
Logical Operator Examples
SELECT * FROM Titles WHERE Price > 20 AND Title LIKE ‘%SQL%’
SELECT *FROM TitlesWHERE Not Contract
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
Sample Grouping
• Count number of books by publisher:SELECT Count(*) BookCount, pub_idFROM titlesGROUP BY pub_id
• Count books by priceSELECT Price, Count(*) AS BookCountFROM TitlesGROUP BY Price
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
Sample Having
• Display publisher ID’s of those publishers having more than 5 books published:SELECT pub_idFROM TitlesGROUP BY pub_idHAVING Count(*) > 5
Order By
• Provides means to sort results by a column (actual or calculated)
• Default sort order is Ascending
ORDER BY pub_dateORDER BY au_lname DESC