• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

sql query - order by clause

Hello Experts
I am trying to use a simple sort by clause in the SQL to order the products in alphabetic order but to drop the stock to the last row if their stock =0
Please can someone advise how can I go about doing this?

SELECT productid,productname,stock
  FROM  Products
where categoryid=4
order by    productname

If I have any product whose stock = 0, then it should display in the last.

Please advise?

1 Solution
Rajkumar GsSoftware EngineerCommented:
Try this query
SELECT productid,productname, stock
  FROM  Products
where categoryid=4
order by  case when stock = 0 then 1 else 0 end, productname

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now