count top 10 most occuring values in a column in mysql - mysql

I have a column in mysql table that has the the data type INT(11).
How can I search to get the top 10 most occurring values in this column?

SELECT col, count(*)
FROM tablethingie
GROUP BY col
ORDER BY count(*) DESC
LIMIT 10

Try the following code
SELECT colname, COUNT(*) AS cnt
FROM tablename
GROUP BY colname
ORDER BY cnt DESC
LIMIT 10

TOP is a keyword which is not supported in MySQL, it is in MSSQL though.
This following query should do what you want (untested, but the idea should become clear):
SELECT column, COUNT(*) AS matches
FROM table
GROUP BY column
ORDER BY matches DESC
LIMIT 10

Try:
SELECT ColName, Count(1) AS occurances
FROM
table
GROUP BY
ColName
ORDER BY
occurances DESC
LIMIT
10

Related

want to get total count of records in a table using COUNT(*) in MySQL

I have a mysql query which will return all the details from table along with i need max_row count i.e total no of rows in a table using COUNT(*) in a single select query without using cross join.
Note: MySQL version is earlier version of 8
Query :
SELECT * FROM tablename ORDER BY column name DESC LIMIT 0,10;
The total count of a table is simple, when you want to add it to every row.
SELECT
*
,(SELECT COUNT(*) FROM tablename ) count1
FROM tablename
ORDER BY column name
DESC LIMIT 0,10;,

Getting minimum of top n rows

I wish to get min(somecolumn) from table from first n rows in MySQL. What is the best query to get the result?
So far I found
select min(a.column) from (select column from table limit 2000) a
select min(a.column) from table a INNER JOIN (select column from table b limit 2000) on a.pricolumn = b.pricolumn.
select min(t.columnName) from tableName as t limit 10 (Here n limit is for first 10 rows as example)
OR
select outerTable.columnName from (select distinct t.columnName from tableName as t order by t.columnName asc limit 10) as outerTable limit 1,1
Try the below syntax:
SELECT min(columname) FROM tablename limit n

How to find the most common and least common numbers

I have a database which consists of 5 columns
Each column is an INT
I want to find out which numbers occur most frequently in each column
I also wanted to know which sequence of numbers occurs more frequently
The same thing is true with the least common
I would like to use MYSQL or SQLITE
assuming schema as follows
tbl(..., int_col1, int_col2, int_col3, int_col4, int_col5, ...)
most frequently
do following query for each column
SELECT int_col1, COUNT(int_col1)
FROM tbl
GROUP BY int_col1
ORDER BY COUNT(int_col) DESC LIMIT 1
least frequently
do following query for each column
SELECT int_col1, COUNT(int_col1)
FROM tbl
GROUP BY int_col1
ORDER BY COUNT(int_col) ASC LIMIT 1
sequence of numbers occurs more frequently
SELECT int_col1, int_col2, int_col3, int_col4, int_col5, COUNT(*)
FROM tbl
GROUP BY int_col1, int_col2, int_col3, int_col4, int_col5
ORDER BY COUNT(*) DESC;
assuming column is userid
select UserID, count(UserID)
from myUsers
group by UserID
order by count(UserID) desc
OR
with cte as
(
select user_id,ROW_NUMBER() over (order by UserID) as rn
)
select user_id,count(user_id) as se_count from cte group by user_id

how do I get most recent 10 calls in group concat with a query?

Here is my table structure
phone_calls(id, phone_number, call_id, timestamp, colx, col y )
I want to retrieve 10 most recent calls from phone_calls table within a group concat, without sub query
Try this (without a subquery it will not work):
SELECT
GROUP_CONCAT(call_id)
FROM (
SELECT
call_id
FROM
phone_calls
ORDER BY
id DESC
LIMIT 10
) as tmp
UPDATE: without sub-query:
SET #c:='';
SELECT
#c:=CONCAT(#c,',',call_id)
FROM
phone_calls
ORDER BY
id DESC
LIMIT 10;
SELECT #c;
Ok I'm not sure if this is your problem, but to use group_concat you need a group by column right? Not sure if this will work but you can try it, I used this dummy col way before
select 1 as col, group_concat(call_id) as latest_calls from phone_calls
ORDER BY timestamp DESC GROUP BY col LIMIT 10
you'll have an extra useless column col, but latest_calls should be correct

Find most frequent value in SQL column

