Display first true and then false

In my SQL table, I have records. Some of them have FLAG Y/N. Now, when I run my query, I want to display first all the Y values and then N.

Table details

StuID …. StuFlag

1 …. N

2 …. N

3 …. Y

4 …. N

5 …. Y

Now, when I execute the query, the output should be:

3 …. Y

5 …. Y

1 …. N

2 …. N

4 …. N

>Solution :

The clean way would be:

CREATE TABLE table1 (
  stuid  INTEGER NOT NULL ...
, stuflag BIT
, [.. OTHER COLUMNS .. ]
);

INSERT INTO table1 VALUES (42,TRUE);
INSERT INTO table1 VALUES (42,FALSE);

SELECT * FROM table1 ORDER BY stuflag DESC;
-- FALSE is less than TRUE ...

Leave a Reply