List the item number and description of all bicycles that currently have an auction in progress, ordered by item number.
Solution in XQuery:
{
for $i in doc("items.xml")//item_tuple
where $i/start_date <= current-date()
and $i/end_date >= current-date()
and contains($i/description, "Bicycle")
order by $i/itemno
return
{ $i/itemno }
{ $i/description }
}
Note:
This solution assumes that the current date is 1999-01-31.
Expected Result:
1003
Old Bicycle
1007
Racing Bicycle
The above query returns an element named item_tuple, but its definition does not match the definition of item_tuple in the DTD.
For all bicycles, list the item number, description, and highest bid (if any), ordered by item number.
Solution in XQuery:
{
for $i in doc("items.xml")//item_tuple
let $b := doc("bids.xml")//bid_tuple[itemno = $i/itemno]
where contains($i/description, "Bicycle")
order by $i/itemno
return
{ $i/itemno }
{ $i/description }
{ max($b/bid) }
}
Expected Result:
1001
Red Bicycle
55.0
1003
Old Bicycle
20.0
1007
Racing Bicycle
225
1008
Broken Bicycle
Find cases where a user with a rating worse (alphabetically, greater) than "C" is offering an item with a reserve price of more than 1000.
Solution in XQuery:
{
for $u in doc("users.xml")//user_tuple
for $i in doc("items.xml")//item_tuple
where $u/rating > "C"
and $i/reserve_price > 1000
and $i/offered_by = $u/userid
return
{ $u/name }
{ $u/rating }
{ $i/description }
{ $i/reserve_price }
}
Expected Result:
Dee Linquent
D
Helicopter
50000
List item numbers and descriptions of items that have no bids.
Solution in XQuery:
{
for $i in doc("items.xml")//item_tuple
where empty(doc("bids.xml")//bid_tuple[itemno = $i/itemno])
return
{ $i/itemno }
{ $i/description }
}
Expected Result:
1005
Tennis Racket
1006
Helicopter
1008
Broken Bicycle
For bicycle(s) offered by Tom Jones that have received a bid, list the item number, description, highest bid, and name of the highest bidder, ordered by item number.
Solution in XQuery:
{
for $seller in doc("users.xml")//user_tuple,
$buyer in doc("users.xml")//user_tuple,
$item in doc("items.xml")//item_tuple,
$highbid in doc("bids.xml")//bid_tuple
where $seller/name = "Tom Jones"
and $seller/userid = $item/offered_by
and contains($item/description , "Bicycle")
and $item/itemno = $highbid/itemno
and $highbid/userid = $buyer/userid
and $highbid/bid = max(
doc("bids.xml")//bid_tuple
[itemno = $item/itemno]/bid
)
order by ($item/itemno)
return
{ $item/itemno }
{ $item/description }
{ $highbid/bid }
{ $buyer/name }
}
The above query does several joins, and requires the results in a particular order. If there were no order by clause, results would be reported in document order. If you do not care about the order, you can use the unordered function to inform the query processor that the order of the lists in the for clause is not significant, which means that the tuples can be generated in any order. This can enable better optimization.
Unordered Solution in XQuery:
{
unordered (
for $seller in doc("users.xml")//user_tuple,
$buyer in doc("users.xml")//user_tuple,
$item in doc("items.xml")//item_tuple,
$highbid in doc("bids.xml")//bid_tuple
where $seller/name = "Tom Jones"
and $seller/userid = $item/offered_by
and contains($item/description , "Bicycle")
and $item/itemno = $highbid/itemno
and $highbid/userid = $buyer/userid
and $highbid/bid = max(
doc("bids.xml")//bid_tuple
[itemno = $item/itemno]/bid
)
return
{ $item/itemno }
{ $item/description }
{ $highbid/bid }
{ $buyer/name }
)
}
Expected Result:
1001
Red Bicycle
55
Mary Doe
For each item whose highest bid is more than twice its reserve price, list the item number, description, reserve price, and highest bid.
Solution in XQuery:
{
for $item in doc("items.xml")//item_tuple
let $b := doc("bids.xml")//bid_tuple[itemno = $item/itemno]
let $z := max($b/bid)
where $item/reserve_price * 2 < $z
return
{ $item/itemno }
{ $item/description }
{ $item/reserve_price }
{$z }
}
Expected Result:
1002
Motorcycle
500
1200.0
1004
Tricycle
15
40.0
Find the highest bid ever made for a bicycle or tricycle.
Solution in XQuery:
let $allbikes := doc("items.xml")//item_tuple
[contains(description, "Bicycle")
or contains(description, "Tricycle")]
let $bikebids := doc("bids.xml")//bid_tuple[itemno = $allbikes/itemno]
return
{
max($bikebids/bid)
}
Expected Result:
225
How many items were actioned (auction ended) in March 1999?
Solution in XQuery:
let $item := doc("items.xml")//item_tuple
[end_date >= xs:date("1999-03-01") and end_date <= xs:date("1999-03-31")]
return
{
count($item)
}
Expected Result:
3
List the number of items auctioned each month in 1999 for which data is available, ordered by month.
Solution in XQuery:
{
let $end_dates := doc("items.xml")//item_tuple/end_date
for $m in distinct-values(for $e in $end_dates
return get-month-from-date($e))
let $item := doc("items.xml")
//item_tuple[get-year-from-date(end_date) = 1999
and get-month-from-date(end_date) = $m]
order by $m
return
{ $m }
{ count($item) }
}
Expected Result:
1
1
2
2
3
3
4
1
5
1
For each item that has received a bid, list the item number, the highest bid, and the name of the highest bidder, ordered by item number.
Solution in XQuery:
{
for $highbid in doc("bids.xml")//bid_tuple,
$user in doc("users.xml")//user_tuple
where $user/userid = $highbid/userid
and $highbid/bid = max(doc("bids.xml")//bid_tuple[itemno=$highbid/itemno]/bid)
order by $highbid/itemno
return
{ $highbid/itemno }
{ $highbid/bid }
{ $user/name/text() }
}
Expected Result:
1001
55
Mary Doe
1002
1200
Mary Doe
1003
20
Jack Sprat
1004
40
Tom Jones
1007
225
Roger Smith
List the item number and description of the item(s) that received the highest bid ever recorded, and the amount of that bid.
Solution in XQuery:
let $highbid := max(doc("bids.xml")//bid_tuple/bid)
return
{
for $item in doc("items.xml")//item_tuple,
$b in doc("bids.xml")//bid_tuple[itemno = $item/itemno]
where $b/bid = $highbid
return
{ $item/itemno }
{ $item/description }
{ $highbid }
}
Expected Result:
1002
Motorcycle
1200.0
List the item number and description of the item(s) that received the largest number of bids, and the number of bids it (or they) received.
Solution in XQuery:
declare function local:bid_summary()
as element()*
{
for $i in distinct-values(doc("bids.xml")//itemno)
let $b := doc("bids.xml")//bid_tuple[itemno = $i]
return
{ $i }
{ count($b) }
};
{
let $bid_counts := local:bid_summary(),
$maxbids := max($bid_counts/nbids),
$maxitemnos := $bid_counts[nbids = $maxbids]
for $item in doc("items.xml")//item_tuple,
$bc in $bid_counts
where $bc/nbids = $maxbids and $item/itemno = $bc/itemno
return
{ $item/itemno }
{ $item/description }
{ $bc/nbids/text() }
}
Expected Result:
1001
Red Bicycle
5
1002
Motorcycle
5
For each user who has placed a bid, give the userid, name, number of bids, and average bid, in order by userid.
Solution in XQuery:
{
for $uid in distinct-values(doc("bids.xml")//userid),
$u in doc("users.xml")//user_tuple[userid = $uid]
let $b := doc("bids.xml")//bid_tuple[userid = $uid]
order by $u/userid
return
{ $u/userid }
{ $u/name }
{ count($b) }
{ avg($b/bid) }
}
Expected Result:
U01
Tom Jones
2
220.0
U02
Mary Doe
5
387.0
U03
Dee Linquent
2
487.5
U04
Roger Smith
5
266.0
U05
Jack Sprat
2
110.0
List item numbers and average bids for items that have received three or more bids, in descending order by average bid.
Solution in XQuery:
{
for $i in distinct-values(doc("bids.xml")//itemno)
let $b := doc("bids.xml")//bid_tuple[itemno = $i]
let $avgbid := avg($b/bid)
where count($b) >= 3
order by $avgbid descending
return
{ $i }
{ $avgbid }
}
Expected Result:
1002
800.0
1007
200.0
1001
45.0
List names of users who have placed multiple bids of at least $100 each.
Solution in XQuery:
{
for $u in doc("users.xml")//user_tuple
let $b := doc("bids.xml")//bid_tuple[userid=$u/userid and bid>=100]
where count($b) > 1
return
{ $u/name/text() }
}
Expected Result:
Mary Doe
Dee Linquent
Roger Smith
List all registered users in order by userid; for each user, include the userid, name, and an indication of whether the user is active (has at least one bid on record) or inactive (has no bid on record).
Solution in XQuery:
{
for $u in doc("users.xml")//user_tuple
let $b := doc("bids.xml")//bid_tuple[userid = $u/userid]
order by $u/userid
return
{ $u/userid }
{ $u/name }
{
if (empty($b))
then inactive
else active
}
}
Expected Result:
U01
Tom Jones
active
U02
Mary Doe
active
U03
Dee Linquent
active
U04
Roger Smith
active
U05
Jack Sprat
active
U06
Rip Van Winkle
inactive
List the names of users, if any, who have bid on every item.
Solution in XQuery:
{
for $u in doc("users.xml")//user_tuple
where
every $item in doc("items.xml")//item_tuple satisfies
some $b in doc("bids.xml")//bid_tuple satisfies
($item/itemno = $b/itemno and $u/userid = $b/userid)
return
$u/name
}
Expected Result:
(No users satisfy Q17.)
List all users in alphabetic order by name. For each user, include descriptions of all the items (if any) that were bid on by that user, in alphabetic order.
Solution in XQuery:
{
for $u in doc("users.xml")//user_tuple
order by $u/name
return
{ $u/name }
{
for $b in distinct-values(doc("bids.xml")//bid_tuple
[userid = $u/userid]/itemno)
for $i in doc("items.xml")//item_tuple[itemno = $b]
let $descr := $i/description/text()
order by $descr
return
{ $descr }
}
}
Expected Result:
Dee Linquent
Motorcycle
Racing Bicycle
Jack Sprat
Old Bicycle
Racing Bicycle
Mary Doe
Motorcycle
Red Bicycle
Rip Van Winkle
Roger Smith
Motorcycle
Old Bicycle
Racing Bicycle
Red Bicycle
Tom Jones
Motorcycle
Tricycle