Top Banner
Chapter 10 Using SQL Microsoft Access 2013
54
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Access2013 ch10

Chapter 10

Using SQL

Microsoft

Access 2013

Page 2: Access2013 ch10

• Understand the SQL language and how to use it

• Change the font or font size for SQL queries

• Create SQL queries

• Include fields in SQL queries

• Include simple and compound criteria in SQL queries

• Use computed fields and built-in functions in SQL queries

• Sort the results in SQL queries

Using SQL 2

Objectives

Page 3: Access2013 ch10

• Use aggregate functions in SQL queries

• Group the results in SQL queries

• Join tables in SQL queries

• Use subqueries

• Compare SQL queries with Access-generated SQL

• Use INSERT, UPDATE, and DELETE queries to update a database

Using SQL 3

Objectives

Page 4: Access2013 ch10

Using SQL 4

Project – Using SQL

Page 5: Access2013 ch10

• Create a query in SQL view

• Use simple criteria in a query

• Use compound criteria in a query

• Sort results of a query

• Group results of a query

• Join tables in a query

• Use a subquery in a query

• Update data with a query

Using SQL 5

Roadmap

Page 6: Access2013 ch10

• Tap or click FILE on the ribbon to open the Backstage view

• Tap or click Options to display the Access Options dialog box

• Tap or click Object Designers to display the Object Designer options

• In the Query design area, tap or click the Size box arrow, and then tap or click the desired font size

• Tap or click the OK button to close the Access Options dialog box

Using SQL 6

Changing the Font Size

Page 7: Access2013 ch10

Using SQL 7

Changing the Font Size

Page 8: Access2013 ch10

• Close the Navigation Pane

• Display the CREATE tab

• Tap or click the Query Design button (CREATE tab | Queries group) to create a query

• Close the Show Table dialog box without adding any tables

• Tap or click the View arrow (QUERY TOOLS DESIGN tab | Results group) to display the View menu

• Tap or click SQL View to view the query in SQL view

Using SQL 8

Creating a New SQL Query

Page 9: Access2013 ch10

Using SQL 9

Creating a New SQL Query

Page 10: Access2013 ch10

• The basic form of SQL expressions is quite simple: SELECT-FROM-WHERE

• The command begins with a SELECT clause, which consists of the word, SELECT, followed by a list of those fields you want to include

• Next, the command contains a FROM clause, which consists of the word, FROM, followed by a list of the table or tables involved in the query

• Finally, there is an optional WHERE clause, which consists of the word, WHERE, followed by any criteria that the data you want to retrieve must satisfy

Using SQL 10

SQL Commands

Page 11: Access2013 ch10

Using SQL 11

SQL Commands

Page 12: Access2013 ch10

• Tap or click the View arrow (HOME tab | Views group) to display the View menu

• Tap or click SQL View to return to SQL view

Using SQL 12

Preparing to Enter a New SQL Query

Page 13: Access2013 ch10

• To include all fields, you could use the same approach as in the previous steps, that is, list each field in the table after the word, SELECT

• There is a shortcut, however. Instead of listing all the field names after SELECT, you can use the asterisk (*) symbol

Using SQL 13

Including All Fields

Page 14: Access2013 ch10

Using SQL 14

Including All Fields

Page 15: Access2013 ch10

• To restrict the records to be displayed, include the word WHERE followed by a criterion as part of the command

• If the field involved is a numeric field, you simply type the value

Using SQL 15

Using a Criterion Involving

a Numeric Field

Page 16: Access2013 ch10

• A simple criterion has the form: field name, comparison operator, then either another field name or a value

Using SQL 16

Simple Criteria

Page 17: Access2013 ch10

Using SQL 17

Using a Comparison Operator

Page 18: Access2013 ch10

• If the criterion involves a text field, the value must be enclosed in single quotation marks

Using SQL 18

Using a Criterion Involving a Text Field

Page 19: Access2013 ch10

• In most cases, the conditions in WHERE clauses involve exact matches

• The LIKE operator uses one or more wildcard characters to test for a pattern match

– One common wildcard in Access, the asterisk (*), represents any collection of characters

Using SQL 19

Using a Wildcard

Page 20: Access2013 ch10

Using SQL 20

Using a Wildcard

Page 21: Access2013 ch10

• Compound criteria are formed by connecting two or more simple criteria using AND, OR, and NOT

– When simple criteria are connected by the word AND, all the simple criteria must be true in order for the compound criterion to be true

– When simple criteria are connected by the word OR, the compound criterion will be true whenever any of the simple criteria are true

– Preceding a criterion by the word NOT reverses the truth or falsity of the original criterion

Using SQL 21

Compound Criteria

Page 22: Access2013 ch10

Using SQL 22

Using a Compound Criterion

Involving AND

Page 23: Access2013 ch10

Using SQL 23

Using a Compound Criterion

Involving OR

Page 24: Access2013 ch10

Using SQL 24

Using NOT in a Criterion

