Your SlideShare is downloading. ×
0
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Select Queries
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Select Queries

687

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
687
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×