Home > Back-end >  Display first true and then false
Display first true and then false

Time:07-18

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

CodePudding user response:

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 ...

CodePudding user response:

select StuID, StuFlag from table order by StuFlag desc

For more details, you can consult The SQL ORDER BY Keyword

  • Related