RAND()() — SQL Technical Interview Questions & Answers
RAND()()
RAND()()
- RAND() is a MySQL math function: random 0–1. ORDER BY RAND() LIMIT 1 picks a random row (slow on huge tables). Use it in SELECT, e.g. SELECT RAND(marks) FROM students;
- These functions do not change stored marks unless you UPDATE. NULL in, NULL out for most of them.
- ORDER BY RAND() LIMIT 1 is the interview random-row trick. On a large table it is expensive — say that.
- In a viva, one numeric example beats listing twenty functions.
- On the example next to this theory: RAND()(): create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
Technical Interview Tip: Explain RAND with one number. random 0–1. ORDER BY RAND() LIMIT 1 picks a random row (slow on huge tables).
Code Examples for RAND()()
RAND()() — sample query
-- RAND()()
CREATE TABLE demo (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL,
qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
(1, 'alpha', 2),
(2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;RAND()(): create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
How to Explain RAND()() to an Interviewer
What is RAND?
random 0–1. ORDER BY RAND() LIMIT 1 picks a random row (slow on huge tables) Used in SELECT