Query Writing Exercise Access07
Query Writing Exercise Access07
Query Writing Exercise Access07
This tutorial will guide you through the process of writing SQL queries using MS-Access
(2007 version). This will be an important skill to have when it comes time to extract data
from the MySQL database you’ll be using for your final project.
If you need to quit in the middle of this tutorial or want to save any of these queries for
later use, select File > Save As and give the query an appropriate name. You’ll then
see it in the list of Queries and can run it again later by double-clicking on it.
3. Click on the strip at the top of the Navigation Pane (where it now says Queries)
and select Tables to see a list of the database’s tables.
4. Double-click on a table’s name in the Navigation Pane to open it. Open both
tables and note that the STATS table contains an ID for each player rather than
his name. The names associated with the IDs are stored in the PLAYERS table.
1. Click on the Create tab near the top of the application window.
2. Next click on the Query Design button (found on the right side of the Ribbon in
the group of commands labeled as Other).
3. In the Show Table dialog, double-click on the STATS table to add it to the query
and click Close.
4. Double-click on PLAYER_ID in the list of fields in the STATS table to add that
field to the design grid below.
5. Repeat this step to add the YEAR and RBI fields.
6. Click on the Run button (red exclamation point icon) on the left of the Ribbon to
run the query. The result should contain the same number of records as the
underlying STATS table (211), with the only difference being you’ve selected just
the columns you’re interested in.
36. The LAST_NAME column is now redundant, so remove it from the design grid as
described above.
37. Run the query and confirm that the name values concatenated properly.
58. Run the query and note that the calculated values include several digits after the
decimal point. These digits aren’t really necessary, so return to Design View,
right-click on the ABPERHR field and select Properties.
59. A Property Sheet pane will appear on the right side of the window. Select Fixed
from the Format drop-down list and set the Decimal Places value to 1.
60. Re-run the query and confirm that you get the desired results.
Practice Exercises:
1. Write a query that displays all fields from the STATS table for players on the San
Francisco Giants (SFG).
2. Write a query that outputs the batting average (HITS divided by AB) for each
player and season. Format this value so that only 3 digits appear after the
decimal point (e.g., .275).
3. Write a query that displays all fields from the STATS table along with the names
of the players in the format “Ruth, Babe”.
4. Write a query that lists the highest 1-season RBI total each player had.
5. Write a query that calculates the number of years each player played for each of
his teams.