Chapter 4: reating Relational Database Queries: A Comprehensive Guide
Creating Relational Database Queries
Query Views: Design View vs. SQL View
Query Design View
This is Microsoft's specific implementation of a Query-By-Example (QBE) query interface within Access.
QBE interfaces are generally not standardized across different database products, meaning skills learned in one QBE interface may not directly transfer to another database system.
SQL View
Most conventions of the SQL (Structured Query Language) language are standardized across various database products (e.g., MySQL, PostgreSQL, Oracle, SQL Server).
This standardization is crucial as it allows users to apply their SQL query skills to database products beyond Microsoft Access, making them highly transferable and valuable.
Foundational SQL - Structured Query Language Concepts
SELECT Clause: Used to select data (columns) from a database.
Example:
SELECT * FROM Customers;This retrieves all columns (*) from theCustomerstable.
INNER JOIN: Used to combine rows from two or more tables based on a related column between them.
It selects records that have matching values in both tables, effectively retrieving only the rows where the join condition is met.
WHERE Clause: Used to filter records based on specified conditions.
It extracts only those records that fulfill a given condition.
The
WHEREclause can be combined with logical operators:AND,OR,NOT.AND Operator: Displays a record only if all the conditions separated by
ANDareTRUE.OR Operator: Displays a record if any of the conditions separated by
ORareTRUE.NOT Operator: Displays a record if the condition is
FALSE.
BETWEEN Operator: Used to select values within a given range.
The values can be numbers, text, or dates.
It is inclusive, meaning the start and end values of the range are included in the result.
Advanced SQL Concepts
GROUP BY Clause: Groups rows that have the same values in one or more specified columns into summary rows.
It is often used in conjunction with aggregate functions (e.g.,
SUM(),COUNT(),MAX(),MIN(),AVG()) to perform calculations on each group.Syntax:
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s)
Illustrative Queries
Query 1: Listing Invoice Details for a Specific Customer (Design View Example)
Objective: To list the invoice numbers, sale dates (
SaleDate), and salespeople (SalesPerson) for all sales made to a specific customer, Lola Doyle.Underlying Logic: This would involve selecting specific columns from the
Salestable and potentially joining with aCustomerstable if customer name is not directly inSales, then filtering records whereCustomerName(orCustomerIDlinked to Lola Doyle) matches 'Lola Doyle'.
Query 2: Listing Refrigerator Sales in October (SQL View Example)
Objective: To list the sale date (
SaleDate), description of the item (Description), and quantity (Quantity) for each transaction where refrigerators were sold in the month of October.Skills Demonstrated: Use of
SELECTfor specific columns,FROMfor relevant tables (Sales,Sales_Inventory,Inventory), andWHEREconditions combining item description and a date range usingBETWEEN.Example SQL structure:
SELECT Sales.SaleDate, Inventory.Description, Sales_Inventory.Quantity FROM Sales INNER JOIN Sales_Inventory ON Sales.SalesInvoiceID = Sales_Inventory.SalesInvoiceID INNER JOIN Inventory ON Sales_Inventory.ItemID = Inventory.ItemID WHERE Inventory.Description = 'Refrigerator' AND Sales.SaleDate BETWEEN #10/1/2021# AND #10/31/2021#;(Date format may vary by database).
Query 3: Calculating Total Refrigerators Sold in October (Design View Example)
Objective: Builds upon Query 2. The sales manager wants to know the total count (summation of quantity) of refrigerators sold specifically in October.
Skills Demonstrated: This involves applying an aggregate function, specifically
SUM(), to theQuantityfield, after filtering for refrigerators and the month of October.
Query 4: Listing Inventory Types with High Sales Volume in October (Design View Example)
Objective: To list the item description (
Description) and the sum of quantities (SumOfQuantity) for inventory types that sold or more units in total during the month of October.Skills Demonstrated: Use of
GROUP BYon theDescriptioncolumn to group sales by item type,SUM()onQuantity, and then filtering these aggregated results (or groups) using aHAVINGclause (e.g.,HAVING SUM(Quantity) >= 2).
Query 5: Listing Invoice Total for Each October Invoice (SQL View Example)
Objective: To list the sale date (
SaleDate) and the calculatedInvoiceTotalfor each invoice generated in October.Data Model Implied (from provided tables):
Customers Table:
CustomerID,CustomerName,Street,City,StateSales Table:
SalesInvoiceID,SaleDate,SalesPerson,CustomerIDSales_Inventory Table (Junction Table):
SalesInvoiceID,ItemID,Quantity,SoldPriceInventory Table:
ItemID,Description,Color,VendorID,QuantityOnHand,ListPrice
InvoiceTotal Calculation: The
InvoiceTotalis derived from the product ofQuantityandSoldPricefor each item within an invoice, and then summing these products for all items in that invoice:Skills Demonstrated: This requires selecting
SaleDateand the calculatedInvoiceTotal. It involves joiningSalesandSales_Inventorytables, applying an aggregate function (SUM) to calculate the total price per invoice, and then grouping bySalesInvoiceIDandSaleDateto get an individual total for each invoice. The filtering condition for October dates would be applied toSaleDate.Example SQL structure:
SELECT S.SaleDate, SUM(SI.Quantity * SI.SoldPrice) AS InvoiceTotal FROM Sales AS S INNER JOIN Sales_Inventory AS SI ON S.SalesInvoiceID = SI.SalesInvoiceID WHERE S.SaleDate BETWEEN #10/1/2021# AND #10/31/2021# GROUP BY S.SalesInvoiceID, S.SaleDate;