select queries
DESCRIPTION
TRANSCRIPT
![Page 1: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/1.jpg)
DML: SELECT
CIS-182
![Page 2: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/2.jpg)
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
![Page 3: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/3.jpg)
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
![Page 4: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/4.jpg)
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
![Page 5: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/5.jpg)
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()
![Page 6: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/6.jpg)
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
![Page 7: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/7.jpg)
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
![Page 8: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/8.jpg)
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
![Page 9: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/9.jpg)
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
![Page 10: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/10.jpg)
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
![Page 11: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/11.jpg)
*
• * represents all fields in data source– Results may differ from what’s expected if
fields are added, deleted or order changed
SELECT * FROM Publishers
![Page 12: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/12.jpg)
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)
![Page 13: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/13.jpg)
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
![Page 14: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/14.jpg)
Sample WHERE Clauses
SELECT *FROM TitlesWHERE Price > 20
SELECT au_LnameFROM AuthorsWHERE State <> ‘wa’
![Page 15: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/15.jpg)
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
![Page 16: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/16.jpg)
Logical Operator Examples
SELECT * FROM Titles WHERE Price > 20 AND Title LIKE ‘%SQL%’
SELECT *FROM TitlesWHERE Not Contract
![Page 17: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/17.jpg)
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
![Page 18: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/18.jpg)
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
![Page 19: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/19.jpg)
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
![Page 20: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/20.jpg)
Sample Having
• Display publisher ID’s of those publishers having more than 5 books published:SELECT pub_idFROM TitlesGROUP BY pub_idHAVING Count(*) > 5
![Page 21: Select Queries](https://reader033.vdocuments.mx/reader033/viewer/2022061223/54c5e0904a7959b8308b456b/html5/thumbnails/21.jpg)
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