How to Find Most Frequent Value in SQL Column?
Last Updated :
02 Apr, 2023
In SQL, sometimes we need to find frequent values in a column. For this we use a following steps.
So, let’s start by creating a database first.
Step 1: Create a Database.
Query :
CREATE DATABASE GFG
Step 2: Use the GFG Database.
Query :
USE GFG
Step 3: Create a table.
Create a table (GetRepeatedValue) to store the data.
Query :
CREATE TABLE GetRepeatedValue(Value INT);
Step 4: Insert some data into the database.
Query :
INSERT INTO GetRepeatedValue
VALUES
(2),
(3),
(4),
(5),
(6),
(4),
(6),
(9),
(6),
(8);
Step 5: SQL query to select the most repeated value in the column.
Count each value using the count aggregate function in SQL. Then sort those values in descending order. Get the most repeated by selecting the top 1 row.
Query:
SELECT
TOP 1 Value,
COUNT(Value) AS count_value
FROM
GetRepeatedValue
GROUP BY
Value
ORDER BY
count_value DESC;
Output:
In the GetRepeatedValue table, 6 is the most repeated in the value column.
Share your thoughts in the comments
Please Login to comment...