In this exercise you will practice some more SQL queries. First let s practice queries on a single table.

Similar documents
CSCI 1100L: Topics in Computing Lab Lab 07: Microsoft Access (Databases) Part I: Movie review database.

You can write a command to retrieve specified columns and all rows from a table, as illustrated

MIS2502: Data Analytics SQL Getting Information Out of a Database Part 1: Basic Queries

STOP DROWNING IN DATA. START MAKING SENSE! An Introduction To SQLite Databases. (Data for this tutorial at

Using Mail Merge in Microsoft Word XP/2002

MIS2502: Data Analytics SQL Getting Information Out of a Database. Jing Gong

Access 2007: Advanced Instructor s Edition

Using Mail Merge in Microsoft Word 2003

SharePoint Designer Advanced

A Simple Guide to Using SPSS (Statistical Package for the. Introduction. Steps for Analyzing Data. Social Sciences) for Windows

DB - Week 3 Lab1-2 Introduction to Databases. Dina A. Said

Access Made Easy. Forms.

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

Chapter11 practice file folder. For more information, see Download the practice files in this book s Introduction.

Using Data From Quandl in EViews

When you start Microsoft Access, the upper left of your screen looks like this.

STAT 113: R/RStudio Intro

CSC Web Programming. Introduction to SQL

Microsoft Access XP (2002) - Advanced Queries

Editing and Formatting Worksheets

3. Getting data out: database queries. Querying

Microsoft Excel Level 2

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

GETTING STARTED: Let s start by getting familiar with moving around in Access.

Upload and Go! Tired of doing data entry? Save time and increase cash flow by submitting accounts in bulk upload. Upload and Go!

Graded Project. Microsoft Excel

Microsoft Access 2013 Lab #01 Part #01

Math 25 and Maple 3 + 4;

MA 1128: Lecture 02 1/22/2018

Divisibility Rules and Their Explanations

Microsoft Access XP (2002) Queries

Importing source database objects from a database

Access Intermediate

Getting Information from a Table

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Math 1191 Mathematica Introduction

Programming for Engineers Introduction to C

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

GEO 425: SPRING 2012 LAB 9: Introduction to Postgresql and SQL

Skills Exam Objective Objective Number. Creating crosstab queries Create a crosstab query

Programming with Python

Using Word 2000 for Mail Merge

CIS 100 Databases in Excel Creating, Sorting, Querying a Table and Nesting Functions

Step 1: Create a totals query to show the total cost price and total sale price of the wine for each supplier.

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object.

I KNOW HOW TO PROGRAM IN SAS HOW DO I NAVIGATE SAS ENTERPRISE GUIDE?

Microsoft Access: Let s create the tblperson. Today we are going to use advanced properties for the table fields and use a Query.

Introduction to Access 97/2000

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

EXCEL 2010 BASICS JOUR 772 & 472 / Ira Chinoy

Create and Modify Queries 7

ITEC 101 LAB 9 USING A DATABASE: Tables and Queries

Prophet 21 World Wide User Group Webinars. Barry Hallman. SQL Queries & Views. (Basic Skill Level)

LABORATORY. 16 Databases OBJECTIVE REFERENCES. Write simple SQL queries using the Simple SQL app.

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

12B. Laboratory. Databases. Objective. References. Write simple SQL queries using the Simple SQL applet.

Introducing Databases

Creating Accessible PDFs

MIS0855: Data Science In-Class Exercise for Mar Creating Interactive Dashboards

Sample A2J Guided Interview & HotDocs Template Exercise

My First iphone App. 1. Tutorial Overview

MS Office 2016 Excel Pivot Tables - notes

Simple sets of data can be expressed in a simple table, much like a

How & Why We Subnet Lab Workbook

chapter 2 G ETTING I NFORMATION FROM A TABLE

ITS Training Class Charts and PivotTables Using Excel 2007

Create Forms 5. KEY TERMS Blank Form tool common filters filter filter by form Form Design button Form tool Form Wizard Themes LESSON SKILL MATRIX

Using Word 2003 for Mail Merge

Chapter 2 The SAS Environment

PowerPoint Presentation to Accompany GO! All In One. Chapter 13

Jump Right In! Essential Computer Skills Using Microsoft 2013 By Andrews, Dark, and West

(Refer Slide Time 3:31)

Using Microsoft Access

Lesson 14 Transcript: Triggers

NON-CALCULATOR ARITHMETIC

CSCI 201 Lab #11 Prof. Jeffrey Miller 1/23. Lab #11 CSCI 201. Title MySQL Installation. Lecture Topics Emphasized Databases

F.O.R.M.S. USER GUIDE

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

M I C R O S O F T A C C E S S : P A R T 2 G E T T I N G I N F O R M A T I O N O U T O F Y O U R D A T A

UAccess ANALYTICS. Fundamentals of Reporting. updated v.1.00

Learning Worksheet Fundamentals

Graded Project. Integrating Word with Access and Excel

As your databases continue to evolve, you will need to incorporate advanced queries and reports. This chapter addresses how to create and use action

Exercise #6: Sales by Item Year over Year Report

Static query Switch to a dynamic query Hints and Tips Support... 12

Pubzilla Online Pub Database 18 th October 2011

Part 1. Creating an Array of Controls or Indicators

Stamina Software Pty Ltd. TRAINING MANUAL Viságe BIT VIEWER

Site Owners: Cascade Basics. May 2017

Dynamics ODBC REFERENCE Release 5.5a

Query. Training and Participation Guide Financials 9.2

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Laboratory Exercise #0

Working with Data and Charts

Computer Science 236 Fall Nov. 11, 2010

MIS 0855 Data Science (Section 006) Fall 2017 In-Class Exercise (Day 15) Creating Interactive Dashboards

Reference Guide. Adding a Generic File Store - Importing From a Local or Network ShipWorks Page 1 of 21

Make Courses User Guide. version

Transcription:

More SQL queries In this exercise you will practice some more SQL queries. First let s practice queries on a single table. 1. Download SQL_practice.accdb to your I: drive. Launch Access 2016 and open the SQL_practice database by clicking Open Other Files ->Computer->Browse - >click on your I drive in the left pane -> Select SQL_practice.accdb->Click Open You may get a Security Warning pop up at the top of your screen indicating some active content has been disabled.. Click Enable content to enable this content. If another Security Warning Do you want to make this file a Trusted Document? appears, check Do not ask me again for network files and click Yes. 2. Open the table Store and view it in Design View. Each record represents a branch of Fearless Fred s Fine Foods. Note that the StoreNumber is the primary key. The Address field is the address of the store, not the manager s address. 3. Change to Datasheet view and examine the contents of the Store table. 4. Click the Create tab, and then Query Design in Queries Group. Add the Store table to the query and close the table selection box. Save the query as StoreQuery. 5. Use the View menu to select SQL View. You should see the default query, SELECT from store. Now try to view this query in Datasheet view. You will see an SQL error message telling you that the query must have at least one destination field, whatever that is! 6. Go back to SQL view and edit the query to say Select address from Store;. The capitalization here does not matter, nor does it matter whether the SQL query is on a single line. SQL queries can get lengthy, so we run them from line to line and use a semicolon to mark the end of the query. Page 1

7. Now switch to Datasheet view. You should now see the list of all the store addresses. Notice that the columns we want to appear in our query results go in the select line of our query. The word address was a column from our Store table. 8. Switch back to SQL view and change the query to Select address, city from store. Be sure to put a comma between the two fields address and city. Switch back to Datasheet view. You will now see the address and the city. Note that the address comes before city in the query results, even though City comes before address in the table definition. The Order Clause 9. Often we want to be able to control the order in which records are displayed. In SQL view, add the following to the end of the query: order by city. Be sure to put these new words in front of the semicolon, which ends the query. For best readability, we like to put each SQL clause on a separate line with the SQL key words capitalized, like this: 10. Switch to Datasheet view. You will now see the stores arranged in alphabetical order by city. 11. Switch back to SQL view, and change order by city to order by sales. In datasheet view, you will see that the order has changed. Re-open the Store table window to check that the stores are now listed in increasing order of sales. This shows that we can use a field that is not displayed in the query to order the results. 12. What if we want the list in decreasing order of sales? Add the keyword desc (for descending ) after order by sales. Don t forget about the semicolon! You should now see all the stores in reverse order. Modifiers 13. Suppose our board of directors wants a list of all the cities where Fearless Fred has stores. Change the query so you only see the city names. Switch to datasheet view and you will see that Athens and Atlanta are each listed twice. To fix this, change select city to select distinct city. 14. Try switching back to Datasheet view. Oops! Access is unhappy. It appears that showing each city only once contradicts the requirement that stores be displayed in order of Page 2

sales. Eliminate the offending order by clause. Don t lose track of the semicolon! You should now see a result with three rows representing the different cities. Where Clauses 15. In the last exercise we learned to add a WHERE clause to limit our query results to certain records. Start with a default, blank SQL Query window. Go to Create, Query Design, don t add any tables, then change the view to SQL View. The only thing you should see in the SQL Query window is SELECT; Try these queries save each query below as query17a, query17b, etc: SELECT City, Manager FROM Store WHERE StoreNumber =13 When you switch to datasheet view, you should get the result: Compose a query to find the city and address of the store whose manager is Laurie Smith. You must put single quotes around any field value of type text. Compose a query to find the city and address of the store whose manager is Clint Eastwood. Compose a query to find the managers of all stores whose sales are less than $200,000. Don t use the dollar sign or comma in the query, since the type of the Sales field is just a plain number. Compose a query to find the managers of all stores whose sales are greater than $200,000. This and the previous query should be disjoint, and together should include all the managers. Compose a query to list the addresses of all the stores in Athens. Compose a query to list the addresses of all the stores in Athens whose sales are over $200,000. For this you will need the AND keyword. The WHERE condition will look something like (condition to check city) AND (condition to check sales) The AND keyword tells Access that only those records satisfying both of the conditions should be displayed. Page 3

Compose a query to list the addresses of all the stores that are either located in Athens or whose sales are over $200,000. For this you will need the OR keyword. Your query in datasheet view should look something like this: REMEMBER, SAVE EACH QUERY BELOW SEPARATELY EACH WITH DIFFERENT NAMES. Functions 15. Open up the Stores table. By inspecting this table, we can easily find the highest annual sales for any of Fred s stores. But what if the table contained hundreds or thousands of records? SQL provides a function, MAX(), to handle this. Remember, start with a default, blank SQL Query window. Go to Create, Query Design, don t add any tables, then change the view to SQL View. The only thing you should see in the SQL Query window is SELECT; Try this query: Select MAX(Sales) from Store. In Datasheet view, you should see the single value 514780. 16. Compose a query to find the lowest annual sales of any of Fred s stores using the SQL function MIN(). 17. OK, but we would also like to know which store has the highest sales, not just the value. Since we know the max value is 514780, we can put the expression into a WHERE clause: WHERE sales = 514780 Try this out to compose a query for the city and address of the store that has the highest sales. Subqueries It doesn t make sense that we have to do the dirty work like in step #20 above. Usually we try to avoid copying numbers by hand, which can lead to errors. Instead, we can reproduce the query that found the number as a subquery. Try this query: Select city, address From Store WHERE sales=(select max(sales) from store); 18. Compose a query for the city and address of the store with the least sales. Page 4

19. Compose a query for the city and address of all the stores whose sales were more than half of the maximum amount. (HINT: To get half the maximum, try multiplying 0.5 times the max function) Open the Delivery table, which records deliveries made to each of Fred s stores. In a real database, this information would be broken down much further, giving the specific food items and quantities delivered to each store. You should always check to make sure your SQL field names match the fields from the table. Keep that in mind as you work the following queries below. Remember, save each query separately with different names (query24a, etc). 20. In the Database window, create a new query named query24a (remember, start with a default, blank SQL query that only says SELECT; ). Use SQL View to compose the following queries: a. Show the DeliveryDate and Amount of the deliveries, ordered by the DeliveryDate (Hint: Did you forget how to order by? Check out earlier in the lab). b. Show the dates and amounts of the deliveries, ordered by the date in descending order. (Did you forget how to descend? Check out earlier in the lab). c. Show the dates and amounts of deliveries to store number 22. d. Show the dates, store numbers, and amounts of deliveries on October 10, 2007. (To check the date, you will need to use Access s special notation #10/10/2007# to denote this). e. List the dates, amounts, and store numbers of all deliveries greater than $20,000 (remember to leave off the dollar sign and comma). f. Find the maximum delivery amount. g. Find the date and store number of the store that received the largest delivery (you ll need to use the subquery method we just learned). 21. Now we will show you how to make queries that combine data from both tables. The diagram here shows how the two tables are linked via their StoreNumber fields. We can use data from both tables, simply by including the extra condition AND Store.StoreNumber=Delivery.StoreNumber in each query. Notice that we need to say Store.StoreNumber or Delivery.StoreNumber because just StoreNumber by itself is ambiguous (Access would not know which column from which table you re talking about if you said just StoreNumber). Also, we will need to include both table names in the FROM clause. Page 5

22. Create a new Query called query26a starting with just the SELECT; default query and try the following saving each query with a different name. a. SELECT City, Manager FROM Store, Delivery WHERE DeliveryDate = #10/10/2007# AND Store.StoreNumber=Delivery.StoreNumber (remember the bold section above links the two tables like using a Relationship except using SQL. This is necessary to be able to correctly find data from two separate tables) b. Find the dates and amounts of all deliveries made to the store managed by Clint Eastwood. (Remember to put quotes around the name). c. Find all the dates on which deliveries were made to stores in Athens. You should get this: d. List the city, address, and date of all deliveries over $20,000. Congrats! You ve reached the end of the database lab. You should be well prepared to write your own queries for quizzes and exams. Page 6