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()
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.
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
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
i have a table MEN in sql server 2008 that contain 150 rows.
how i can show only the even or only the odd rows ?
Check out ROW_NUMBER()
SELECT t.First, t.Last
FROM (
SELECT *, Row_Number() OVER(ORDER BY First, Last) AS RowNumber
--Row_Number() starts with 1
FROM Table1
) t
WHERE t.RowNumber % 2 = 0 --Even
--WHERE t.RowNumber % 2 = 1 --Odd
Assuming your table has auto-numbered field "RowID" and you want to select only records where RowID is even or odd.
To show odd:
Select * from MEN where (RowID % 2) = 1
To show even:
Select * from MEN where (RowID % 2) = 0
FASTER: Bitwise instead of modulus.
select * from MEN where (id&1)=0;
Random question: Do you actually use uppercase table names?
Usually uppercase is reserved for keywords. (By convention)
odd number query:
SELECT *
FROM ( SELECT rownum rn, empno, ename
FROM emp
) temp
WHERE MOD(temp.rn,2) = 1
even number query:
SELECT *
FROM ( SELECT rownum rn, empno, ename
FROM emp
) temp
WHERE MOD(temp.rn,3) = 0
For even values record :
select * from www where mod(salary,2)=0;
For odd values record:
select * from www where mod(salary,2)!=0;
Try this :
odd :
select * from(
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber',
FROM table1
) d where (RowNumber % 2) = 1
even :
select * from(
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber',
FROM table1
) d where (RowNumber % 2) = 0
SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) row_number, sr,sal FROM empsal) a WHERE (row_number%2) = 1
and
SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) row_number, sr,sal FROM empsal) a WHERE (row_number%2) = 0
SELECT *
FROM
(
SELECT rownum rn, empno, ename
FROM emp
) temp
WHERE MOD(temp.rn,2) = 1
select * from Tablename
where id%2=0
Following is for fetching even number::
Select * from MEN where Men_ID%2=0;
Following is for fetching odd number::
Select * from MEN where Men_ID%2!=0;
Here MEN is your table_name Men_ID is the column in MEN Table.
Try following
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
Here’s a simple and straightforward answer to your question, (I think).
I am using the TSQL2012 sample database and I am returning only even or odd rows based on “employeeID” in the “HR.Employees” table.
USE TSQL2012;
GO
Return only Even numbers of the employeeID:
SELECT *
FROM HR.Employees
WHERE (empid % 2) = 0;
GO
Return only Odd numbers of the employeeID:
SELECT *
FROM HR.Employees
WHERE (empid % 2) = 1;
GO
Hopefully, that’s the answer you were looking for.
To fetch even records
select *
from (select id,row_number() over (order by id) as r from table_name) T
where mod(r,2)=0;
To fetch odd records
select *
from (select id,row_number() over (order by id) as r from table_name) T
where mod(r,2)=1;
Oracle Database
ODD ROWS
select * from (select mod(rownum,2) as num , employees.* from employees) where num =0;
EVEN ROWS
select * from (select mod(rownum,2) as num , employees.* from employees) where num =1;
To select an odd id from a table:
select * from Table_Name where id%2=1;
To select an even id from a table:
select * from Table_Name where id%2=0;
We can achieve by this
Query To Find ODD Records
/*Query To Find ODD Result using CTE */
WITH EVEN_ODD_RESULT AS
(
select *, ROW_NUMBER() OVER (ORDER BY CountryID) AS ROWNUM
FROM schema.Country_TBL
)
SELECT * FROM EVEN_ODD_RESULT
WHERE (EVEN_ODD_RESULT.ROWNUM % 2) =1
Query To Find EVEN Records
/*Query To Find EVEN Result using CTE */
WITH EVEN_ODD_RESULT AS
(
select *, ROW_NUMBER() OVER (ORDER BY CountryID) AS ROWNUM
FROM schema.Country_TBL
)
SELECT * FROM EVEN_ODD_RESULT
WHERE (EVEN_ODD_RESULT.ROWNUM % 2) = 0
Thank You
for SQL > odd:
select * from id in(select id from employee where id%2=1)
for SQL > Even:
select * from id in(select id from employee where id%2=0).....f5