Query Skills Demo Using Northwind Database
Query Skills Demo Using Northwind Database
Using the Northwind.accdb database Solutions are provided on the following pages. From a single table: Products. 1. What products are in Northwinds Products table? 2. What is the price of each product? 3. Which products are most expense and which are least expensive? (List all products and their prices in descending order.) 4. Which products have been discontinued? 5. Which current products cost less than $10? 6. Which current products cost between $10 and $20? (two variations) 7. Which current products are in danger of going out of stock? (Quantity in stock is less than the quantity on order) 8. How many current products and how many discontinued products are there? From two tables: Products and Categories. 1. 2. 3. 4. What products exist in each category? Same question, but alphabetize the list by category and product. How many products are there in each category? What is the average price in each category? (sort from highest to lowest)
From four tables: Products, OrderDetails, Orders and Customers. 1. (Customer preferences) List all of the products each customer has ever ordered sorted by customer and product. 2. (High-variety-to-low-variety customers) How many different kinds of products each customer has ordered sorted by product? Sort high to low. 3. (High-end-to-low-end customers) What is the average price of the products each customer has ordered? Sort high to low. 4. (Invoice) What is the extension (price x quantity) of each product on each order for each customer? (A calculated field is needed here. Also include the order date.) 5. (Amount due) What is/was the total amount owed on each order? 6. (Best customers) What is the total amount purchased by each customer? (List in descending order.) 7. (Most-widely-popular-to-least-widely-popular products) What products have been purchased by the most customers? (List in descending order)
Solutions To see how to answer a question above, 1. Copy the SQL statement below (one at a time). 2. Paste it into the SQL window in your query designer. 3. Then switch to the Design view of the query. From a single table: Products.
1 2 3 What products are in Northwinds Products table? What is the price of each product? Which products are most expense and which are least expensive? (List all products and their prices in descending order.) Which products have been discontinued? SELECT Products.ProductName FROM Products; SELECT Products.ProductName, Products.UnitPrice FROM Products; SELECT Products.ProductName, Products.UnitPrice FROM Products ORDER BY Products.UnitPrice DESC; SELECT Products.ProductName, Products.UnitPrice, Products.Discontinued FROM Products WHERE (((Products.Discontinued)=True)) ORDER BY Products.UnitPrice DESC; SELECT Products.ProductName, Products.UnitPrice, Products.Discontinued FROM Products WHERE (((Products.UnitPrice)<10) AND ((Products.Discontinued)=False)) ORDER BY Products.UnitPrice DESC; SELECT Products.ProductName, Products.UnitPrice, Products.Discontinued FROM Products WHERE (((Products.UnitPrice)>=10 And (Products.UnitPrice)<=20) AND ((Products.Discontinued)=False)) ORDER BY Products.UnitPrice DESC; or SELECT Products.ProductName, Products.UnitPrice, Products.Discontinued FROM Products WHERE (((Products.UnitPrice) Between 10 And 20) AND ((Products.Discontinued)=False)) ORDER BY Products.UnitPrice DESC; SELECT Products.ProductName, Products.Discontinued, Products.UnitsInStock FROM Products WHERE (((Products.Discontinued)=False) AND ((Products.UnitsInStock)<[UnitsOnOrder])); SELECT Count(Products.ProductName) AS CountOfProductName, Products.Discontinued FROM Products GROUP BY Products.Discontinued;
Which current products cost between $10 and $20? (two variations)
Which current products are in danger of going out of stock? (Quantity in stock is less than the quantity on order) How many current and discontinued products are there?
What is the average price in each category? (sort from highest to lowest)
(Best customers) What is the total amount purchased by each customer? (List in descending order) (Most-popular-to-leastpopular products) What products have been purchased by the most customers? (List in descending order)