How Can I set result set row count using sql query - mysql

I wanted to set the number rows returned per query say 5. How can I set this in the sql query.
Can any one please help me

Highly dependent on what RDBMS you are using.
For Oracle
SELECT * FROM the_table WHERE ROWNUM < 6
(since 12c there is another option, too).
For Postgresql
SELECT * FROM the_table LIMIT 5

According to the MySQL manual you can do this by adding LIMIT statement to your query:
SELECT * FROM tbl_name
LIMIT offset, row_numbers
or
SELECT * FROM tbl_name
LIMIT row_numbers OFFSET offset
offset option is very useful in case of pagination.

SELECT TOP 5 *
FROM dbo.MyTable

As someone suggest zou can use:
select top X from table_name
where X is the numer of rows that you want
or you can use row_number
With cte AS
( SELECT *,
ROW_NUMBER() OVER (order by table_column) as RowNumber
FROM table_name)
select *
from cte
Where RowNumber <= 5
or even:
With cte AS
( SELECT *,
ROW_NUMBER() OVER (order by table_column) as RowNumber
FROM table_name)
select *
from cte
Where RowNumber between 5 and 10

Related

How to select random 2 percent rows in mysql?

How to get a random 2% sample from a table in MySQL?
I tried the following queries
SELECT orderID
FROM orders
ORDER BY rand()
LIMIT 1 OFFSET (SELECT convert(0.02 * count(*), unsigned) FROM orders)
;
SELECT orderID
FROM orders
ORDER BY rand()
LIMIT (SELECT convert(0.02 * count(*), unsigned) FROM orders)
;
Both of them give errors. Could someone explain error and alternate methods.
Database: Northwind
MySQL version: 8.0
I would use ROW_NUMBER here:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY RAND()) rn, COUNT(*) OVER () cnt
FROM orders
)
SELECT *
FROM cte
WHERE rn < 0.02 * cnt; -- select first 2% of a random sample
If approximately 2% is sufficient, then you can just use:
SELECT orderID
FROM orders
WHERE rand() <= 0.02;
Because no sorting is required, this is probably the fastest method.
Do note that the result is not exactly 2% but on a large table it will be close enough.
You can use MySQL Stored Procedure to create a function inside our MySQL.
https://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx/
CREATE PROCEDURE GetRand2PercentSample()
BEGIN
DECLARE nLimit INT DEFAULT 0;
SELECT COUNT(*) * 0.2
INTO nLimit
FROM orders;
SELECT * FROM orders ORDER BY RAND() LIMIT nLimit;
END
// to call the function
call GetRand2PercentSample()

LIMIT(SELECT...) IN SQL?

Is this syntax allowed in sql (any DMS):
SELECT rows from table limit (select count(*) from table where value = 7)
In other words, is having a select statement within a limit allowed? Why or why not?
The syntax is not allowed in MySQL, not without using dynamic SQL to build the LIMIT clause. On MySQL 8+, we can try the following using ROW_NUMBER:
SELECT *
FROM
(
SELECT *, ROW_NUMBER() OVER (ORDER BY some_col) rn
FROM yourTable
) t
WHERE rn < (SELECT COUNT(*) FROM yourTable WHERE value = 7);
Postgres supports this syntax. For example:
create table n as
values (1), (2) ;
select *
from (values ('a'), ('b')) v(x)
limit (select count(*) from n)
Here is a db<>fiddle.
However, most databases require that the value for limit (or the equivalent construct) be defined at compile time.
Your syntax is not allowed in any database engine. limit requires fix values not varying one (applicable in sqlite and postgresql)
SELECT rows from table limit (select 10)
see dbfiddle

Select limit percent

I want
example 10% of records in table
not 10 records
This query run in SQL Server
select top 10 percent * from tablename
Why this query in MySQL do not run?
select top 10 percent * from tablename
You could do it with a subquery, this is pretty basic since you want everything in one table:
SELECT *
FROM (
SELECT tablename.*, #counter := #counter +1 AS counter
FROM (select #counter:=0) AS initvar, tablename
ORDER BY value DESC
) AS X
where counter <= (10/100 * #counter);
ORDER BY value DESC
For MySQL use order by or limit
select * from tablename order by percent desc limit 10
TOP clause works on MSSQL server not sql.

Turn MySQL query to SQL Server

I have this query in MySQL:
Select *
From Customer
Order By ID DESC Limit 1,1
How to use this query for SQL server ?
For SQL Server 2005 and up:
;WITH cte AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY ID DESC) AS RowNumber
FROM Customer
)
SELECT *
FROM cte
WHERE RowNumber = 2
MSSQL, use this query to fetch Nth record
SELECT * FROM (
SELECT
*, ROW_NUMBER() OVER(ORDER BY userID) AS ROW
FROM tblUser
) AS TMP
WHERE ROW = n
In SQL Server 2012 or higher, you can also use Offset and Fetch:
Select *
From Customer
Order By ID Desc
Offset 1 Rows
Fetch Next 1 Rows Only

how to select rows of a table in 3th multiply order in sql like 3,6,9 etc

My table consists of 1024 rows and two columns.
I want to select and display rows like 3,4,6,etc..
Is it possible in sql query.
If possible what is the code for that..
In a relational database there is no concept of the "3rd" or "6th" row unless you can define a sort order.
Assuming you do have a column by which you can order the result in order to define a row as "3rd", or "4th", the following will work with Postgres:
select *
from (
select col1, col2, col3,
row_number() over (order by your_sort_column) as rn
from your_table
) t
where rn in (3,4,6);
Where your_sort_column is the one that defines the order or rows. This could be an incremental id or a timestamp column that stores the time of insert or update of the row.
In MySQL it can be accomplished as follows:
SELECT * from
(SELECT (#row := #row + 1) num, tab.* FROM your_table, (SELECT #row := 0) r
ORDER by your_sort_column) t
WHERE (num % 3) = 0
We have used a MODULO operator to get row which is at multiple of 3.
See it working at SQL Fiddle: http://www.sqlfiddle.com/#!2/192b0/4
In PostgreSQL it can be accomplished as follows:
select *
from (
select *, row_number() over (order by your_sort_column) as rn
from your_table
) t
where (rn % 3)=0 ;
http://www.sqlfiddle.com/#!15/cc649/5