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
– SE...
Key Words
• SELECT {field list}: What‟s returned
• FROM {data source}: Which table to use
• WHERE {criteria}: Which rows t...
How Process
1. FROM – get data from original source(s)
2. WHERE – limit rows to work with
3. GROUP BY – create groups
4. H...
SELECT
• SELECT starts statement
• Followed by field list
– Fields can refer to table fields or calculations created
as pa...
FROM
• Describes where to find the data
• Can refer to a table or view
– View is a pre-defined select query, considered a
...
Using SELECT
• INTO – write results into table
• DISTINCT – eliminate repeating value(s) or
rows
• AS – name or rename a t...
INTO
• Optional clause can write data from one or more
tables into another (new) table
• Typically used for
– Archiving
– ...
DISTINCT
• Distinct eliminates repeating rows or values
– Used in SELECT clause
SELECT DISTINCT Title FROM Titles
Ensures ...
AS
• AS is used to create a field name for a calculation
or to rename an existing field
– Also referred to as an alias
– U...
*
• * represents all fields in data source
– Results may differ from what‟s expected if fields are
added, deleted or order...
Where
• WHERE limits which rows are included in the
result
• Criteria evaluate to True/False
– True means row will be used...
Comparison Operators
• Relational (=, >, <,<>,>=,<=)
• Like – compares text patterns
– Wildcards
• One Character: _ (under...
Sample WHERE Clauses
SELECT *
FROM Titles
WHERE Price > 20
SELECT au_Lname
FROM Authors
WHERE 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...
Logical Operator Examples
SELECT *
FROM Titles
WHERE Price > 20
AND Title LIKE „%SQL%‟
SELECT *
FROM Titles
WHERE Not Cont...
Group By
• Allows data from tables to be summarized
– Find rows that share a common value
• Can discover information about...
Sample Grouping
• Count number of books by publisher:
SELECT Count(*) BookCount, pub_id
FROM titles
GROUP BY pub_id
• Coun...
Having
• Determines which groups to include
• Use Criteria similar to Where clause
– Test characteristics of group using H...
Sample Having
• Display publisher ID‟s of those publishers
having more than 5 books published:
SELECT pub_id
FROM Titles
G...
Order By
• Provides means to sort results by a column
(actual or calculated)
• Default sort order is Ascending
ORDER BY pu...
Upcoming SlideShare
Loading in …5
×

Select Queries

810 views
745 views

Published on

Published in: Education, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
810
On SlideShare
0
From Embeds
0
Number of Embeds
280
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Select Queries

  1. 1. DML: SELECT CIS-182
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  14. 14. Sample WHERE Clauses SELECT * FROM Titles WHERE Price > 20 SELECT au_Lname FROM Authors WHERE State <> „wa‟
  15. 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
  16. 16. Logical Operator Examples SELECT * FROM Titles WHERE Price > 20 AND Title LIKE „%SQL%‟ SELECT * FROM Titles WHERE Not Contract
  17. 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. 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. 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. 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. 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

×