How to select rows between x and y in Redshift - sql

I can select the TOP rows from a Redshift table by using the TOP clause. But I would like to select the rows between, say, 10 and 20. I am using the ORDER BY clause, so the resultset is consistent.
How do I do that?
I would ideally like something like:
SELECT * BETWEEN 10 AND 20
FROM <tablename>

The more traditional method in Redshift would be LIMIT/OFFSET. For the second set of 10 rows:
select t.*
from t
order by XXX
limit 10 offset 9;
Note that the offset is one less than the row where you want to start (i.e. offset 0 = "first row").
This is also the syntax used in Postgres.

Related

Using offset in SQL to count column or sum is empty in postgresql

Using PostgreSQL 10.6 (RedHat)
When I am using this SQL to sum or count data like this
select count(1)
from test
limit 10
it also returns the all data count?
If I am using this SQL:
select count(1)
from test
offset 10 limit 10 ; -- return empty
or this SQL:
select sum(some_column)
from test
offset 10 limit 10 -- return empty
I am surprised.
What is confusing? An aggregation query with no group by always returns one row.
If you try to take the first ten rows . . . well, you get the one row.
If you try to take the eleventh row . . . well, there is none.

PostgreSQL:How get last rows from a select query

See the below example,
create table data(name varchar, value int);
insert into data values('joe',1);
insert into data values('bob',2);
insert into data values('jane',3);
insert into data values('anne',4);
insert into data values('kate',5);
And if I Execute
select * from data limit 2;
Will Get
name | value
------+-------
joe | 1
bob | 2
(2 rows)
So,How Can I Get the Last 2 Rows in select * from data?
What I'm expecting is....
name | value
------+-------
anne | 4
kate | 5
(2 rows)
You have two options according to your need i.e,
select * from data order by value desc limit 2
Or
LIMIT and OFFSET
if you want the 4th and 5th row just offset the first 3 so that the 4th row becomes the start of our set and you can specify a limit to say that you only want 2 rows from that.
select * from data offset 3 limit 2;
/* The order of LIMIT and OFFSET does not matter. This gives the same result */
select * from data limit 2 offset 3;
I know I'm answering a six year old question and I know the accepted answer says to use an offset, however that's only going to be useful if you know the length of the table. Looking at the wording of the question and the example given, I assumed that like myself, the author wanted the last entries in a table based on an id or other value in the order that they were entered.
The best solution I've found so far that orders the values as expected is using a subquery as follows:
SELECT * FROM ( SELECT * FROM data ORDER BY VALUE DESC LIMIT 2) AS _ ORDER BY VALUE ASC;
You'd substitute VALUE for whichever column you'd want to sort by to get the "last" entries. In my experience, this method is an order of magnitude quicker than using a count to find an offset.
To get the x last rows, example with x=10,
use offset alone, with count:
SELECT *
FROM data
ORDER BY value ASC
OFFSET (SELECT count(*) FROM DATA)-10
I let you check if the offset is not negative...
Make use of Order by Clause
select * from data order by value desc limit 2;
OR
select top 2 * from data order by value desc ;
You can achieve it using Order by clause desc
SELECT *
FROM data
ORDER BY value DESC limit 2;
like does Krishraj Rana you can try this function for get the last row with "limit 1" and order by "x col" desc, complete it "by x_col desc limit 1".

SQL Server SQL Select: How do I select rows where sum of a column is within a specified multiple?

I have a process that needs to select rows from a Table (queued items) each row has a quantity column and I need to select rows where the quantities add to a specific multiple. The mulitple is the order of between around 4, 8, 10 (but could in theory be any multiple. (odd or even)
Any suggestions on how to select rows where the sum of a field is of a specified multiple?
My first thought would be to use some kind of MOD function which I believe in SQL server is the % sign. So the criteria would be something like this
WHERE MyField % 4 = 0 OR MyField % 8 = 0
It might not be that fast so another way might be to make a temp table containing say 100 values of the X times table (where X is the multiple you are looking for) and join on that

Is there an alternative to TOP in MySQL?

I want to know the alternative of the TOP keyword as in MySQL. I have read about TOP in SQL Server.
Is there any alternative to this in MySQL, or any other method in MySQL from which we can get same functionality?
Ordering and limiting the results:
SELECT field1, field2
FROM myTable
ORDER BY field1 ASC
LIMIT 10
You can use the LIMIT keyword (See the documentation of the SELECT instruction) -- it goes at the end of the query :
select *
from your_table
where ...
limit 10
to get the top 10 lines
Or even :
select *
from your_table
where ...
limit 5, 10
To get 10 lines, startig from the 6th (i.e. getting lines 6 to 15).
yes, there is the limit clause.
Example:
SELECT * FROM `your_table` LIMIT 0, 10
This will display the first 10 results from the database.
mysql equivalent of top and you can find further more about LIMIT in MySql Doc

In SQL, how do you get the top N rows ordered by a certain column?

I want to select the top N rows of a table, ranked by how high the number in one of their columns is.
I already have:
SELECT * FROM movie ORDER BY worldwide_gross DESC;
How can I get the first twenty?
If it makes any difference I'm using MySQL.
Cheers!
Definition: Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results. It is phrased as Limit X, Y and included at the end of your query. X is the starting point (remember the first record is 0) and Y is the duration (how many records to display).
Also Known As: Range Results
Examples:
SELECT * FROM `your_table` LIMIT 0, 10
This will display the first 10 results from the database.
SELECT * FROM `your_table` LIMIT 5, 5
This will show records 6, 7, 8, 9, and 10
More from About.com
I believe:
SELECT *
FROM movie
ORDER BY worldwide_gross DESC
LIMIT 20
should do the trick. See also this link.