SQLite LIMIT OFFSET and WHERE clause - sql

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;

Related

Fetch first 5 and last 5 records through a single statement

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

Select top nth row (or last row if there's fewer than n rows)

I have a table with a numeric score, and I want to select the value of the 1000th highest score. Normally I would do
SELECT score FROM table ORDER BY score DESC LIMIT 1 OFFSET 999;
However, there may not be 1000 rows in the table yet. Of course I could COUNT(*) first, but is there another way? I want to get the 1000th row, or the last row if there are not yet 1000 rows.
select score
from (
select score
from table
order by score desc
limit 1000
) s
order by score
limit 1

SQL Function to count data

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

What is the best and simplest SQL Query to get the Best 3 scores and their corresponding details.

I have a table 'results' in which the scores are stored. Its ordered by the date of `test'. Now i need to get the details of the users who have secured 1st , 2nd and 3rd in the test.
i suppose that to get the 1st score i can use:
select *
from RESULTS
where SCORE=max(SCORE)
But what about for 2nd and 3rd.
Also is it possible to get the three of them together in a single query.
You could try (MySql)
SELECT * FROM results
ORDER BY score DESC
LIMIT 3
or (MS-SQL)
SELECT TOP 3 * FROM results
ORDER BY score DESC
or (Oracle)
SELECT * FROM
(SELECT * FROM results
ORDER BY score DESC) t
WHERE ROWNUM < 4

How do i filter a sql table by the TOP 1?

I have a query that is filtered by SO Number. It also has a column that has a unique number generated each time the SO is updated. How can i alter my code so that not only will it be filtered by the SO Number, but also filter by the TOP 1, or highest count of the updated key?
Thank you!
This is on SQL Server. Should have specified earlier
SELECT whatever_you_want
FROM whereever_it_is
WHERE your_criteria
ORDER BY so_number DESC
LIMIT 1
which will give you the "highest" so_number, returning only one record even if there are several with the same value
or
SELECT whatever_you_want
FROM whereever_it_is
WHERE your_criteria
AND so_number == MAX(so_number)
which will give all rows with that maximum value, returning all if there are more than one.
SELECT TOP 1 SONumber
FROM ExampleTable
ORDER BY SONumber DESC
Or
SELECT MAX(SONumber)
FROM ExampleTable