Ordering by the order of values in a SQL IN clause

0 votes
I was curious if there's any way (possibly a better way) to order by the order of the values in an IN() clause.

The problem is if we have 2 queries, one that gets all of the IDs and the other retrieves all the information. The first creates the order of the IDs which we want the second to order by. The IDs are put in an IN() clause in the correct order.
May 28, 2022 in Others by Sohail
• 2,960 points
100 views

1 answer to this question.

0 votes

We can use expressions as well in Order by clause. Suppose we want to sort for a year in a date column. Let us run the query without any Order by clause first.

SELECT top 10 [NationalIDNumber]

  , SickLeaveHours as [SickHours]

      ,[Vacationhours]

      ,[BirthDate]

      ,[MaritalStatus]

      ,[Gender]

      ,[ModifiedDate]

      ,[SalariedFlag]

FROM [AdventureWorks2017].[HumanResources].[Employee]

Where MaritalStatus='M'

Now, we want to sort on Birthdate column yearly in ascending order. I am using the TOP clause to limit result in the output. We are using DATEPART function we get a specified part of a date. In this query, we want to extract year to sort data using the DATEPART function.

1

2

3

4

5

6

7

8

9

10

11
SELECT top 10 [NationalIDNumber]

  , SickLeaveHours as [SickHours]

      ,[Vacationhours]

      ,[BirthDate]

      ,[MaritalStatus]

      ,[Gender]

      ,[ModifiedDate]

      ,[SalariedFlag]

FROM [AdventureWorks2017].[HumanResources].[Employee]

Where MaritalStatus='M'

Order by DATEPART(YEAR , BirthDate) ASC
answered May 31, 2022 by Sohail
• 2,960 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

Change the permission for a file created by me in linux

If you are the system administrator or ...READ MORE

answered Mar 8, 2019 in Others by Nabarupa
423 views
0 votes
1 answer

How to retrieve the value of a text field in Flutter App?

Hi@akhtar, In your TextField, you can call one ...READ MORE

answered Sep 3, 2020 in Others by MD
• 95,420 points
1,115 views
0 votes
1 answer

How to add Conditional Formatting in Excel for a Range of Values

Three distinct rules are required, one for ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 53,520 points
131 views
0 votes
1 answer

Ordering by the order of values in a SQL IN() clause

Use MySQL's FIELD() function: SELECT name, description, ... FROM ... WHERE id ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 8,940 points
1,964 views
0 votes
1 answer

Ordering by the order of values in a SQL IN() clause

Use MySQL FIND_IN_SET function: SELECT * ...READ MORE

answered Jun 6, 2022 in Others by nisha
• 2,190 points
119 views
0 votes
1 answer
0 votes
1 answer

How to change the default value and to alter a column in sql?

Hi, You can try this: ALTER TABLE foobar_data CHANGE ...READ MORE

answered Jun 24, 2019 in Big Data Hadoop by Gitika
• 65,910 points
639 views
0 votes
1 answer

Does Java have a IN operator or function like SQL

A Java-language was designed to be powerful ...READ MORE

answered May 30, 2022 in Others by Sohail
• 2,960 points
69 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP