SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
SQL ORDER BY RANDOM
If you want the resulting record to be ordered randomly, you should use the following codes according to several databases.
Here is a question: what is the need to fetch a random record or a row from a database?
Sometimes you may want to display random information like articles, links, pages, etc., to your user.
If you want to fetch random rows from any of the databases, you have to use some altered queries according to the databases.
- Select a random row with MySQL:
If you want to return a random row with MY SQL, use the following syntax:
- Select a random row with Postgre SQL:
-
Select a random row with SQL Server:
-
SELECT TOP 1 column FROM table ORDER BY NEWID ();
- Select a random row with oracle:
-
SELECT column FROM (SELECT column FROM table ORDER BY dbms_random.value) WHERE rownu
-
Select a random row with IBM DB2:
-
SELECT column RAND () as IDX FROM table ORDER BY IDX FETCH FIRST 1 ROWS ONLY;
To understand this concept practically, let us see some examples using the MySQL database. Consider we have a table items created into the database with the following data:
Table: items
ID | Item_Name | Item_Quantity | Item_Price | Purchase_Date |
---|---|---|---|---|
1 | Soap | 5 | 200 2021-07-08 | |
2 | Toothpaste | 2 | 80 | 2021-07-10 |
3 | Pen | 10 | 50 | 2021-07-12 |
4 | Bottle | 1 | 250 | 2021-07-13 |
5 | Brush | 3 | 90 | 2021-07-15 |
Suppose we want to retrieve any random record from the items table.
We will write the query as follows:
We may get the following results:
ID | Item_Name | Item_Quantity | Item_Price | Purchase_Date |
---|---|---|---|---|
3 | Pen | 10 | 20 | 2021-07-12 |
Now let us try executing the same query one more time.
We may get the following results:
ID | Item_Name | Item_Quantity | Item_Price | Purchase_Date |
---|---|---|---|---|
5 | Brush | 3 | 90 | 2021-07-15 |
From the above results, we can conclude that we get different records as output both times even though we executed the same query twice. RAND () function has selected random records both times for the same query from a single table. Therefore, even we execute the same query again, we will get different output every time. There is a rare possibility of getting the same record consecutively using the RAND () function.
Now, suppose you want all the records of the table to be fetched randomly.
To do so, we need to execute the following query:
We may get the following results:
ID | Item_Name | Item_Quantity | Item_Price | Purchase_Date |
---|---|---|---|---|
4 | Bottle | 1 | 250 | 2021-07-13 |
5 | Brush | 3 | 90 | 2021-07-15 |
1 | Soap | 5 | 200 | 2021-07-08 |
2 | Toothpaste | 2 | 80 | 2021-07-10 |
3 | Pen | 10 | 50 | 2021-07-12 |
There is also a possibility of getting some different arrangements of records if we execute the RAND () function again on the employees table.