Say I have 100 records in a certain column in a certain table.
All of these pieces of data in that column are random numbers from 1 to 10
What SQL function can I use to count the number that appears the most within those 100 records and it will display that number alone in the column?
How do I do this? Thanks
Assuming you're using mysql (because of question tags):
SELECT n
FROM tablename
GROUP BY n
ORDER BY COUNT(*) DESC
LIMIT 1
Try a query like this to get the count:
select count(*)
from t
group by col
order by count(*) desc
limit 1
This is MySQL syntax. The limit 1 is database-specific. In SQL Server, for instance, it would be select top 1.
And this to get the number in the column:
select col
from t
group by col
order by count(*) desc
limit 1
Related
I have table Test1 => ID(INT), NAME(VARCHAR) having
values like (1,'One'), (2,'two') ..... (51,'Fifty-one')
I want sum of ID of last 5 rows whose ID is divisible by 5. I tried following query but not getting any output:
SELECT SUM(ID) FROM Test1 WHERE id%5 = 0 LIMIT 5 OFFSET (SELECT COUNT(*) FROM Test1)
So answer should be 50+45+40+35+30=200
You should never use LIMIT without ORDER BY. Only with ORDER BY is the order in your result set guaranteed and only then LIMIT makes sense.
Moreover you use SUM without a GROUP BY. That gives you one result row. Then you use LIMIT on your results, which is still one result row.
And what is the offset supposed to do? You want to start after the last record in the table? That doesn't seem to make sense.
Here is the query with ORDER BY and SUM after LIMIT:
select sum(id)
from
(
select id
from test1
where id % 5 = 0
order by id desc
limit 5
) last5;
I'm trying to select the 5 rows with the highest count value
This is my query:
string sql = "SELECT top 5 count FROM Likes ORDER BY COUNT(*) DESC";
It's just throwing an error code that
Column 'Likes.count' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
It's for a project I've got to present tomorrow...
On SQL Server, simply do this:
SELECT TOP 5 * FROM Likes ORDER BY [Count] DESC
This assumes that your Likes-table already contains a column named [Count] meaning that you don't need to count the records yourself (which is what COUNT(*) does).
You should not use COUNT(*) here for order by.
SELECT top 5 [count] FROM Likes ORDER BY [Count] DESC
count is a reserved word which is why you should stay clear of using them for column names. If you don't want to rename the column you can escape it, different dbms may effect who you do this. In ssms you would use square brackets.
string sql = "SELECT top 5 [count] FROM Likes ORDER BY COUNT(*) DESC";
I am working on python sqlite3.
This statement gets records 5 - 14;
SELECT * FROM something LIMIT 5, 10;
But how do I get, lets say the first five and last five records through a single statement?
You can combine output of two select statement like this:
(SELECT * FROM `something` order by some_column_name
limit 0,5)
union
(SELECT * FROM `something` order by some_column_name desc
limit 0,5
)
Specify some ordering of rows, so that it will select rows accordingly.
Maybe the better way is using rowid in your order by clause to get the first and last rows based on inserting the rows:
select test from
(select test,rowid from table1 order by rowid asc limit 0,5)t1
union all
select test from
(select test,rowid from table1 order by rowid desc limit 0,5)t2;
Here is a sample in SQL Fiddle
If I say:
select * from table order by col1 where rownum < 100
If the table has 10 million records, will Oracle bring all 10 million, sort it and then show me the first 10? Or is there a way it will optimise it?
If you do this
select * from table order by col1 where rownum < 100
then Oracle will throw an error as the WHERE clause comes before the ORDER BY.
If you do this
select * from table where rownum < 100 order by col1
then Oracle will return a random 99 records as the WHERE clause comes before the ORDER BY.
If you want to return a the first 100 records, ordered by a column, you must put the order by in a sub-select.
select *
from ( select * from table order by col1 )
where rownum <= 100
Oracle will do the sort, how else will it know the records you want? However, it will be a sort with a stopkey because of the ROWNUM. Oracle doesn't actually sort the entire result set, as some optimisation goes on under the hood, but this is what you can assume takes place.
Please see this article by Tom Kyte.
I am working with mysql. I need queries that fetch a list of maximum values.
I know how to get one maximum value i.e using max(colName) in select query.
Is there some similar query that can fetch more than 1 maximum values like the top 10 values.
select * from table order by colName desc limit 10;
SELECT * FROM table
ORDER by price DESC
LIMIT 10
try
select distinct colName from tablename order by colName desc limit 10;