473,657 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

7.4, 'group by' default ordering?

I notice that 7.4 doesn't do default ordering on a 'group by', so you have
to throw in an 'order by' clause to get the output in ascending group order.

Is this something that most RDB's have historically done (including PG prior
to 7.4) but isn't really part of the SQL standard?

On a mostly unrelated topic, does the SQL standard indicate whether NULL
should sort to the front or the back? Is there a way to force it to
one or the other independent of whether the order by clause uses ascending or
descending order?
--
Mike Nolan

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 12 '05 #1
5 3220
On Thu, Jan 08, 2004 at 13:42:33 -0600,
Mike Nolan <no***@gw.tssi. com> wrote:
I notice that 7.4 doesn't do default ordering on a 'group by', so you have
to throw in an 'order by' clause to get the output in ascending group order.

Is this something that most RDB's have historically done (including PG prior
to 7.4) but isn't really part of the SQL standard?
That is because group by is often done with a sort, so rows would naturally
be in that order. If there isn't an order by clause, the set of return
rows can be in any order.
On a mostly unrelated topic, does the SQL standard indicate whether NULL
should sort to the front or the back? Is there a way to force it to
one or the other independent of whether the order by clause uses ascending or
descending order?


In SQL for Smarties, Joe Ceclko says that either NULLs should all be first
or all be last (independent of whether the sort is ascending or descending).
There was also some discussion on how the order is constrained if the sort
is on multiple columns where the value of the first column is NULL, but the
values of other columns are not. I don't have the book here with me now,
but I think the result of the discussion was that within rows with a NULL
value for the first column, they should be sorted by the values in the
later columns.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2
After takin a swig o' Arrakan spice grog, no***@gw.tssi.c om (Mike Nolan) belched out:
I notice that 7.4 doesn't do default ordering on a 'group by', so you have
to throw in an 'order by' clause to get the output in ascending group order.

Is this something that most RDB's have historically done (including PG prior
to 7.4) but isn't really part of the SQL standard?
No, in the absence of an "ORDER BY" clause to impose order, there is
no "standard" reason to expect _any_ SQL database to return results in
_any_ particular sort of order.

It would be perfectly legitimate for a database to store all data in
hash tables, and to return rows in the resulting random order, sorting
the result set only if the query specified an order.
On a mostly unrelated topic, does the SQL standard indicate whether
NULL should sort to the front or the back? Is there a way to force
it to one or the other independent of whether the order by clause
uses ascending or descending order?


NULL isn't equal to any other value, so that, heading back to that
"any order is reasonable" notion, it might, in theory, be
"standards-conformant" to randomly intersperse the NULL values amongst
the other entries that _ARE_ returned in order.

I understand that Oracle declines to include NULL entries in indices,
which doubtless has interesting implications...

Clustering NULLs together at either the start or end of a query seems
sensible; different SQL databases handle this differently...
--
If this was helpful, <http://svcs.affero.net/rm.php?r=cbbrow ne> rate me
http://www.ntlug.org/~cbbrowne/
It's a little known fact that the Dark Ages were caused by the Y1K
problem.
Nov 12 '05 #3
Bruno Wolff III <br***@wolff.to > writes:
On Thu, Jan 08, 2004 at 13:42:33 -0600,
Is this something that most RDB's have historically done (including PG prior
to 7.4) but isn't really part of the SQL standard?
That is because group by is often done with a sort, so rows would naturally
be in that order. If there isn't an order by clause, the set of return
rows can be in any order.
PG has historically implemented GROUP BY with sort + uniq (and still may
if the planner thinks it better than a hash method), but I am not sure
that this is particularly widespread among other DBMSes. In any case,
the spec certainly says that you cannot expect any particular result
ordering if you didn't say ORDER BY.
On a mostly unrelated topic, does the SQL standard indicate whether NULL
should sort to the front or the back? Is there a way to force it to
one or the other independent of whether the order by clause uses
ascending or descending order?

In SQL for Smarties, Joe Ceclko says that either NULLs should all be first
or all be last (independent of whether the sort is ascending or descending).


If Celko really says that, I think he's wrong. SQL92 13.1 general rule
3 says:

Whether a sort key value that is null is considered greater
or less than a non-null value is implementation-defined, but
all sort key values that are null shall either be considered
greater than all non-null values or be considered less than
all non-null values.

Since they use the phraseology "greater than" and "less than", I'd
expect that switching between ASC and DESC order would reverse the
output ordering, just as it would for two ordinary values one of which
is greater than the other.

We actually went to some trouble to make this happen, a release or three
back. IIRC, at one time PG did sort NULLs to the end regardless of
ASC/DESC, but we were persuaded that this was contrary to spec.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #4
Tom Lane wrote:
Bruno Wolff III <br***@wolff.to > writes:

On Thu, Jan 08, 2004 at 13:42:33 -0600,
<snip>

On a mostly unrelated topic, does the SQL standard indicate whether NULL
should sort to the front or the back? Is there a way to force it to
one or the other independent of whether the order by clause uses
ascending or descending order?

In SQL for Smarties, Joe Ceclko says that either NULLs should all be first
or all be last (independent of whether the sort is ascending or descending).


If Celko really says that, I think he's wrong. SQL92 13.1 general rule
3 says:

Whether a sort key value that is null is considered greater
or less than a non-null value is implementation-defined, but
all sort key values that are null shall either be considered
greater than all non-null values or be considered less than
all non-null values.

Since they use the phraseology "greater than" and "less than", I'd
expect that switching between ASC and DESC order would reverse the
output ordering, just as it would for two ordinary values one of which
is greater than the other.

We actually went to some trouble to make this happen, a release or three
back. IIRC, at one time PG did sort NULLs to the end regardless of
ASC/DESC, but we were persuaded that this was contrary to spec.

