7. Working with Numbers

Computing an Average

We want to compute the average value in a column.

  1. Computing the average of all employee salaries

select avg(sal) as avg_sal from emp
  1. Compute the average salary for each department

select deptno, avg(sal) as avg_sal from emp
group by deptno

Finding the Min/Max Value in a Column

We want to find the highest and lowest values in a given column.

  1. Lowest and highest salaries for all employees

select min(sal) as min_sal, max(sal) as max_sal from emp
  1. Lowest and highest salaries for each department

select deptno, min(sal) as min_sal, max(sal) as max_sal 
from emp group by deptno

Summing the Values in a Column

We want to compute the sum of all values in a column.

  1. Sum for all employees

  1. Sum for each department

Counting Rows in a Table

We want to count the number of rows in a table, or want to count the number of values in a column.

  1. Counting rows for the whole table

  1. Counting rows for each department

Counting Values in a Column

We want to count the number of non-NULL values in a column

When we do COUNT(*), counting is rows (regardless of actual value, which is why rows containing NULL and non-NULL values are counted). But when we do COUNT a column, we are counting the number of non-NULL values in that column.

Generating a Running Total

We want to calculate a running total or cumulative of values in a column

Generating a Running Product

We want to compute a running product on a numeric column

Steps to Calculate the Product Using LN and EXP

  1. Calculate the natural logarithm (LN) of each number.

  2. Sum the logarithms.

  3. Take the exponential (EXP) of the sum to get the product.

Smoothing a Series of Values

We have a series of values that appear over time, such as monthly sales figures. We want to implement a simple smoother, such as weighted running average.

A moving average can be calculated by summing the current value and the preceding n-1 values and dividing by n.

Calculating a Mode

We want to find the mode.

Mode is the element that appears most frequently for a given set of data.

For example, select sal from emp where deptno = 20 order by sal;

SAL

800 1100 2975 3000 3000 The mode is 3000.

Calculating a Median

We want to calculate the median.

Median is the value of the middle member of a set of ordered elements.

For example, select sal from emp where deptno = 20 order by sal

SAL 800 1100 2975 3000 3000 The median is 2975.

Determining the Percentage of a Total

We want to determine the percentage that values in a specific column represent against a total. For example, we want to determine what percentage of all salaries are the salaries in DEPTNO 10 (the percentage that DEPTNO 10 salaries contribute to the total)

Aggregating Nullable Columns

We want to perform an aggregation on a column, but the column is nullable.

Computing Averages Without High and Low Values

We want to compute an average, but excluding the highest and lowest values

Converting Alphanumeric Strings into Numbers

We have alphanumeric data and would like to return numbers only. You want to return the number 123321 from the string “pjenil123g321.”

Changing Values in a Running Total

We want to modify the values in a running total depending on the values in another column.

Last updated