Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:46:22+00:00 2026-05-28T06:46:22+00:00

I have a table with incremented id from 1,2,3…and so on. What i want

  • 0

I have a table with incremented id from 1,2,3…and so on. What i want is just to sort the data in descending order on the basis of field ‘id’ except first two rows. I tried using below query:-

SELECT * FROM categories ORDER BY CASE WHEN id<3 THEN 0 ELSE id END DESC

It give me the result like

id    name
5     Meal
4     Apparel
3     Electronics
1     Sports
2     Lifestyle

But output should come like

id    name
1     Sports
2     Lifestyle
5     Meal
4     Apparel
3     Electronics

Is there any way to achieve this by using such query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:46:23+00:00Added an answer on May 28, 2026 at 6:46 am

    Try this:

    SELECT *
    FROM categories
    ORDER BY CASE WHEN id < 3 THEN 10000 - id ELSE id END DESC;
    -- ----------------------------^
    -- use a very large number
    

    Edit:

    A better solution which does not require hard coding 10000:

    SELECT *
    FROM categories
    ORDER BY CASE WHEN id < 3 THEN id ELSE 3 END, id DESC
    -- ---------------------^              ^
    -- ------------------------------------|
    -- these numbers must be same
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one de normalized table. I want to select all the values from
I want to know if it's possible to select incremented integer from mysql table?
I have a table with a auto-incremented primary key: user_id. For a currently theoretical
I have a table with an identity column that have incremented by 1 for
I have the following hsqldb table, in which I map UUIDs to auto incremented
I have table with an auto-increment field, but I need to transfer the table
I have a table that has an AUTO_INCREMENT field. Currently, it is also a
I have a table with a primary key that is auto increment. I want
Updated I have some huge data, which becomes a large table say table parent
Possible Duplicate: Incrementing a field in SQL using PHP I have a table (T1)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.