ACC306 Chapter 4 Video 4
Introduction to Writing Queries
- Learning goals for creating database queries based on provided data queries.
Overview of the Database
- Data Source: Chapter four relational database.
- Tables Included:
- Customer Table:
- Customer ID (Primary Key)
- Customer Name
- Street
- City
- State
- Inventory Table:
- Inventory ID
- Description
- Other Attributes
- Sales Table:
- Sales Invoice ID (Primary Key)
- Sales Date
- Salesperson
- Customer ID (Foreign Key)
- Linking Table:
- Cross-relationship between Sales and Inventory through Sales Invoice ID and Item ID.
Exploring Database Views
- External View vs. Internal View:
- External Level: The table as seen by the user, displaying basic data entries.
- Internal Level: Design view shows details including primary keys (e.g., Customer ID with a key icon indicates it's a primary key).
Conceptual Level
- Relationships Among Tables:
- Customer table is linked to the Sales table via Customer ID.
- Inventory table and Sales table are interlinked through the linking sales inventory table which contains two primary keys:
- Sales Invoice ID
- Item ID
Creating Queries
Query 1: Invoice Details for Lola Doyle
- Objective: List Invoice Number, Sales Date, and Salesperson.
- Steps to Create Query:
- Open Create menu.
- Select Query Design
- Pop-up appears to select tables; choose Sales and Customer tables.
- Select fields to display:
- Sales Invoice ID (as Invoice Number)
- Sales Date
- Salesperson
- Customer Name
- Set criteria to filter results for "Lola Doyle" in criteria row of Customer Name.
- Running the Query: Execute to see results specific to Lola Doyle.
- Hiding Customer Name: Return to Design view and uncheck the Show option for Customer Name if desired.
- Saving the Query: Name it appropriately (e.g., Query 1).
Query 2: Refrigerator Sales in October
- Objective: List Sales Date, Description, and Quantity for transactions involving refrigerators sold in October.
- Steps to Create Query:
- Open Query Design and select the required tables (Sales, Inventory, Sales Inventory).
- Add the following fields to display:
- Sales Date (from Sales Table)
- Description (from Inventory Table)
- Quantity (from Sales Inventory Table)
- Set criteria for Description to "Refrigerator".
- Set criteria for Sales Date to filter transactions between October 1 to October 31, 2021:
- Use between syntax with hash symbols (e.g., #10/01/2021#) and conditions for date range.
- Running the Query: Execute to see the list of refrigerators sold with dates and quantities.
- Saving the Query: Save after checking results.
- Objective: Determine total number of refrigerators sold in October.
- Steps to Create Query:
- Open a new query and select the same tables as Query 2.
- Fields to include are:
- Sales Date
- Description
- Quantity
- Set criteria for Description as "Refrigerator" and Sales Date (October range).
- Include total row, set function to Sum for Quantity to aggregate sales.
- Running the Query: Execute to get the total quantity of refrigerators sold.
- Example Output: 6 refrigerators sold.
Query 4: Inventory Types Sold in October
- Objective: List items with a quantity of two or more sold in October.
- Steps to Create Query:
- Create a new query and select required tables (Sales, Sales Inventory, Inventory).
- Include fields:
- Description
- Quantity
- Sales Date
- Set criteria for Quantity:
- Greater than or equal to 2.
- Set criteria for Sales Date (October dates).
- Running the Query: Execute to see results for items sold in specified quantity.
Query 5: Sales Date and Invoice Total
- Objective: List Sales Date and calculate invoice total for each invoice in October.
- Steps to Create Query:
- Create a new query and add necessary tables (Sales, Sales Inventory).
- Select fields to include:
- Calculating Invoice Total: Create an expression to calculate total:
- Formula: Sum of (Quantity * Price)
- Ensure to properly reference the fields in the Sales Inventory table.
- Set criteria for Sales Date range (October).
- Running the Query: Execute and verify calculations of invoice totals. Sort results as necessary.
- Sorting and Filtering: Can sort by descending order of invoice totals for the highest first.
Additional Query Functions
- Use of Aggregate Functions:
SUM() for totals,COUNT() for number of entries.
- Condition Handling: Ensure correct spelling in criteria to avoid errors in queries.
Conclusion
- Review of various types of queries to extract different information from the database.
- Importance of understanding relationships between tables for effective querying in databases.