How to Return Random Rows Efficiently in SQL Server?
Last Updated :
25 Jan, 2022
In this article, we are going to learn an SQL Query to return random rows efficiently. To execute the query, we are going to first create a table and add data into it. We will then sort the data according to randomly created IDs(using the NEWID() method) and return the top rows after the sorting operations. We are printing data here so we would use the "SELECT command" in SQL.
NEWID():
NEWID( ) is a SQL function that is used to generate a random unique value of type unique identifier.
Step 1: New Database creation
To make a new database creation, the following query can be used:
Query:
CREATE DATABASE random_sql;

Step 2: Specifying the database in use
We need to specify in which database we are going to do operations. The query to use a Database is :
Query:
USE random_sql;

Step 3: New table creation
To create a new table we will use the following query:
Query:
CREATE TABLE random_table(
col1 INT,
col2 VARCHAR(100));

Step 4 : Data insertion
To insert data into the table, the following query will be used:
Query:
INSERT INTO random_table(col1,col2)
VALUES
(13,'John'),
(98,'Xin Fu'),
(87,'Utkarsh'),
(54,'Gordon'),
(1,'Graham');

Step 5: Selecting random data
In this step, we create final query by using the SELECT TOP clause. This clause is used to fetch limited number of rows from a database. The rows returned would be made random by an operation on the table. We assign a random ID to all rows and sort the rows according to the created ID, thus giving us a randomly sorted table to extract data. For this we use ORDER BY NEWID().
SELECT TOP 3 *
FROM random_table
ORDER BY NEWID();

Output:
Similar Reads
How to Join to First Row in SQL Server Joining the first row in SQL Server can be a common requirement in various scenarios, such as when we need to retrieve specific data associated with the first occurrence of a particular group or when you want to fetch additional details related to the first record in a result set. In this article, W
4 min read
How to Limit Rows in a SQL Server? To limit rows in SQL Server, use the TOP clause in the SELECT statement. Using the TOP clause in SQL Server, users can limit the number of rows in the results set. Here, we will understand how to limit rows in SQL Server with the help of different examples. Steps to Limit Rows in SQL ServerLet's che
3 min read
How to Request a Random Row in SQLite? SQLite is a database engine that provides a relational database management system and is a C language library with features like self-contained, serverless, and high reliability. SQLite is different from other traditional SQL database engines like MySQL, Oracle, PostgreSQL, etc. Traditional database
4 min read
How to Select Random Record From Table in PL/SQL? In Oracle PL/SQL, selecting random records from a table is a common yet essential operation, used for a variety of purposes like data sampling, random selection for testing, or picking winners in contests.In this article, we will explore different methods to select random records from a table in PL/
5 min read
How to Select Random Row in MySQL In database operations, selecting random rows from a table is a common requirement for various applications, such as gaming, content recommendation, and statistical sampling. In this article, we learn different methods for selecting random rows in MySQL. We'll understand various approaches, includin
6 min read