Page 25: Access2013 ch10

• Just as with queries created in Design view, you can include fields in queries that are not in the database, but that can be computed from fields that are

– Called a computed field

– Can involve addition, subtraction, multiplication, or division

• To indicate the contents of the new field (the computed field), you can name the field by following the computation with the word, AS, and then the name you want to assign the field

Using SQL 25

Using a Computed Field

Page 26: Access2013 ch10

Using SQL 26

Using a Computed Field

Page 27: Access2013 ch10

• To sort the output, you include an ORDER BY clause, which consists of the words ORDER BY followed by the sort key

Using SQL 27

Sorting

Page 28: Access2013 ch10

Using SQL 28

Sorting the Results on a Single Field

Page 29: Access2013 ch10

Using SQL 29

Sorting the Results on Multiple Fields

Page 30: Access2013 ch10

• To sort in descending order, you follow the name of the sort key with the DESC operator

Using SQL 30

Sorting the Results in Descending Order

Page 31: Access2013 ch10

• The DISTINCT operator eliminates duplicate values in the results of a query

• To use the operator, you follow the word DISTINCT with the field name in parentheses

Using SQL 31

Omitting Duplicates When Sorting

Page 32: Access2013 ch10

• SQL has built-in functions, also called aggregate functions, to perform various calculations

– COUNT

– SUM

– AVG

– MAX

– MIN

Using SQL 32

Using a Built-In Function

Page 33: Access2013 ch10

Using SQL 33

Using a Built-In Function

Page 34: Access2013 ch10

• You can assign a name to the results of a function

• To do so, follow the expression for the function with the word AS and then the name to be assigned to the result

Using SQL 34

Assigning a Name to the Results

of a Function

Page 35: Access2013 ch10

Using SQL 35

Using Multiple Functions

in the Same Command

Page 36: Access2013 ch10

• Grouping means creating groups of records that share some common characteristic

• When you group rows, any calculations indicated in the SELECT command are performed for the entire group

– GROUP BY clause

Using SQL 36

Using Grouping

Page 37: Access2013 ch10

Using SQL 37

Using Grouping

Page 38: Access2013 ch10

Using SQL 38

Using Grouping

Page 39: Access2013 ch10

• The HAVING clause, which consists of the word HAVING followed by a criterion, is used to restrict the groups to be included

Using SQL 39

Restricting the Groups that Appear

Page 40: Access2013 ch10

• Many queries require data from more than one table

Using SQL 40

Joining Tables

Page 41: Access2013 ch10

• You can restrict the records to be included in a join by creating a compound criterion

Using SQL 41

Restricting the Records in a Join

Page 42: Access2013 ch10

• When tables appear in the FROM clause, you can give each table an alias, or an alternative name, that you can use in the rest of the statement

Using SQL 42

Aliases

Page 43: Access2013 ch10

Using SQL 43

Joining a Table to Itself

Page 44: Access2013 ch10

Using SQL 44

Joining a Table to Itself

Page 45: Access2013 ch10

• A subquery is a query within another query

Using SQL 45

Subqueries

Page 46: Access2013 ch10

• You can add records to a table using the SQL INSERT command

• The command consists of the words INSERT INTO followed by the name of the table into which the record is to be inserted

• Next is the word VALUE followed by the values for the fields in the record

– Values for Text fields must be enclosed within quotation marks

Using SQL 46

Using an INSERT Command

Page 47: Access2013 ch10

Using SQL 47

Using an INSERT Command

Page 48: Access2013 ch10

• You can update records in SQL by using the UPDATE command

• The command consists of UPDATE, followed by the name of the table in which records are to be updated

• Next, the command contains one or more SET clauses, which consist of the word SET, followed by a field to be updated, an equal sign, and the new value

Using SQL 48

Using an UPDATE Command

Page 49: Access2013 ch10

Using SQL 49

Using an UPDATE Command

Page 50: Access2013 ch10

• You can delete records in SQL using the DELETE command

• The command consists of DELETE FROM, followed by the name of the table from which records are to be deleted

• Finally, you include a WHERE clause to specify the criteria

Using SQL 50

Using a DELETE Command

Page 51: Access2013 ch10

Using SQL 51

Using a DELETE Command

Page 52: Access2013 ch10

• Understand the SQL language and how to use it

• Change the font or font size for SQL queries

• Create SQL queries

• Include fields in SQL queries

• Include simple and compound criteria in SQL queries

• Use computed fields and built-in functions in SQL queries

• Sort the results in SQL queries

Using SQL 52

Chapter Summary

Page 53: Access2013 ch10

• Use aggregate functions in SQL queries

• Group the results in SQL queries

• Join tables in SQL queries

• Use subqueries

• Compare SQL queries with Access-generated SQL

• Use INSERT, UPDATE, and DELETE queries to update a database

Using SQL 53

Chapter Summary

Page 54: Access2013 ch10

Chapter 10 Complete

Microsoft

Access 2013