Sorting by NULL values

From: Ian Burrell <ib(at)onsitetech(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Sorting by NULL values
Date: 2003-03-04 19:45:02
Message-ID: 3E65023E.10404@onsitetech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I am doing a query where I need to sort by a column that may be NULL
because it is coming from an OUTER JOIN. I noticed a difference between
PostgreSQL and other databases about where NULLs show up. It seems that
with Postgres, NULLs are sorted after other values. Other databases
sort them before.

Is there any standard on how sorting NULLs work? Is there a way to
change Postgres's behavior? Is there a way to replace the NULLs with
empty strings?

This is my query:

SELECT product_id
FROM product p
LEFT OUTER JOIN attribute_data ad
ON p.product_id= ad.product_id
AND ad.attribute_id = ?
WHERE p.category_id = ?
ORDER BY data;

The tables look like:

CREATE TABLE product (
product_id serial NOT NULL PRIMARY KEY,
category_id integer NOT NULL
);

CREATE TABLE attribute_data (
attribute_id integer NOT NULL,
product_id integer NOT NULL,
data varchar(1024),
PRIMARY KEY (attribute_id, product_id)
);

- Ian

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jean-Luc Lachance 2003-03-04 19:52:54 Re: Forcing query to use an index
Previous Message Joe Conway 2003-03-04 19:39:32 Re: Convert a text list to text array? Was: Denormalizing during