How can I find the most frequent value in a given column in an SQL table?
For example, for this table it should return two since it is the most frequent value:
one
two
two
three
SELECT
<column_name>,
COUNT(<column_name>) AS `value_occurrence`
FROM
<my_table>
GROUP BY
<column_name>
ORDER BY
`value_occurrence` DESC
LIMIT 1;
Replace <column_name> and <my_table>. Increase 1 if you want to see the N most common values of the column.
Try something like:
SELECT `column`
FROM `your_table`
GROUP BY `column`
ORDER BY COUNT(*) DESC
LIMIT 1;
Let us consider table name as tblperson and column name as city. I want to retrieve the most repeated city from the city column:
select city,count(*) as nor from tblperson
group by city
having count(*) =(select max(nor) from
(select city,count(*) as nor from tblperson group by city) tblperson)
Here nor is an alias name.
Below query seems to work good for me in SQL Server database:
select column, COUNT(column) AS MOST_FREQUENT
from TABLE_NAME
GROUP BY column
ORDER BY COUNT(column) DESC
Result:
column MOST_FREQUENT
item1 highest count
item2 second highest
item3 third higest
..
..
For use with SQL Server.
As there is no limit command support in that.
Yo can use the top 1 command to find the maximum occurring value in the particular column in this case (value)
SELECT top1
`value`,
COUNT(`value`) AS `value_occurrence`
FROM
`my_table`
GROUP BY
`value`
ORDER BY
`value_occurrence` DESC;
Assuming Table is 'SalesLT.Customer' and the Column you are trying to figure out is 'CompanyName' and AggCompanyName is an Alias.
Select CompanyName, Count(CompanyName) as AggCompanyName from SalesLT.Customer
group by CompanyName
Order By Count(CompanyName) Desc;
If you can't use LIMIT or LIMIT is not an option for your query tool. You can use "ROWNUM" instead, but you will need a sub query:
SELECT FIELD_1, ALIAS1
FROM(SELECT FIELD_1, COUNT(FIELD_1) ALIAS1
FROM TABLENAME
GROUP BY FIELD_1
ORDER BY COUNT(FIELD_1) DESC)
WHERE ROWNUM = 1
If you have an ID column and you want to find most repetitive category from another column for each ID then you can use below query,
Table:
Query:
SELECT ID, CATEGORY, COUNT(*) AS FREQ
FROM TABLE
GROUP BY 1,2
QUALIFY ROW_NUMBER() OVER(PARTITION BY ID ORDER BY FREQ DESC) = 1;
Result:
Return all most frequent rows in case of tie
Find the most frequent value in mysql,display all in case of a tie gives two possible approaches:
Scalar subquery:
SELECT
"country",
COUNT(country) AS "cnt"
FROM "Sales"
GROUP BY "country"
HAVING
COUNT("country") = (
SELECT COUNT("country") AS "cnt"
FROM "Sales"
GROUP BY "country"
ORDER BY "cnt" DESC,
LIMIT 1
)
ORDER BY "country" ASC
With the RANK window function, available since MySQL 8+:
SELECT "country", "cnt"
FROM (
SELECT
"country",
COUNT("country") AS "cnt",
RANK() OVER (ORDER BY COUNT(*) DESC) "rnk"
FROM "Sales"
GROUP BY "country"
) AS "sub"
WHERE "rnk" = 1
ORDER BY "country" ASC
This method might save a second recount compared to the first one.
RANK works by ranking all rows, such that if two rows are at the top, both get rank 1. So it basically directly solves this type of use case.
RANK is also available on SQLite and PostgreSQL, I think it might be SQL standard, not sure.
In the above queries I also sorted by country to have more deterministic results.
Tested on SQLite 3.34.0, PostgreSQL 14.3, GitHub upstream.
Most frequent for each GROUP BY group
MySQL: MySQL SELECT most frequent by group
PostgreSQL:
Get most common value for each value of another column in SQL
https://dba.stackexchange.com/questions/193307/find-most-frequent-values-for-a-given-column
SQLite: SQL query for finding the most frequent value of a grouped by value
SELECT TOP 20 WITH TIES COUNT(Counted_Column) AS Count, OtherColumn1,
OtherColumn2, OtherColumn3, OtherColumn4
FROM Table_or_View_Name
WHERE
(Date_Column >= '01/01/2023') AND
(Date_Column <= '03/01/2023') AND
(Counted_Column = 'Desired_Text')
GROUP BY OtherColumn1, OtherColumn2, OtherColumn3, OtherColumn4
ORDER BY COUNT(Counted_Column) DESC
20 can be changed to any desired number
WITH TIES allows all ties in the count to be displayed
Date range used if date/time column exists and can be modified to search a date range as desired
Counted_Column 'Desired_Text' can be modified to only count certain entries in that column
Works in INSQL for my instance
One way I like to use is:
select *<given_column>*,COUNT(*<given_column>*)as VAR1 from Table_Name
group by *<given_column>*
order by VAR1 desc
limit 1