SQL SERVER in randomly sort query results

Source: Internet
Author: User
Tags sql query rand sort
Server | sorted | Random Translation: SQL SERVER in randomly sort query results


Randomly Sorting Query Results
I randomly sort query results



Q. How can I randomly sort query results?
Q: How can I randomly sort query results?

A. to randomly order rows, or to return X number of randomly chosen rows, you can use the RAND function inside the SELECT statement. but the RAND function is resolved only once for the entire query, so every row will get same value. you can use an order by clause to sort the rows by the result from the NEWID function, as the following code shows:

A: The results records randomly sort, or random returns X records, you can SELECT statement used in the RAND function. But the RAND function in the query only once, so every row will get the same value. Can pass in order by clause NEWID function to the results of the sort method to achieve, code is as follows:

SELECT *
FROM Northwind.. Orders
Order by newid ()

Select top 10 *
FROM Northwind.. Orders
Order by newid ()


Author: Water


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.