What is the effect of this order_by clause?

Posted by bread on Stack Overflow See other posts from Stack Overflow or by bread
Published on 2010-06-09T08:07:05Z Indexed on 2010/06/09 8:12 UTC
Read the original article Hit count: 685

Filed under:

I don't understand what this order_by clause is doing and whether I need it or not:

select c.customerid, c.firstname, c.lastname, i.order_date, i.item, i.price from 
items_ordered i, customers c 
where i.customerid = c.customerid
group by c.customerid, i.item, i.order_date
order by i.order_date desc;

This produces this data:

10330   Shawn   Dalton  30-Jun-1999 Pogo stick  28.00
10101   John    Gray    30-Jun-1999 Raft    58.00
10410   Mary Ann    Howell  30-Jan-2000 Unicycle    192.50
10101   John    Gray    30-Dec-1999 Hoola Hoop  14.75
10449   Isabela Moore   29-Feb-2000 Flashlight  4.50
10410   Mary Ann    Howell  28-Oct-1999 Sleeping Bag    89.22
10339   Anthony Sanchez 27-Jul-1999 Umbrella    4.50
10449   Isabela Moore   22-Dec-1999 Canoe   280.00
10298   Leroy   Brown   19-Sep-1999 Lantern 29.00
10449   Isabela Moore   19-Mar-2000 Canoe paddle    40.00
10413   Donald  Davids  19-Jan-2000 Lawnchair   32.00
10330   Shawn   Dalton  19-Apr-2000 Shovel  16.75
10439   Conrad  Giles   18-Sep-1999 Tent    88.00
10298   Leroy   Brown   18-Mar-2000 Pocket Knife    22.38
10299   Elroy   Keller  18-Jan-2000 Inflatable Mattress 38.00
10438   Kevin   Smith   18-Jan-2000 Tent    79.99
10101   John    Gray    18-Aug-1999 Rain Coat   18.30
10449   Isabela Moore   15-Dec-1999 Bicycle 380.50
10439   Conrad  Giles   14-Aug-1999 Ski Poles   25.50
10449   Isabela Moore   13-Aug-1999 Unicycle    180.79
10101   John    Gray    08-Mar-2000 Sleeping Bag    88.70
10299   Elroy   Keller  06-Jul-1999 Parachute   1250.00
10438   Kevin   Smith   02-Nov-1999 Pillow  8.50
10101   John    Gray    02-Jan-2000 Lantern 16.00
10315   Lisa    Jones   02-Feb-2000 Compass 8.00
10449   Isabela Moore   01-Sep-1999 Snow Shoes  45.00
10438   Kevin   Smith   01-Nov-1999 Umbrella    6.75
10298   Leroy   Brown   01-Jul-1999 Skateboard  33.00
10101   John    Gray    01-Jul-1999 Life Vest   125.00
10330   Shawn   Dalton  01-Jan-2000 Flashlight  28.00
10298   Leroy   Brown   01-Dec-1999 Helmet  22.00
10298   Leroy   Brown   01-Apr-2000 Ear Muffs   12.50

While if I remove the order_by clause completely, as in this query:

select c.customerid, c.firstname, c.lastname, i.order_date, i.item, i.price from 
items_ordered i, customers c 
where i.customerid = c.customerid
group by c.customerid, i.item, i.order_date;

I get these results:

10101   John    Gray    30-Dec-1999 Hoola Hoop  14.75
10101   John    Gray    02-Jan-2000 Lantern 16.00
10101   John    Gray    01-Jul-1999 Life Vest   125.00
10101   John    Gray    30-Jun-1999 Raft    58.00
10101   John    Gray    18-Aug-1999 Rain Coat   18.30
10101   John    Gray    08-Mar-2000 Sleeping Bag    88.70
10298   Leroy   Brown   01-Apr-2000 Ear Muffs   12.50
10298   Leroy   Brown   01-Dec-1999 Helmet  22.00
10298   Leroy   Brown   19-Sep-1999 Lantern 29.00
10298   Leroy   Brown   18-Mar-2000 Pocket Knife    22.38
10298   Leroy   Brown   01-Jul-1999 Skateboard  33.00
10299   Elroy   Keller  18-Jan-2000 Inflatable Mattress 38.00
10299   Elroy   Keller  06-Jul-1999 Parachute   1250.00
10315   Lisa    Jones   02-Feb-2000 Compass 8.00
10330   Shawn   Dalton  01-Jan-2000 Flashlight  28.00
10330   Shawn   Dalton  30-Jun-1999 Pogo stick  28.00
10330   Shawn   Dalton  19-Apr-2000 Shovel  16.75
10339   Anthony Sanchez 27-Jul-1999 Umbrella    4.50
10410   Mary Ann    Howell  28-Oct-1999 Sleeping Bag    89.22
10410   Mary Ann    Howell  30-Jan-2000 Unicycle    192.50
10413   Donald  Davids  19-Jan-2000 Lawnchair   32.00
10438   Kevin   Smith   02-Nov-1999 Pillow  8.50
10438   Kevin   Smith   18-Jan-2000 Tent    79.99
10438   Kevin   Smith   01-Nov-1999 Umbrella    6.75
10439   Conrad  Giles   14-Aug-1999 Ski Poles   25.50
10439   Conrad  Giles   18-Sep-1999 Tent    88.00
10449   Isabela Moore   15-Dec-1999 Bicycle 380.50
10449   Isabela Moore   22-Dec-1999 Canoe   280.00
10449   Isabela Moore   19-Mar-2000 Canoe paddle    40.00
10449   Isabela Moore   29-Feb-2000 Flashlight  4.50
10449   Isabela Moore   01-Sep-1999 Snow Shoes  45.00
10449   Isabela Moore   13-Aug-1999 Unicycle    180.79

I'm not sure what the order_by is doing here and if it's having the intended effects.

© Stack Overflow or respective owner

Related posts about sql