1. SQL Simple Queries – Worksheet 3
SQL Queries - Basics
Worksheet - 3
SUMMARY QUERIES
1 What is a summary query?
For certain queries we are not interested in finer details of each record in a table. Rather we
want aggregate or summary information. For example, consider the following queries:
a) How many salespeople have exceeded their yearly quota?
b) What is the sixe of the average order for an office?
c) How many salespeople are assigned to each office?
d) What is the average mark of students in an exam?
SQL supports these requests for summary data through column functions and the GROUP
BY and HAVING clauses of the SELECT statement.
2 What are the COLUMN functions in SQL?
SQL lets you summarize data from the database through a set of column functions. A SQL
column function takes an entire column of data as its argument and produces a single data
item that summarizes the column.
For example, the AVG() column function takes a column of data and computes its average.
Here is a query that uses the AVG() column function to compute the average value of two
columns from the SALESREPS table:
What are the average quota and average sales of our salespeople?
SELECT AVG(QUOTA), AVG(SALES)
FROM SALESREPS
AVG(QUOTA) AVG(SALES)
The first column function in the query takes values in the QUOTA column and computes
their average; the second one averages the values in the SALES column. The query
produces a single row of query results summarizing the data in the SALESREPS table.
SQL offers 6 different column functions. These are:
1. SUM() – computes the total of a column
2. AVG() – computes the average value in a column
3. MIN() – finds the minimum value in a column
4. MAX() – finds the maximum value in a column
5. COUNT() – counts the number of values in a column
6. COUNT(*) – counts row of query results
The argument to a column function can be a simple column name or an expression.
Example 1:
Prof. Mukesh N. Tekwani Page 1 of 4
2. SQL Simple Queries – Worksheet 3
What is the average quota performance of our salespeople?
SELECT AVG (100 * (SALES/QUOTA))
FROM SALESREPS
Computing a column total
The SUM() column function computes the sum of a column of data values. The data in the
column must have a numeric type (integer, decimal, floating point, or money). The result of
the SUM() function has the same basic data type as the data in the column, but the result
may have a higher precision. For example, if you apply the SUM() function to a column of
16-bit integers, it may produce a 32-bit integer as its result.
Example 2:
What are the total quotas and sales for all salespeople?
SELECT SUM(QUOTA), SUM(SALES)
FROM SALESREPS
What is the total of the orders taken by Bill Adams?
SELECT SUM(AMOUNT)
FROM ORDERS, SALESREPS
WHERE NAME = 'Bill Adams'
AND REP = EMPL_NUM
Computing an Average
The AVG() column function computes the average of a column of data values. The data in
the column must have a numeric type.
Because the AVG() function adds the values in the column and then divides by the number
of values, its result may have a different data type than that of the values in the column. For
example, if you apply the AVG() function to a column of integers, the result will be either
a decimal or a floating point number.
Calculate the average price of products from manufacturer ACI.
SELECT AVG(PRICE)
FROM PRODUCTS
WHERE MFR_ID = 'ACI'
Calculate the average size of an order placed by Acme Mfg. (customer number 2103).
SELECT AVG(AMOUNT)
FROM ORDERS
WHERE CUST = 2103
Page 2 of 4 mukeshtekwani@hotmail.com
3. SQL Simple Queries – Worksheet 3
Finding maximum and minimum values
The MIN() and MAX() column functions find the smallest and largest values in a column,
respectively. The data in the column can contain numeric, string, or date/time information.
The result of the MIN() or MAX() function has exactly the same data type as the data in
the column.
What are the smallest and largest assigned quotas?
SELECT MIN(QUOTA), MAX(QUOTA)
FROM SALESREPS
What is the earliest order date in the database?
SELECT MIN(ORDER_DATE)
FROM ORDERS
What is the best sales performance of any salesperson?
SELECT MAX(100 * (SALES/QUOTA))
FROM SALESREPS
MAX(100*(SALES/QUOTA))
Counting Data Values
The COUNT() column function counts the number of data values in a column. The data in
the column can be of any type. The COUNT() function always returns an integer,
regardless of the data type of the column.
How many customers are there?
SELECT COUNT(CUST_NUM)
FROM CUSTOMERS
How many salespeople are over quota?
SELECT COUNT(NAME)
FROM SALESREPS
WHERE SALES > QUOTA
Find the average order amount, total order amount, average order amount as a percentage
of the customer's credit limit, and average order amount as a percentage of the
salesperson's quota.
SELECT AVG(AMOUNT), SUM(AMOUNT),
(100 * AVG(AMOUNT/CREDIT_LIMIT)),
(100 * AVG(AMOUNT/QUOTA))
FROM ORDERS, CUSTOMERS, SALESREPS
WHERE CUST = CUST_NUM
AND REP = EMPL_NUM
Prof. Mukesh N. Tekwani Page 3 of 4
4. SQL Simple Queries – Worksheet 3
3 What are Subqueries?
Subqueries are query statements inside of query statements. Like the order of operations in
algebra, order of operations also come into play when you start to embed SQL commands
inside of other SQL commands (subqueries).
Example 1:
Select only the most recent order(s) in the orders table.
We use the MAX(). This function wraps around a table column and returns the current
highest (max) value for the specified column. We use this function to return the current
"highest", or most recent date value in the orders table.
USE mydatabase;
SELECT MAX(day_of_order)
FROM orders
Now we can throw this query into the WHERE clause of another SELECT query.
USE mydatabase;
SELECT *
FROM orders
WHERE day_of_order = ( SELECT MAX(day_of_order) FROM orders )
4 SELECT Case statement
SQL CASE is a very unique conditional statement providing if/then/else logic for any
ordinary SQL command, such as SELECT or UPDATE. It then provides when-then-else
functionality (WHEN this condition is met THEN do_this).
USE northwind
SELECT productid,
'Status' = CASE
WHEN quantity > 0 THEN 'in stock'
ELSE 'out of stock'
END
FROM "order details";
5 GROUP BY statement
SQL GROUP BY aggregates (consolidates and calculates) column values into a single
record value. GROUP BY requires a list of table columns on which to run the calculations.
USE mydatabase
SELECT customer
FROM orders
GROUP BY customer
Page 4 of 4 mukeshtekwani@hotmail.com