sql order by where condition

SELECT ProcductCode AS Id, ProductPrice AS Price
FROM Products WITH (NOLOCK)
WHERE ProductCode IN ('efg', 'abc', 'xyz')
ORDER BY (CASE WHEN ProductCode = 'efg' THEN 1
               WHEN ProductCode = 'abc' THEN 2
               WHEN ProductCode = 'xyz' THEN 3
               ELSE 4  -- in case you change the `where`, put them last
          END);

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
can we have a condition with order by in sql sql orderby clause sql order and where how to use where clause with order by in sql orderby sql with condition order by and where clause in sql server sql where condition ascending where with order by sql where condition with order by in sql querydsl or condition order query for order by clause with where clause in sql order by where clause in sql order by with when clause where condition and order by in sql asc SQL "ORDER BY IF" sql order by if condition in order by sql if clause in order by sql order by where clause order by clause in sql with condition sql order by if condition order by and where clause in sql order by in where condition condition order by sql order by with condition in sql how to check condition to order by in sql order by query with where condition sql order by with condition order by in specific condition sql order by and where in sql order by clause order by sql with where clause sql order by with where SQL if order by order by in sql with condition order by in sql with where condition how to order in sql when condition order by with where clause in sql where condition and order by in sql order by with where clause order by condition in sql sql order by without where clause what is the use of orderby in table ? sql order by where condition sql query with descending order with where condition where and order by clause in sql order by sql query example order in where sql The ORDER BY clause must be placed before the WHERE clause in a SQL statement. select from where oredre by order by and where clause together in sql how to write orderby and where clause in sql
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source