Example 1
Pick two random products
The random function produces a different sort key for each row. The limit then keeps two rows from that randomized order.
CREATE TABLE products (id INT, name VARCHAR(50));
INSERT INTO
products
VALUES
(1, 'Widget'),
(2, 'Gadget'),
(3, 'Doohickey'),
(4, 'Thing');SELECT
id,
name
FROM
products
ORDER BY
RAND ()
LIMIT
2;CREATE TABLE products (id INT, name VARCHAR(50));
INSERT INTO
products
VALUES
(1, 'Widget'),
(2, 'Gadget'),
(3, 'Doohickey'),
(4, 'Thing');SELECT
TOP 2 id,
name
FROM
products
ORDER BY
NEWID ();CREATE TABLE products (id INT, name VARCHAR(50));
INSERT INTO
products
VALUES
(1, 'Widget'),
(2, 'Gadget'),
(3, 'Doohickey'),
(4, 'Thing');SELECT
id,
name
FROM
products
ORDER BY
RANDOM ()
LIMIT
2;CREATE TABLE products (id INT, name TEXT);
INSERT INTO
products
VALUES
(1, 'Widget'),
(2, 'Gadget'),
(3, 'Doohickey'),
(4, 'Thing');SELECT
id,
name
FROM
products
ORDER BY
RANDOM ()
LIMIT
2;Result rows vary on each execution, so this command intentionally has no fixed expected rows.