totn Oracle / PLSQL

Oracle / PLSQL: Retrieve the value that occurs most in a column

Question: When I do a select on a table, I want to know which value occurs the most in a certain column?

Answer: To retrieve the value that occurs the most in a certain column, you could try executing the following select statement:

select cnt1.column_name
from (select COUNT(*) as total, column_name
      from table_name
      group by column_name) cnt1,
     (select MAX(total) as maxtotal
      from (select COUNT(*) as total, column_name from table_name group by column_name)) cnt2
where cnt1.total = cnt2.maxtotal;

You will need to replace table_name with the name of your table and column_name with the name of your column.