How To Use COUNT In Oracle SQL - ORACLE

How To Use COUNT In Oracle SQL

March 31, 2023
Oracle DBA
oracle dba oracle database management database management system database oracle performance tuning oracle sql learn sql learn personal injury attorney new york mesothelioma lawyers mesothelioma lawyers new york mesothelioma lawyer the stock market plumbing retirement planning barclays stock broker financial advisor personal retirement financial advisor

This help is based on examples so it would be easier to understand. Oracle Count Function returns a number of rows returned by the SQL query. The Count function can be used with “*“, “ALL“, “DISTINCT“, or/and your own condition. The last one is for setting the Count function to return only required rows. The function returns always a value and it can be used as an aggregate or analytic function depending on the optional analytic clause existsing or not. The syntax of Oracle Count is:

COUNT(*) [OVER (<analytic_clause>)])
COUNT(ALL <condition>) [OVER (<analytic_clause>)])
COUNT(DISTINCT <condition>) [OVER (<analytic_clause>)])
COUNT(<condition>) [OVER (<analytic_clause>)])

When you are using the condition in the Oracle Count then the function counts only lines where the condition returns not null value.

To go through the examples we will need a table and some data. The easiest way is to used the Dual table and generated some lines with Oracle Rownum and the hierarhical clause Connect By as in the following sample.

 SELECT ROWNUM
   FROM DUAL
CONNECT BY ROWNUM < 11;

oracle dba oracle database management system database management oracle performance tuning oracle count oracle count in oracle sql count oracle sql count in oracle sql database learn sql learn retirement financial advisor retirement financial

The query above returns 10 rows with Rownum value from 1 to 10. The first example will count all those 10 rows and we are doing it with the COUNT(*) way. Take a look at the following query.

 SELECT COUNT(*)
   FROM DUAL
CONNECT BY ROWNUM < 11;

oracle dba oracle database management system database management oracle performance tuning oracle count oracle count in oracle sql count oracle sql count in oracle sql database learn sql learn retirement financial advisor retirement financial

The function above is used as an aggregate function so it returned the value as one row. The SQL output shows the count number 10 as we had the same amount of lines above.

The second example is demonstrating the Count function with your condition. The condition is ROWNUM function and since the function always returns a value per line and we do have 10 rows then the Count returns again number 10.

 SELECT count(ROWNUM)
   FROM DUAL
 CONNECT BY ROWNUM < 11;

oracle dba oracle database management system database management oracle performance tuning oracle count oracle count in oracle sql count oracle sql count in oracle sql database learn sql learn retirement financial advisor retirement financial

The following example shows why can’t we use Oracle SUM to summarise the amount of rows. Using Oracle SUM the amount gets summarised by the column’s value and not by the row’s amount as on the following output.

 SELECT SUM(ROWNUM)
   FROM DUAL
CONNECT BY ROWNUM < 11;

oracle dba oracle database management system database management oracle performance tuning oracle count oracle count in oracle sql count oracle sql count in oracle sql database learn sql learn retirement financial advisor retirement financial

The number 55 came from summarising the following Rownum values over 10 lines:

1+2+3+4+5+6+7+8+9+10 = 55

And using the column value doesn’t return the amount of rows but a value inside them.

Now we’ll have a look what is different between an aggregate and analytic function. The examples above were aggregate functions and the following Select statement is written as an analytic function. The analytic function doesn’t group the values so we left the Rownum function as the first column and the second is Count and an empty analytic condition. The condition should be written into the “OVER” keyword. The current SQL runs the Oracle Count over all lines without any restriction.

 SELECT rownum, COUNT(*) over ()
   FROM DUAL
CONNECT BY ROWNUM < 11;

oracle dba oracle database management system database management oracle performance tuning oracle count oracle count in oracle sql count oracle sql count in oracle sql database learn sql learn retirement financial advisor retirement financial

The aggregate function returned the value as 1 row and the analytic function repeats the value on every row. The total amount 10 is written as second column per every row.

The analytic function can be run with restrictions or an extra clause and the fifth example has the Count function with order by condition in the analytic section. The clause sets to call the Oracle Count function on-fly per row and it only counts left rows depending on the current row. The output below has a count number decreasing per every row so you will know on every row how many rows are still left with out manually counting them.

 SELECT rownum,
  COUNT(*) over (order by rownum DESC)
   FROM DUAL
  CONNECT BY ROWNUM < 11
 ORDER BY 1;

oracle dba oracle database management system database management oracle performance tuning oracle count oracle count in oracle sql count oracle sql count in oracle sql database learn sql learn retirement financial advisor retirement financial

The next example is again the analytic function and this time the restriction is set per value groups. We are using lightly different source query were Rownum value has applied Oracle MOD function and the value is divided by 3. This column has set to the analytic function as a partition (please don’t confuse this with table partitions). The partitions are treated in Oracle Analytic function as value groups so the count is taken only per data group as the following example shows.

 SELECT t.numbers,
  COUNT(*) over (partition BY t.numbers)
   FROM (SELECT mod(rownum,3) numbers
           FROM DUAL
        CONNECT BY ROWNUM < 11 ) t
 ORDER BY t.numbers;

oracle dba oracle database management system database management oracle performance tuning oracle count oracle count in oracle sql count oracle sql count in oracle sql database learn sql learn retirement financial advisor retirement financial

The SQL output above shows repeating Count values per data group. Take a look at the left column first three rows are number “0” and on the right side the count values shows 3. That does mean we do have “3” lines with number 0. The following number group is “1” – 4 rows and number “2” with 3 rows.

See Also:
Oracle Select Oracle Substr Oracle Instr Home