Vous êtes sur la page 1sur 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
!. What is the price of each product
". Which products are most e#pense and which are least e#pensive $%ist all
products and their prices in descending order.&
'. Which products have been discontinued
(. Which current products cost less than )1*
+. Which current products cost between )1* and )!* $two variations&
,. Which current products are in danger of going out of stoc- $.uantit/ in stoc-
is less than the 0uantit/ on order&
1. 2ow man/ current products and how man/ discontinued products are there
From two tables: Products and Categories.
1. What products e#ist in each categor/
!. Same 0uestion3 but alphabeti4e the list b/ categor/ and product.
". 2ow man/ products are there in each categor/
'. What is the average price in each categor/ $sort from highest to lowest&
From four tables: Products, OrderDetails, Orders and Customers.
1. (Customer preferences) %ist all of the products each customer has ever
ordered 5 sorted b/ customer and product.
!. (High-ariet!-to-low-ariet! customers) 2ow man/ different -inds of
products each customer has ordered 5 sorted b/ 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.
'. ("noice) What is the e#tension $price # 0uantit/& of each product on each
order for each customer $6 calculated field is needed here. 6lso include the
order date.&
(. (#mount due) What is7was the total amount owed on each order
+. ($est customers) What is the total amount purchased b/ each customer $%ist
in descending order.&
,. (%ost-widel!-popular-to-least-widel!-popular products) What products have
been purchased b/ the most customers $%ist in descending order&
&olutions
8o see how to answer a 0uestion above3
1. 9op/ the S.% statement below $one at a time&.
!. Paste it into the S.% window in /our 0uer/ designer.
". 8hen switch to the :esign view of the 0uer/.
From a single table: Products.
1 What products are in Northwinds
Products table
S;%;98 Products.ProductName F<=> Products?
! What is the price of each
product
S;%;98 Products.ProductName3 Products.UnitPrice F<=>
Products?
" Which products are most e#pense
and which are least e#pensive
$%ist all products and their prices
in descending order.&
S;%;98 Products.ProductName3 Products.UnitPrice F<=>
Products
=<:;< @A Products.UnitPrice :;S9?
' Which products have been
discontinued
S;%;98 Products.ProductName3 Products.UnitPrice3
Products.:iscontinued
F<=> Products
W2;<; $$$Products.:iscontinued&B8rue&&
=<:;< @A Products.UnitPrice :;S9?
( Which current products cost less
than )1*
S;%;98 Products.ProductName3 Products.UnitPrice3
Products.:iscontinued
F<=> Products
W2;<; $$$Products.UnitPrice&C1*& 6N:
$$Products.:iscontinued&BFalse&&
=<:;< @A Products.UnitPrice :;S9?
+ Which current products cost
between )1* and )!* $two
variations&
S;%;98 Products.ProductName3 Products.UnitPrice3
Products.:iscontinued
F<=> Products
W2;<; $$$Products.UnitPrice&DB1* 6nd
$Products.UnitPrice&CB!*& 6N:
$$Products.:iscontinued&BFalse&&
=<:;< @A Products.UnitPrice :;S9?
E or E
S;%;98 Products.ProductName3 Products.UnitPrice3
Products.:iscontinued
F<=> Products
W2;<; $$$Products.UnitPrice& @etween 1* 6nd !*& 6N:
$$Products.:iscontinued&BFalse&&
=<:;< @A Products.UnitPrice :;S9?
, Which current products are in
danger of going out of stoc-
$.uantit/ in stoc- is less than the
0uantit/ on order&
S;%;98 Products.ProductName3 Products.:iscontinued3
Products.UnitsFnStoc-
F<=> Products
W2;<; $$$Products.:iscontinued&BFalse& 6N:
$$Products.UnitsFnStoc-&CGUnits=n=rderH&&?
1 2ow man/ current and
discontinued products are there
S;%;98 9ount$Products.ProductName& 6S
9ount=fProductName3 Products.:iscontinued
F<=> Products
I<=UP @A Products.:iscontinued?
From two tables: Products and Categories.
1 What products e#ist in each
categor/
S;%;98 9ategories.9ategor/Name3 Products.ProductName
F<=> 9ategories FNN;< J=FN Products =N
9ategories.9ategor/F: B Products.9ategor/F:?
! Same 0uestion3 but alphabeti4e
the list b/ categor/ and product.
S;%;98 9ategories.9ategor/Name3 Products.ProductName
F<=> 9ategories FNN;< J=FN Products =N
9ategories.9ategor/F: B Products.9ategor/F:
=<:;< @A 9ategories.9ategor/Name3 Products.ProductName?
" 2ow man/ products are there in
each categor/
S;%;98 9ategories.9ategor/Name3
9ount$Products.ProductName& 6S 9ount=fProductName
F<=> 9ategories FNN;< J=FN Products =N
9ategories.9ategor/F: B Products.9ategor/F:
I<=UP @A 9ategories.9ategor/Name
=<:;< @A 9ategories.9ategor/Name3
9ount$Products.ProductName&?
' What is the average price in each
categor/ $sort from highest to
lowest&
S;%;98 9ategories.9ategor/Name3 6vg$Products.UnitPrice& 6S
6vg=fUnitPrice
F<=> 9ategories FNN;< J=FN Products =N
9ategories.9ategor/F: B Products.9ategor/F:
I<=UP @A 9ategories.9ategor/Name
=<:;< @A 9ategories.9ategor/Name3 6vg$Products.UnitPrice&?
From four tables: Products, OrderDetails, Orders and Customers.
1 (Customer preferences)
%ist all of the products
each customer has ever
ordered 5 sorted b/
customer and product.
S;%;98 9ustomers.9ompan/Name3 Products.ProductName
F<=> $9ustomers FNN;< J=FN =rders =N 9ustomers.9ustomerF: B =rders.9ustomerF:&
FNN;< J=FN $Products FNN;< J=FN G=rder :etailsH =N Products.ProductF: B G=rder
:etailsH.ProductF:& =N =rders.=rderF: B G=rder :etailsH.=rderF:
=<:;< @A 9ustomers.9ompan/Name3 Products.ProductName?
! (High-ariet!-to-low-
ariet! customers) 2ow
man/ -inds of products
each customer has
ordered 5 sorted b/
product Sort high to
low.
S;%;98 9ustomers.9ompan/Name3 9ount$Products.ProductName& 6S
9ount=fProductName
F<=> $9ustomers FNN;< J=FN =rders =N 9ustomers.9ustomerF: B =rders.9ustomerF:&
FNN;< J=FN $Products FNN;< J=FN G=rder :etailsH =N Products.ProductF: B G=rder
:etailsH.ProductF:& =N =rders.=rderF: B G=rder :etailsH.=rderF:
I<=UP @A 9ustomers.9ompan/Name
=<:;< @A 9ount$Products.ProductName& :;S9?
" (High-end-to-low-end
customers) What is the
average price of the
products each customer
has ordered Sort high to
low.
S;%;98 9ustomers.9ompan/Name3 6vg$Products.UnitPrice& 6S 6vg=fUnitPrice
F<=> $9ustomers FNN;< J=FN =rders =N 9ustomers.9ustomerF: B =rders.9ustomerF:&
FNN;< J=FN $Products FNN;< J=FN G=rder :etailsH =N Products.ProductF: B G=rder
:etailsH.ProductF:& =N =rders.=rderF: B G=rder :etailsH.=rderF:
I<=UP @A 9ustomers.9ompan/Name
=<:;< @A 6vg$Products.UnitPrice& :;S9?
' ("noice) What is the
e#tension $price #
0uantit/& of each product
on each order for each
customer $6 calculated
field is needed here. 6lso
include the order date.&
S;%;98 9ustomers.9ompan/Name3 =rders.=rderF:3 =rders.=rder:ate3
Products.ProductName3 G=rder :etailsH.UnitPrice3 G=rder :etailsH..uantit/3 G=rder :etailsHK
GUnitPriceHLG=rder :etailsHKG.uantit/H 6S ;#tension
F<=> $9ustomers FNN;< J=FN =rders =N 9ustomers.9ustomerF: B =rders.9ustomerF:&
FNN;< J=FN $Products FNN;< J=FN G=rder :etailsH =N Products.ProductF: B G=rder
:etailsH.ProductF:& =N =rders.=rderF: B G=rder :etailsH.=rderF:
=<:;< @A 9ustomers.9ompan/Name3 =rders.=rder:ate3 Products.ProductName?
( (#mount due) What
is7was the total amount
owed on each order
S;%;98 9ustomers.9ompan/Name3 =rders.=rderF:3 =rders.=rder:ate3 Sum$G=rder
:etailsHKGUnitPriceHLG=rder :etailsHKG.uantit/H& 6S ;#tension
F<=> $9ustomers FNN;< J=FN =rders =N 9ustomers.9ustomerF: B =rders.9ustomerF:&
FNN;< J=FN $Products FNN;< J=FN G=rder :etailsH =N Products.ProductF: B G=rder
:etailsH.ProductF:& =N =rders.=rderF: B G=rder :etailsH.=rderF:
I<=UP @A 9ustomers.9ompan/Name3 =rders.=rderF:3 =rders.=rder:ate
=<:;< @A 9ustomers.9ompan/Name3 =rders.=rder:ate?
+ ($est customers) What is
the total amount
purchased b/ each
customer $%ist in
descending order&
S;%;98 9ustomers.9ompan/Name3 Sum$G=rder :etailsHKGUnitPriceHLG=rder :etailsHK
G.uantit/H& 6S ;#tension
F<=> $9ustomers FNN;< J=FN =rders =N 9ustomers.9ustomerF: B =rders.9ustomerF:&
FNN;< J=FN $Products FNN;< J=FN G=rder :etailsH =N Products.ProductF: B G=rder
:etailsH.ProductF:& =N =rders.=rderF: B G=rder :etailsH.=rderF:
I<=UP @A 9ustomers.9ompan/Name
=<:;< @A 9ustomers.9ompan/Name?
,
(%ost-popular-to-
least-popular
products) What
products have been
purchased b/ the most
customers $%ist in
descending order&
S;%;98 Products.ProductName3 9ount$9ustomers.9ompan/Name& 6S
9ount=f9ompan/Name
F<=> $9ustomers FNN;< J=FN =rders =N 9ustomers.9ustomerF: B =rders.9ustomerF:&
FNN;< J=FN $Products FNN;< J=FN G=rder :etailsH =N Products.ProductF: B G=rder
:etailsH.ProductF:& =N =rders.=rderF: B G=rder :etailsH.=rderF:
I<=UP @A Products.ProductName
=<:;< @A 9ount$9ustomers.9ompan/Name& :;S9?

Vous aimerez peut-être aussi