0% found this document useful (0 votes)
145 views4 pages

Query Skills Demo Using Northwind Database

The document demonstrates how to perform basic queries on the Northwind sample database using SQL. It provides 27 example queries across 4 tables (Products, Categories, OrderDetails, Orders, Customers) to return requested data like product names and prices, category breakdowns, customer order histories, and popular products. Accompanying SQL solutions are provided step-by-step to model answering each type of query.

Uploaded by

Bhanu Kanna
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
145 views4 pages

Query Skills Demo Using Northwind Database

The document demonstrates how to perform basic queries on the Northwind sample database using SQL. It provides 27 example queries across 4 tables (Products, Categories, OrderDetails, Orders, Customers) to return requested data like product names and prices, category breakdowns, customer order histories, and popular products. Accompanying SQL solutions are provided step-by-step to model answering each type of query.

Uploaded by

Bhanu Kanna
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 4

Demo of Basic Query Skills

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 less than $10?

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?

From two tables: Products and Categories.


1 2 What products exist in each category? Same question, but alphabetize the list by category and product. How many products are there in each category? SELECT Categories.CategoryName, Products.ProductName FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID; SELECT Categories.CategoryName, Products.ProductName FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID ORDER BY Categories.CategoryName, Products.ProductName; SELECT Categories.CategoryName, Count(Products.ProductName) AS CountOfProductName FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID GROUP BY Categories.CategoryName ORDER BY Categories.CategoryName, Count(Products.ProductName); SELECT Categories.CategoryName, Avg(Products.UnitPrice) AS AvgOfUnitPrice FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID GROUP BY Categories.CategoryName ORDER BY Categories.CategoryName, Avg(Products.UnitPrice);

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. (High-variety-to-lowvariety customers) How many kinds of products each customer has ordered sorted by product? Sort high to low. (High-end-to-low-end customers) What is the average price of the products each customer has ordered? Sort high to low. (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.) (Amount due) What is/was the total amount owed on each order? SELECT Customers.CompanyName, Products.ProductName FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN (Products INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID) ON Orders.OrderID = [Order Details].OrderID ORDER BY Customers.CompanyName, Products.ProductName; SELECT Customers.CompanyName, Count(Products.ProductName) AS CountOfProductName FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN (Products INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID) ON Orders.OrderID = [Order Details].OrderID GROUP BY Customers.CompanyName ORDER BY Count(Products.ProductName) DESC; SELECT Customers.CompanyName, Avg(Products.UnitPrice) AS AvgOfUnitPrice FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN (Products INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID) ON Orders.OrderID = [Order Details].OrderID GROUP BY Customers.CompanyName ORDER BY Avg(Products.UnitPrice) DESC; SELECT Customers.CompanyName, Orders.OrderID, Orders.OrderDate, Products.ProductName, [Order Details].UnitPrice, [Order Details].Quantity, [Order Details]! [UnitPrice]*[Order Details]![Quantity] AS Extension FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN (Products INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID) ON Orders.OrderID = [Order Details].OrderID ORDER BY Customers.CompanyName, Orders.OrderDate, Products.ProductName; SELECT Customers.CompanyName, Orders.OrderID, Orders.OrderDate, Sum([Order Details]! [UnitPrice]*[Order Details]![Quantity]) AS Extension FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN (Products INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID) ON Orders.OrderID = [Order Details].OrderID GROUP BY Customers.CompanyName, Orders.OrderID, Orders.OrderDate ORDER BY Customers.CompanyName, Orders.OrderDate; SELECT Customers.CompanyName, Sum([Order Details]![UnitPrice]*[Order Details]! [Quantity]) AS Extension FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN (Products INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID) ON Orders.OrderID = [Order Details].OrderID GROUP BY Customers.CompanyName ORDER BY Customers.CompanyName; SELECT Products.ProductName, Count(Customers.CompanyName) AS CountOfCompanyName FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN (Products INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID) ON Orders.OrderID = [Order Details].OrderID GROUP BY Products.ProductName ORDER BY Count(Customers.CompanyName) DESC;

(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)

You might also like