Database Reference
In-Depth Information
because the OrderTotal sum is not a column in a table, the DBMS has no column name to
provide. The preceding result was produced by Microsoft SQL Server 2012, and it names the
column '(No column name)'. Other DBMS products take other, equivalent actions.
This result is ugly. We would prefer to have a meaningful column name, and SQL allows
us to assign one using the SQL AS keyword . If we use the AS keyword in the query as follow:
/* *** SQL-Query-CH02-27 *** */
SELECT SUM(OrderTotal) AS OrderSum
FROM RETAIL_ORDER;
The result of this modified query will be:
This result has a much more meaningful column label. The name OrderSum is arbitrary—
we are free to pick any name that we think would be meaningful to the user of the result. We
could pick OrderTotal_Total , OrderTotalSum , or any other label that we think would be useful.
The utility of the built-in functions increases when you use them with an SQL WHERE
clause. For example, we can write the SQL query:
/* *** SQL-Query-CH02-28 *** */
SELECT
SUM(ExtendedPrice) AS Order3000Sum
FROM
ORDER_ITEM
WHERE
OrderNumber=3000;
The result of this query is:
The SQL built-in functions can be mixed and matched in a single statement. For example,
we can create the following SQL statement:
/* *** SQL-Query-CH02-29 *** */
SELECT SUM(ExtendedPrice) AS OrderItemSum,
AVG(ExtendedPrice) AS OrderItemAvg,
MIN(ExtendedPrice) AS OrderItemMin,
MAX(ExtendedPrice) AS OrderItemMax
FROM ORDER_ITEM;
The result of this query is:
The SQL built-in COUNT function sounds similar to the SUM function, but it produces
very different results. The COUNT function counts the number of rows, whereas the SUM func-
tion adds the values in a column. For example, we can use the SQL built-in COUNT function to
determine how many rows are in the ORDER_ITEM table:
/* *** SQL-Query-CH02-30 *** */
SELECT COUNT(*) AS NumberOfRows
FROM ORDER_ITEM;
The result of this query is:
 
 
Search WWH ::




Custom Search