regards, tom lane

Celko uses the SQL92 13.1 rule wording: "Whether a sort key value that
is NULL is considered greater or less than a non-NULL value is
implementation defined, but all sort key values that are NULL will
either be considered greater than all non-NULL values or be considered
less than all non-NULL values. There are SQL products that do it either
way." 2nd Ed SQL For Smarties.

And of more interest, he also points out that in SQL-89, the last
General Rule of <comparison predicate> should still be applied:
"Although 'x=y' is unkown if both x and y are NULL values, in the
context of GROUP BY, ORDER BY, and DISTINCT, a NULL value is identical
to or is a duplicate of another NULL value." So NULL=NULL for purposes
of GROUP BY, ORDER BY and DISTINCT. PostgresSQL seems to treat them this
way and puts them after non-NULL values.

Here's my test case:

Welcome to psql 7.4beta5, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit

celko=# CREATE TABLE sortable (a INT DEFAULT NULL, b INT);
celko=# INSERT INTO sortable (b) VALUES (8);
INSERT 60836961 1
celko=# INSERT INTO sortable (b) VALUES (4);
INSERT 60836962 1
celko=# SELECT a,b FROM sortable ORDER BY a,b;
a | b
---+---
| 4
| 8
(2 rows)

celko=# INSERT INTO sortable (a,b) VALUES (5,5);
INSERT 60836963 1
celko=# SELECT a,b FROM sortable ORDER BY a,b;
a | b
---+---
5 | 5
| 4
| 8
(3 rows)

celko=# INSERT INTO sortable (b) VALUES (5);
INSERT 60836964 1
celko=# SELECT a,b FROM sortable ORDER BY a,b;
a | b
---+---
5 | 5
| 4
| 5
| 8
(4 rows)

celko=# INSERT INTO sortable (a,b) VALUES (2,2);
INSERT 60836965 1
celko=# SELECT a,b FROM sortable ORDER BY a,b;
a | b
---+---
2 | 2
5 | 5
| 4
| 5
| 8
(5 rows)

celko=# SELECT a,b FROM sortable ORDER BY b,a;
a | b
---+---
2 | 2
| 4
5 | 5
| 5
| 8
(5 rows)

FYI
Ron
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #5
On Thu, Jan 08, 2004 at 15:24:41 -0800,
Ron St-Pierre <rs*******@sysc or.com> wrote:
Celko uses the SQL92 13.1 rule wording: "Whether a sort key value that
is NULL is considered greater or less than a non-NULL value is
implementation defined, but all sort key values that are NULL will
either be considered greater than all non-NULL values or be considered
less than all non-NULL values. There are SQL products that do it either
way." 2nd Ed SQL For Smarties.


The part just after that prompted my statement. The text is:
And there are those that have it all wrong; the Sybase family simply
treats the NULLs as if they were really values -- that is, they sort
low for ascending and high for descending.

This seems to me to be saying contradictory things now that I have reread it.
If NULLs are treated as real values, I would expect them to be output
first for one of descending and ascending and last for the other. But
the second part of the statement contradicts this and seems to be saying
that Sybase always emits records with NULL values for the key first.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
587
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need to provide users with a mechanism to change the order of items that are listed on the page.For example, the _New York Times_ website (http://www.nytimes.com) lists a bunch of top news articles, and normally these are ordered by purely mechanical...
2
5932
by: Simone | last post by:
Hello. I am using the code below: ------------------------------------------- Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer) If Me.Section(0).BackColor = vbWhite Then Me.Section(0).BackColor = 15724527 Else Me.Section(0).BackColor = vbWhite End If
12
4234
by: Gerrit Beuze | last post by:
Hi all, I'm wondering if you how you organize as (in sorting / order) your C# class code: Do you sort/ group by member type: fields, methods, properties etc.? If yes: what ordering scheme do yo use? Do you group members by visibility - and how does that relate to grouping by type? Where do you insert (nested) types such as enums and delegates in classes
4
7601
by: Mike Nolan | last post by:
I'm trying to create a summary log by hour. Here's the query (somewhat simplified): select to_char(mtrantime,'mm-dd hh AM') as datetime, count(*) as tot from memtran group by datetime order by datetime; The problem is this produces the data in the following order:
1
5742
by: Matt Roberts | last post by:
Please accept my apologies if this is answered elsewhere in the archives or docs but I have searched without luck. I've always assumed that default ordering of selects are based on a first in first out principle and that this remains true at the row level despite edits to columns. However I'm dealing with a case where this doesn't seem to hold true. The ordering has changed over time. Its difficult for me to gauge whether the data has...
33
3387
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a 4-byte value. Can I use the arithmatic shift operators to hide the endian-ness of the underlying processor when assembling a native 4-byte value like follows: unsigned int integerValue; unsigned char byteArray;
1
1592
by: evanburen | last post by:
When my page loads, I check for the existence of a cookie value through readCookie(). If there is a value present for the cookie, I would like that to be the default value in function ordering() and that value to be the SELECTED value in ddlProfileNames. In other words, if there is no cookie value, then function ordering will default to ordering("Div1,Div2,Div3,Div4"). If there is a value, then that should be used in function ordering...
4
2075
ChrisWang
by: ChrisWang | last post by:
Dear all, I am reading the book "Core Python Programming". In the chapter talking about modules, it says the modules should follow this ordering: import Python Standard Library modules import Python third party modules import Application-specific modules
4
3950
by: SjaakBanaan | last post by:
Hey all, I have a question that ought to be simple but has given me headaches for a while. I have a table with contact email addresses, say. T ID ContID Priority Emailaddress ----------------------------------------------
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.