PrepAway - Latest Free Exam Questions & Answers

Which query should you use?

Your database contains two tables named DomesticSalesOrders and
InternationalSalesOrders. Both tables contain more than 100 million rows. Each table has a
Primary Key column named SalesOrderId. The data in the two tables is distinct from one
another.
Business users want a report that includes aggregate information about the total number of
global sales and total sales amounts.
You need to ensure that your query executes in the minimum possible time.
Which query should you use?

PrepAway - Latest Free Exam Questions & Answers

A.
SELECT COUNT(*) AS NumberOfSales, SUM(SalesAmount) AS TotalSalesAmount
FROM (
SELECT SalesOrderId, SalesAmount
FROM DomesticSalesOrders
UNION ALL
SELECT SalesOrderId, SalesAmount
FROM InternationalSalesOrders
) AS p

B.
SELECT COUNT(*) AS NumberOfSales, SUM(SalesAmount) AS TotalSalesAmount
FROM (
SELECT SalesOrderId, SalesAmount
FROM DomesticSalesOrders
UNION
SELECT SalesOrderId, SalesAmount
FROM InternationalSalesOrders
) AS p

C.
SELECT COUNT(*) AS NumberOfSales, SUM(SalesAmount) AS TotalSalesAmount
FROM DomesticSalesOrders
UNION
SELECT COUNT(*) AS NumberOfSales, SUM(SalesAmount) AS TotalSalesAmount
FROM InternationalSalesOrders

D.
SELECT COUNT(*) AS NumberOfSales, SUM(SalesAmount) AS TotalSalesAmount
FROM DomesticSalesOrders
UNION ALL
SELECT COUNT(*) AS NumberOfSales, SUM(SalesAmount) AS TotalSalesAmount
FROM InternationalSalesOrders

Explanation:
Reference:
http://msdn.microsoft.com/en-us/library/ms180026.aspx
Reference:
http://blog.sqlauthority.com/2009/03/11/sql-server-difference-between-union-vs-union-alloptimalperformance-comparison/

26 Comments on “Which query should you use?

  1. Yommy O. says:

    The question says “The data in the two tables is distinct from one another.” Hence, there’s no need to remove duplicates. However, the UNION ALL clause is more effective, as the SQL Engine implicitly eliminates the SORT operation required to return a DISTINCT result set.




    0



    0
  2. Yommy O. says:

    Option D aggregates the result set individually for both Domestic Sales and International Sales, and then merges the result into one contiguous result set; the UNION ALL eliminates duplicates (even though these may not be necessary).




    0



    0
  3. Turen says:

    Wonderful story, reckoned we could combine a few unrelated information, nonetheless really worth taking a search, whoa did a single master about Mid East has got extra problerms at the same time




    0



    0

Leave a Reply