Q: SQL order by rand() with LIMIT I try to use ORDER BY RAND() to get random data from my database. I put the limit for the database as code below, but it still gives all the data, the LIMIT did not work. Can I know why? I hope someone can help me because I'm not good with SQL statements. The last question, if we use ORDER BY RAND() we may get repeated data, how to make the data not repeated? Thank you in advance. I use php code.
<?php

/*
*   Get total questions
*/
$query = "SELECT * FROM `questengine` ";
//Get result
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;
    
/*
*   Get Question
*/
$query = "SELECT * FROM `questengine`ORDER BY RAND() LIMIT 5";

//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$question = $result->fetch_assoc();

/*
*   Get Choices
*/
$query = "SELECT * FROM `choicesengine`
            WHERE question_number = ".$question['id'];
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);

?>

A: Your first query result is never used so I have removed it.
Your third query seems to relate to your second so I merged them into a single query.
Please post data table example if the below is incorrect.
<?php
// Get Question and Answer
$query = "SELECT * FROM `questengine` qe JOIN `choicesengine` ce ON ce.question_number = qe.id ORDER BY RAND() LIMIT 5";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$question = $result->fetch_assoc();

