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
• 3,040 points
254 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
• 3,040 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

How to programmatically get the values of a spilled Excel range in VBA?

By using the Text property, I was ...READ MORE

answered Mar 23, 2023 in Others by narikkadan
• 63,420 points
683 views
0 votes
1 answer

How to automatically assign a color to the maximum and minimum values in a set of selected cells in Excel?

See Conditional Formatting, which may be accessed ...READ MORE

answered Apr 7, 2023 in Others by Kithuzzz
• 38,010 points
283 views
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
727 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
• 9,060 points
4,360 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,210 points
459 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
1,196 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
• 3,040 points

edited Jun 22, 2023 by Khan Sarfaraz 245 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