cis245 sql

21
CIS-245

Upload: randy-riness-south-puget-sound-community-college

Post on 11-Jun-2015

266 views

Category:

Education


1 download

TRANSCRIPT

Page 1: CIS245 sql

CIS-245

Page 2: CIS245 sql

Used to display data and calculations Main tool for making use of the data

that’s stored in tablesSELECT can be used as part of other

commands

Page 3: CIS245 sql

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: CIS245 sql

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: CIS245 sql

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: CIS245 sql

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 FROMSELECT * FROM Titles

Page 7: CIS245 sql

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: CIS245 sql

Optional clause can write data from one or more tables into another (new) tableAccess uses this for Append queries

Typically used for ArchivingTo speed processing by creating temporary

or summary tablesTo organize for reporting

Page 9: CIS245 sql

Distinct eliminates repeating rows or valuesUsed in SELECT clause

SELECT DISTINCT Lastname FROM Persons

Ensures that a Lastname is returned once, regardless of how may times that Lastname exists in persons

Page 10: CIS245 sql

AS is used to create a field name for a calculation or to rename an existing field Also referred to as an aliasUse is optional

Rename fields in tblPersons:SELECT prs_txtLastname AS LastName FROM

tblPersonsSELECT prs_txtFirstName FirstName FROM

tblPersons

Page 11: CIS245 sql

* represents all fields in data sourceResults may differ from what’s expected if

fields are added, deleted or order changed

SELECT * FROM tblCustomers

Page 12: CIS245 sql

WHERE limits which rows are included in the result

Criteria evaluate to True/FalseTrue means row will be used for result, false

means row will be excludedSometimes ‘Predicates’ is used in place of

criteria (a predicate is a characteristic that’s true about a row)

Page 13: CIS245 sql

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: CIS245 sql

SELECT *FROM tblRatesWHERE rts_curAmount > 20

SELECT *FROM tblAddressesWHERE add_txtState <> ‘wa’

Page 15: CIS245 sql

If have more than one test use AND/OR to tie tests togetherAND: All parts must evaluate to True for a

row to be includedOR: Any part evaluating to True means a

row is includedNo Precedence

NOT returns inverseNot False returns True

Page 16: CIS245 sql

SELECT * FROM Titles WHERE Price > 20 AND Title LIKE ‘%SQL%’

SELECT *FROM TitlesWHERE Not Contract

Page 17: CIS245 sql

Allows data from tables to be summarizedFind rows that share a common value

Can discover information about the group using aggregate functionsCountAverageMax

Page 18: CIS245 sql

Count number of agreements by Customer:SELECT Count(*) AgreementCount,

agr_lngCustomerIDFROM tblAgreementsGROUP BY agr_lngCustomerID

Page 19: CIS245 sql

Determines which groups to include Use Criteria similar to Where clause

Test characteristics of group using HAVING Tests involve aggregate functions which

summarize characteristics of rowsUse WHERE to determine which particular

rows to include in result

Page 20: CIS245 sql

Display Employee ID’s of those Employees who have received more than 5 payments:SELECT pmt_lngEmployeeIDFROM tblPaymentsGROUP BY pmt_lngEmployeeIDHAVING Count(*) > 5

Page 21: CIS245 sql

Provides means to sort results by a column (actual or calculated)

Default sort order is Ascending

ORDER BY pub_dateORDER BY au_lname DESC