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 in Others by Sohail
• 2,280 points
10 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 by Sohail
• 2,280 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
326 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,360 points
742 views
0 votes
2 answers

How to get the URL of the current tab in Google Chrome?

Its so simple.... If you want to ...READ MORE

answered Aug 12, 2020 in Others by Steve
• 200 points
712 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 in Database by Neha
• 8,560 points
349 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 in Others by nisha
• 2,160 points
15 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,850 points
331 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 in Others by Sohail
• 2,280 points
17 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP