I have a table with a million rows, how do i select the most common(the value which appears most in the table) value from a field?
The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.
To select first 10 elements from a database using SQL ORDER BY clause with LIMIT 10. Insert some records in the table using insert command. Display all records from the table using select statement.
You need to group by the interesting column and for each value, select the value itself and the number of rows in which it appears.
Then it's a matter of sorting (to put the most common value first) and limiting the results to only one row.
In query form:
SELECT column, COUNT(*) AS magnitude FROM table GROUP BY column ORDER BY magnitude DESC LIMIT 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With