Top Banner
INTRODUCTION TO QUERIES William Klingelsmith
25

INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Dec 13, 2015

Download

Documents

Whitney Ross
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: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

INTRODUCTION TO QUERIESWilliam Klingelsmith

Page 2: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Reminders• MyITLab Lesson C due 10/22• Homework #4 (Nielson TV Problem) due 10/26• Last day to drop is also 10/26• Midterm grades will be computed on HWK1-3 and Excel

Exam

Page 3: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Database Updated• The Music database we worked on last class has been

augmented with several records• Two new tables (Genres and Sales) need to be added to

this database• These tables and their records are stored in external files

Page 4: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Importing Data• To import data from external sources, you will utilize the

External Data tab in Access.• There are many different import/export options, but today

we will focus on XML file imports.• “XML File” in the Import & Link subgroup on the External

Data Tab

Page 5: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Importing Data

• After you choose your file, you will get this dialog.

• Most of the time for this class, the default ‘Structure and Data’ is appropriate.

Page 6: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Establish Relationships• Now that we have all of our tables, we must link them

together using relationships• Albums -> Songs• Albums -> Sales

• Use Referential Integrity• What about the link between Genre and Albums?

Page 7: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Lookup Fields• Lookup fields are special Data Types in tables.

• Possible choices for a field in one table are determined by existing entries in another table.

• They let you make a dropdown type box for a field which can aide in data entry efficiency.

• They will build a relationship between two tables automatically.

Page 8: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Lookup Fields• Move to the design view of the Albums table• Change the data type for the Genre field to Lookup

Wizard• Follow the wizard’s instructions to create the lookup field.• When finished, the wizard should create a relationship

between the Genre and Albums tables.

Page 9: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Queries• Queries are used to view a subset of the data contained in

your database• These subsets are most commonly broken down by a limiting

criteria• “Return only the songs that are on albums by a particular artist”• “List all albums of a certain genre”

• Think of QUEries as QUEstions!

Page 10: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Constructing a Query• Let’s start our query lesson with a simple example• We wish to list every Artist and Song name• Where is this information stored?

Page 11: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Constructing a Query

• Move to the Create tab and choose Query Design

• The Show Table dialog lets you add relevant tables to your query

• Choose only Songs for this query

Page 12: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Constructing a Query• Field: Name of the field• Table: Table the field belongs to• Sort: Sorting the results in Ascending/Descending order• Show: Hide/Display the result in the query• Criteria and Or: Allows you to narrow filter results of the

query on some criteria

Page 13: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Running a Query• Queries can be executed in two ways

• The Run button in the design view of a query• Switching to the Datasheet view of a query

Page 14: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Adding Criteria• Save the last query as Query A• Duplicate it in the object pane• Call the copy Query B• Open the design view of Query B

• For this query, we wish to only return the songs performed by the artist ‘Venetian Snares’

• In the criteria row under the Artist field type “Venetian Snares”

• Run the query

Page 15: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Criteria Notes• Criteria across multiple fields on the same line are all

used at once (AND)• Multiple criteria under the same field are all acceptable

choices (OR)• Conditional operators (<, >, <=, >=, etc) can be used in

criteria• Access has two wildcard operators (? And *)

Page 16: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Criteria Examples (OR)• Duplicate Query B and call it Query C• On the row below where “Venetian Snares” currently

resides, type “Fleet Foxes”• Notice that songs from both artists are now returned in the

result• Criteria is OR’d• It is saying that results can be either from Venetian Snares OR

Fleet Foxes

Page 17: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Criteria Examples (AND)• Duplicate the last query and call it Query D• Add the Favorite field from the Songs table to your query

design• Criteria should be as follows:

• Artist: Venetian Snares• Favorite: Yes

• We have fewer results than before• Criteria across multiple fields on the same row are AND’d• “Songs must both be by Venetian Snares and a favorite to be

included in the query”

Page 18: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Criteria Examples (Conditional Operators)

• Let’s try to get a list of all albums released before the year 2000

• Create a new query and add the Songs and Albums tables

• Populate the query with the fields: Artist, SongName, AlbumName, Genre, ReleaseDate

• In the criteria under the release date enter: “<1/1/2000”

Page 19: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Criteria Examples (Wildcards)• Wildcards are operators that can be substituted for a set

of characters• * Operator allows any set of characters of any length

• Let’s say we had a field that held first names. By using S* in the criteria, we could get these results from our query• Sara• Stephen• Sue

• ? Operator allows any set of characters of length 1• By using ?ill we could get results such as:

• Will• Bill• Jill

Page 20: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

Criteria Examples (Wildcards)• Return only Albums that begin with the letter T

Page 21: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

New Query 1• We must construct a query to display the name of the

artist, the song title, the album name, and the genre of the album. Only display results for the Hip-Hop and House genres.

• Hint: You will need three tables.• Hint: You should have a total of 4 results

Page 22: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

New Query 2• Create a query to display the album name, album price,

and genre. Only display albums where the genre is House and the album price is above $9.

• Hint: You should get only one result

Page 23: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

New Query 3• Duplicate Query 2. Modify the query so that albums from

the EBM genre are included regardless of album price.

• Hint: You should get 3 results

Page 24: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

New Query 4• Construct a query that displays the album name, total

sales in dollars, and release date. Only display albums that were either released before the year 2000 or had total sales greater than 10 million dollars.

• Hint: You should get 8 results

Page 25: INTRODUCTION TO QUERIES William Klingelsmith. Reminders MyITLab Lesson C due 10/22 Homework #4 (Nielson TV Problem) due 10/26 Last day to drop is also.

New Query 5• Construct a query to display the artist name, song name,

album name, and genre. Display only genres that begin with the letter B or H.

• Hint: Use Wildcards• Hint: You should get 7 results