skip to Main Content

I have a below Organisation table.

ID Resource
1 Software Engineer
2 Tech Lead
3 Architect
4 Senior Software Engineer
5 HR

I need to fetch this table using select query such that the output which i receive should have ID 4 between ID 1 and ID 2

Output:

ID Resource
1 Software Engineer
2 Tech Lead
4 Senior Software Engineer
3 Architect
5 HR

How can i achieve this

3

Answers


  1. You could use a CASE expression to generate the ordering you want:

    SELECT ID, Resource
    FROM Organisation
    ORDER BY CASE ID WHEN 4 THEN 1.5 ELSE ID END;
    
    Login or Signup to reply.
  2. You can use a SELECT query with an ORDER BY clause to specify the order in which the rows should be displayed.

    SELECT ID, Resource
    FROM Organisation
    ORDER BY
      CASE
        WHEN ID = 4 THEN 1
        WHEN ID BETWEEN 1 AND 2 THEN 0
        ELSE 2
      END,
      ID;
    
    Login or Signup to reply.
  3. It seems like you’re trying to retrieve specific rows from your Organization table based on the ID. To achieve this, you can use the ORDER BY clause along with a CASE statement in your SQL query. Here’s your query:

        SELECT ID, Resource
        FROM Organization
        ORDER BY 
          CASE 
            WHEN ID = 4 THEN 1
            ELSE ID 
          END;
    

    This query will prioritize the row with ID 4 to be placed after ID 1. The rest of the rows will be ordered based on their IDs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search