CIS245 sql
-
Upload
randy-riness-south-puget-sound-community-college -
Category
Education
-
view
266 -
download
1
Transcript of CIS245 sql
![Page 1: CIS245 sql](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/1.jpg)
CIS-245
![Page 2: CIS245 sql](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/11.jpg)
* 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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/14.jpg)
SELECT *FROM tblRatesWHERE rts_curAmount > 20
SELECT *FROM tblAddressesWHERE add_txtState <> ‘wa’
![Page 15: CIS245 sql](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/16.jpg)
SELECT * FROM Titles WHERE Price > 20 AND Title LIKE ‘%SQL%’
SELECT *FROM TitlesWHERE Not Contract
![Page 17: CIS245 sql](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/18.jpg)
Count number of agreements by Customer:SELECT Count(*) AgreementCount,
agr_lngCustomerIDFROM tblAgreementsGROUP BY agr_lngCustomerID
![Page 19: CIS245 sql](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062319/55789d06d8b42aaf518b4e21/html5/thumbnails/21.jpg)
Provides means to sort results by a column (actual or calculated)
Default sort order is Ascending
ORDER BY pub_dateORDER BY au_lname DESC