How to select random 2 percent rows in mysql? - 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()

Related

SQL limit result to top 50%

I tried two methods but failed in mysql.
/*see top 50% students, but this sql can't work*/
select * from student_table order by chinese_score desc limit count(*) * 0.5 ;
/*also can't work*/
set #num= floor((select count(*) from test.student_score)*0.5);
select * from student_table order by chinese_score desc limit #num ;
How to solve in mysql?
In Mysql this can be done in a single query using user defined variables.
You can store a value in a user-defined variable in one statement and
refer to it later in another statement. This enables you to pass
values from one statement to another.
SELECT * FROM (
SELECT student_table.*, #counter := #counter +1 AS counter
FROM (SELECT #counter:=0) AS initvar, student_table
ORDER BY student_table.chinese_score DESC
) AS result
WHERE counter < (#counter/2) ORDER BY chinese_score DESC;

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 Can I set result set row count using sql query

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

Equivalent of MSSQL with statement in MYSQL

is there any equivalent to the following statement in mysql ?
With Tmp1 as (
Select Distinct EmpID, TypeID
From tb_deductionBalance
), Tmp2 as (
Select *,
row_number() OVER ( order by empID /* no employeeID in Tmp1 */) as RowNum
From Tmp1
)
Select * From Tmp2
Where RowNum Between #Start and #End
I have to migrate an mssql database to mysql and there are plenty of such statements which would take much more time to recreate in mysql if it cannot be translated.
Thanks
SELECT DISTINCT EmpID, TypeID
FROM tb_deductionBalance
ORDER BY empID
LIMIT YourStartNumber - 1, YourEndNumber - YourStartNumber
Unfortunately, you can't use variables in LIMIT, so you'll need to use actual integers there. For instance, if you want results from the rows 3 to 10, you'll need to use:
SELECT DISTINCT EmpID, TypeID
FROM tb_deductionBalance
ORDER BY empID
LIMIT 2, 7