How to get random results in SQL with different SGDBs?

5

Based on this existent question in SOen and wanting to bring interesting and useful content to SOpt I ask this question:

How to get random results in SQL with different SGDBs?

  

I'm leaving a ready answer, but I'm going to leave it open, if they mess with better or more performative solutions

    
asked by anonymous 26.04.2018 / 17:47

2 answers

4

Some of the examples were taken from this SOen response from @ YaakovEllis

  

I've already warned you that this has not been done performance testing, which can probably be a bit tricky if you need to check out many things. I want to run my own tests and maybe suggest different ways.

     

Another detail, the use of LIMIT , TOP 1 , rownum , etc is only to indicate that there was limitation of results, after all the intention is to get random results and not only to order in a random fashion

MySQL

Select random MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

PostgreSQL

Select random in PostgreSQL:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

SQL Server

Select random in Microsoft SQL Server:

SELECT TOP 1 column FROM table
ORDER BY NEWID()

IBM DB2

Select random in IBM DB2

SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX FETCH FIRST 1 ROWS ONLY

Oracle

Select Random in Oracle:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

SQLite

Select random in Sqlite:

SELECT column FROM table
ORDER BY RANDOM() LIMIT 1
    
26.04.2018 / 17:47
0

In SQL Server, if you do not need to select all rows and only a sample rows and performance is important, use the following:

SELECT * FROM table
  WHERE (ABS(CAST(
  (BINARY_CHECKSUM(*) *
  RAND()) as int)) % 100) < 10

The idea here is to generate a random number between 0 and 99 and then select all that are smaller than 10. That way selecting ~ 10% of the base. You can change the 100 of the query by another value if you need to pick up portions smaller than 1% of the base.

Using ORDER BY newid() can be problematic because ORDER BY causes all table rows to be copied to tempdb and this is slow because it can be very IO, and if the base is too large, it can crash the tempdb.

    
06.05.2018 / 01:46