CIS245 sql


Published on

Published in: Education, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • CIS-182 Select Statement 02/09/12
  • 02/09/12 CIS-182 Select Statement
  • CIS245 sql

    1. 1. CIS-245
    2. 2. <ul><li>Used to display data and calculations </li></ul><ul><li>Main tool for making use of the data that’s stored in tables </li></ul><ul><ul><li>SELECT can be used as part of other commands </li></ul></ul>
    3. 3. <ul><li>SELECT {field list}: What’s returned </li></ul><ul><li>FROM {data source}: Which table to use </li></ul><ul><li>WHERE {criteria}: Which rows to include </li></ul><ul><li>GROUP BY {field list}: How to summarize </li></ul><ul><li>HAVING {field list}: Which groups to include when grouping </li></ul><ul><li>ORDER BY {field list}: How to sort </li></ul>
    4. 4. <ul><li>FROM – get data from original source(s) </li></ul><ul><li>WHERE – limit rows to work with </li></ul><ul><li>GROUP BY – create groups </li></ul><ul><li>HAVING – limit groups </li></ul><ul><li>SELECT – specify what to return </li></ul><ul><li>ORDER BY – specify sorting </li></ul>
    5. 5. <ul><li>SELECT starts statement </li></ul><ul><li>Followed by field list </li></ul><ul><ul><li>Fields can refer to table fields or calculations created as part of query </li></ul></ul><ul><li>SELECT can be used by itself: </li></ul><ul><ul><ul><li>SELECT GetDate() </li></ul></ul></ul>
    6. 6. <ul><li>Describes where to find the data </li></ul><ul><li>Can refer to a table or view </li></ul><ul><ul><li>View is a pre-defined select query, considered a “virtual” table </li></ul></ul><ul><li>Basic query typically has SELECT and FROM </li></ul><ul><ul><li>SELECT * FROM Titles </li></ul></ul>
    7. 7. <ul><li>INTO – write results into table </li></ul><ul><li>DISTINCT – eliminate repeating value(s) or rows </li></ul><ul><li>AS – name or rename a table or column </li></ul><ul><li>* - include all fields from source </li></ul>
    8. 8. <ul><li>Optional clause can write data from one or more tables into another (new) table </li></ul><ul><ul><li>Access uses this for Append queries </li></ul></ul><ul><li>Typically used for </li></ul><ul><ul><li>Archiving </li></ul></ul><ul><ul><li>To speed processing by creating temporary or summary tables </li></ul></ul><ul><ul><li>To organize for reporting </li></ul></ul>
    9. 9. <ul><li>Distinct eliminates repeating rows or values </li></ul><ul><ul><li>Used in SELECT clause </li></ul></ul><ul><li>SELECT DISTINCT Lastname FROM Persons </li></ul><ul><ul><ul><li>Ensures that a Lastname is returned once, regardless of how may times that Lastname exists in persons </li></ul></ul></ul>
    10. 10. <ul><li>AS is used to create a field name for a calculation or to rename an existing field </li></ul><ul><ul><li>Also referred to as an alias </li></ul></ul><ul><ul><li>Use is optional </li></ul></ul><ul><ul><li>Rename fields in tblPersons: </li></ul></ul><ul><ul><ul><li>SELECT prs_txtLastname AS LastName FROM tblPersons </li></ul></ul></ul><ul><ul><ul><li>SELECT prs_txtFirstName FirstName FROM tblPersons </li></ul></ul></ul>
    11. 11. <ul><li>* represents all fields in data source </li></ul><ul><ul><li>Results may differ from what’s expected if fields are added, deleted or order changed </li></ul></ul><ul><li>SELECT * FROM tblCustomers </li></ul>
    12. 12. <ul><li>WHERE limits which rows are included in the result </li></ul><ul><li>Criteria evaluate to True/False </li></ul><ul><ul><li>True means row will be used for result, false means row will be excluded </li></ul></ul><ul><ul><li>Sometimes ‘Predicates’ is used in place of criteria (a predicate is a characteristic that’s true about a row) </li></ul></ul>
    13. 13. <ul><li>Relational (=, >, <,<>,>=,<=) </li></ul><ul><li>Like – compares text patterns </li></ul><ul><ul><li>Wildcards </li></ul></ul><ul><ul><ul><li>One Character: _ (underscore) </li></ul></ul></ul><ul><ul><ul><li>Any combination: % </li></ul></ul></ul><ul><li>Between – compares to range </li></ul><ul><li>Is – compares true/false/null </li></ul><ul><li>In – value is in a list </li></ul><ul><li>Exists – whether there’s a value </li></ul>
    14. 14. <ul><li>SELECT * </li></ul><ul><li>FROM tblRates </li></ul><ul><li>WHERE rts_curAmount > 20 </li></ul><ul><li>SELECT * </li></ul><ul><li>FROM tblAddresses </li></ul><ul><li>WHERE add_txtState <> ‘wa’ </li></ul>
    15. 15. <ul><li>If have more than one test use AND/OR to tie tests together </li></ul><ul><ul><li>AND : All parts must evaluate to True for a row to be included </li></ul></ul><ul><ul><li>OR : Any part evaluating to True means a row is included </li></ul></ul><ul><ul><li>No Precedence </li></ul></ul><ul><li>NOT returns inverse </li></ul><ul><ul><li>Not False returns True </li></ul></ul>
    16. 16. <ul><li>SELECT * </li></ul><ul><li>FROM Titles </li></ul><ul><li>WHERE Price > 20 </li></ul><ul><li>AND Title LIKE ‘%SQL%’ </li></ul><ul><li>SELECT * </li></ul><ul><li>FROM Titles </li></ul><ul><li>WHERE Not Contract </li></ul>
    17. 17. <ul><li>Allows data from tables to be summarized </li></ul><ul><ul><li>Find rows that share a common value </li></ul></ul><ul><li>Can discover information about the group using aggregate functions </li></ul><ul><ul><li>Count </li></ul></ul><ul><ul><li>Average </li></ul></ul><ul><ul><li>Max </li></ul></ul>
    18. 18. <ul><li>Count number of agreements by Customer: </li></ul><ul><ul><li>SELECT Count(*) AgreementCount, agr_lngCustomerID </li></ul></ul><ul><ul><li>FROM tblAgreements </li></ul></ul><ul><ul><li>GROUP BY agr_lngCustomerID </li></ul></ul>
    19. 19. <ul><li>Determines which groups to include </li></ul><ul><li>Use Criteria similar to Where clause </li></ul><ul><ul><li>Test characteristics of group using HAVING </li></ul></ul><ul><ul><ul><li>Tests involve aggregate functions which summarize characteristics of rows </li></ul></ul></ul><ul><ul><li>Use WHERE to determine which particular rows to include in result </li></ul></ul>
    20. 20. <ul><li>Display Employee ID’s of those Employees who have received more than 5 payments: </li></ul><ul><ul><li>SELECT pmt_lngEmployeeID </li></ul></ul><ul><ul><li>FROM tblPayments </li></ul></ul><ul><ul><li>GROUP BY pmt_lngEmployeeID </li></ul></ul><ul><ul><li>HAVING Count(*) > 5 </li></ul></ul>
    21. 21. <ul><li>Provides means to sort results by a column (actual or calculated) </li></ul><ul><li>Default sort order is Ascending </li></ul><ul><li>ORDER BY pub_date </li></ul><ul><li>ORDER BY au_lname DESC </li></ul>