Search code examples
sqlsql-servert-sql

Multiple select queries with union, flatten to single row


In this simplified example, I have the following query:

SELECT COUNT(*) AS OrderCount
FROM Orders
UNION
SELECT COUNT(*) AS CustomerCount
FROM Customers

Which results in the following output:

Order Count
135
283

But I would like the output to be:

OrderCount CustomerCount
135 283

Solution

  • You can select like below, if there is any relation between orders and customers you can use joins as well

    SELECT
      (SELECT COUNT(*) FROM Orders) AS OrderCount,
      (SELECT COUNT(*) FROM Customers) AS CustomerCount;
    
    OrderCount CustomerCount
    135 283