---
title: "How To Use ORDER BY with UNION Operators in MS SQL Server?"  
description: "How To Use ORDER BY with UNION Operators in MS SQL Server?"  
author: "Amit Singh"  
published: 2010-11-09  
updated: 2020-09-22  
canonical: https://www.mindstick.com/interview/221/how-to-use-order-by-with-union-operators-in-ms-sql-server  
category: "db2"  
tags: ["db2"]  
reading_time: 1 minute  

---

# How To Use ORDER BY with UNION Operators in MS SQL Server?

If you need to sort the output from two queries grouped together with a UNION operator, you need to apply the ORDER BY clause at the group level, not at the subquery level.\
\
for example:\
(SELECT * FROM sample WHERE tag = 'MCA' ORDER BY created)\
UNION\
(SELECT * FROM sample WHERE tag = 'BCA' ORDER BY created)\

## Answers

### Answer by Amit Singh

If you need to sort the output from two queries grouped together with a UNION operator, you need to apply the ORDER BY clause at the group level, not at the subquery level.\
\
for example:\
(SELECT * FROM sample WHERE tag = 'MCA' ORDER BY created)\
UNION\
(SELECT * FROM sample WHERE tag = 'BCA' ORDER BY created)\


---

Original Source: https://www.mindstick.com/interview/221/how-to-use-order-by-with-union-operators-in-ms-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
