RANDOM()

SQL · Math Functions

Theory

RANDOM returns a random value each time it is evaluated, typically once per row. Its most common use is random sampling: ORDER BY RANDOM() LIMIT 3 gives each row a random sort key, shuffles the table and keeps the first three, so you get a different three employees on every run.

The exact function and its output differ by database. PostgreSQL's RANDOM() returns a decimal between 0 and 1. SQLite's RANDOM() returns a large signed integer, so you use ABS(RANDOM()) % 100 for a 0–99 value. MySQL uses RAND(), SQL Server uses RAND() for a single value but NEWID() for per-row shuffling (because RAND() returns the same value for every row in a query), and Snowflake's RANDOM() returns a 64-bit integer. ORDER BY RANDOM() must sort the entire table, which is slow on millions of rows; databases offer TABLESAMPLE or SAMPLE clauses for large tables. Results are not repeatable unless you set a seed (for example setseed() in PostgreSQL).

Why we use it

Analysts often need a fair, unbiased subset: a sample for manual QA, random A/B test groups, or a quick look at data without reading everything. RANDOM gives every row an equal chance.

When to use it

Use ORDER BY RANDOM() LIMIT n for small to medium tables and one-off samples. For big tables use TABLESAMPLE/SAMPLE. For repeatable splits (stable A/B groups), use MOD on a hash or id instead of RANDOM.

How it works

  1. Query reads all candidate rows
  2. Evaluate RANDOM() once per row as a sort key
  3. Sort rows by that random key
  4. Is LIMIT n present? Yes: Keep only the first n rows. No: Return all rows in shuffled order.
  5. Different sample on every run

Syntax

RANDOM()

Example

SELECT * FROM employees ORDER BY RANDOM() LIMIT 3;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic