Is there an alternative to TOP in MySQL? - sql

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

Related

How to select rows between x and y in Redshift

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.

How can I correctly format "select top 1000 * from schema.table_name;" to work in Toad 4 Apache Hadoop?

Trying to run this in Toad 4 Apache Hadoop (Hive), and it does not recognize the top function. How can I reformat this?
select top 1000 * from Finance.ACCT_LIST
You can use LIMIT:
select * from Finance.ACCT_LIST
order by somecolumn
limit 1000
You should add an order by clause if you want to get a consistent set of rows.

Cannot figure out how to use MODULO/MODULUS function in Caché SQL

Here is the documentation.
I'm using DB Visualizer Pro 9.2.6
I've tried multiple variants including the following
SELECT TOP 10 * FROM tableName WHERE {ID MOD(5,3)}
SELECT TOP 10 * FROM tableName WHERE ID MOD(5,3)
SELECT TOP 10 * FROM tableName WHERE ID MOD 5 = 3
SELECT TOP 10 * FROM tableName WHERE ID % 5 = 3
In all honesty the documentation makes no sense to me anyway... Is "fn" field name? I assume it's a function but then what field are they selecting? It's just confusing...
It appears from the examples that fn should be included, within the curly brackets:
The following example shows the remainder returned by MOD.
SELECT DISTINCT {fn MOD(5,3)} AS Remainder
FROM Sample.Person
returns 2
The following example shows the remainder returned by MOD with a negative dividend.
SELECT DISTINCT {fn MOD(-5,3)} AS Remainder
FROM Sample.Person
returns 1
If you check out another function, like ROUND they use the same syntax (among others).
So in your case, maybe something like (assuming you want records satisfying 5 % ID == 3)
SELECT TOP 10 * FROM tableName WHERE {fn MOD(ID,5)} = 3
Confusing? Yes.
There is also a binary operator for modulo: #
http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GSQL_langelements#GSQL_langelements_ops_math

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".

How can I get sets of 2 entries where each set starts with a different letter and no letter is repeated from a sqlite database?

Like this:
apple
aardvark
banana
bet
cow
car
...
zipper
zoo
Assuming the database has more than just two different entries that start with any of the letters. I was thinking of doing something with TOP and wildcards, but I don't really know enough about SQL to pull this off. What can I do?
You can do this with the substr function and a correlated subquery:
SELECT *
FROM YourTable a
WHERE wordField IN (SELECT wordField
FROM YourTable AS b
WHERE substr(a.wordField ,1,1) = substr(b.wordField ,1,1)
ORDER BY wordField
LIMIT 2)
Demo: SQL Fiddle
You can use the ORDER BY to adjust which 2 records are returned. Like ORDER BY RANDOM() if that's supported.