# Query random item(s) from database (Supabase)

**URL:** https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159
**Category:** Ask us anything
**Created:** [September 21, 2022, 11:43pm UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159 "2022-09-21T23:43:22Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![khairul](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@khairul](https://community.weweb.io/u/khairul)
#### Post date: [September 21, 2022, 11:43pm UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/1 "2022-09-21T23:43:22Z")

</div>

Hi I am building a quiz app, I wonder how to query random single question from my questions table in supabase. For example I want to query a question 1 by 1 each page.

---

<div class="post-metadata">

### Author: ![khairul](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@khairul](https://community.weweb.io/u/khairul)
#### Post date: [September 22, 2022, 2:38am UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/2 "2022-09-22T02:38:00Z")

</div>

I figured I need to make another query to find the length first, which returns all the id, get the length of the collections, and used it for the other query. I guess this will cause some leakage of data. If you found better solution please help

---

<div class="post-metadata">

### Author: ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)
#### Post date: [September 22, 2022, 1:28pm UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/3 "2022-09-22T13:28:41Z")

</div>

Not only a leakage of data but also a waterfall of requests that will slow down the load of your quiz (wait for first query and then wait for the second query with the data you need).

With supabase you can create [database functions](https://supabase.com/docs/guides/database/functions) that allow you to do get a random row with only one request.  
Have a look at the documentation.

In short:

- create and run this SQL snippet:

```auto
create or replace function get_random_quiz()
returns setof quiz
language sql
as $$
   select * from quiz 
   order by random()
   limit 1;
$$;

```

- call the supabase REST API in your workflow with a GET request to the url `https://<project_ref>.supabase.co/rest/v1/rpc/get_random_quiz`. Be sure to switch on the option to make the request with the server and to add two header to the request:
  - apikey: \<SUPABASE\_ANON\_KEY\>
  - Authorization: Bearer \<SUPABASE\_ANON\_KEY\>

This specific way of getting a random row (`order by random()`) is good only for small tables. If you are going to work with big tables you can modify the query to randomly select in a more performant way.

---

<div class="post-metadata">

### Author: ![khairul](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@khairul](https://community.weweb.io/u/khairul)
#### Post date: [September 22, 2022, 1:36pm UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/4 "2022-09-22T13:36:28Z")

</div>

Thank you so much @dorilama 🙏 🙏 🙏, I will try it and update the outcome

---

<div class="post-metadata">

### Author: ![khairul](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@khairul](https://community.weweb.io/u/khairul)
#### Post date: [September 23, 2022, 7:32am UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/5 "2022-09-23T07:32:20Z")

</div>

@dorilama I came across this video after watching @Quentin live on supabase views. So basically I generated a new view that contains random order of my quiz. Really quick and easy solution. Thank you!

[![](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/7/74750bbaac94f3ea738ec7d586b28065ed40489c.jpeg "⚡️4mins Supabase: Get a Random Row out of Supabase with Table Views") ](https://www.youtube.com/watch?v=ez8dfCYQGvE)

Also @Quentin the video was very helpful. I am just getting the hang of backend, I think we need more on those since us no-coders never touched on API before, so we don’t know what’s the best practices for databases and backend.

---

<div class="post-metadata">

### Author: ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)
#### Post date: [September 23, 2022, 8:58am UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/6 "2022-09-23T08:58:25Z")

</div>

This is also another good way to get the same result.  
Now you can choose between two solutions 🙂

---

<div class="post-metadata">

### Author: ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)
#### Post date: [September 23, 2022, 10:51am UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/7 "2022-09-23T10:51:15Z")

</div>

keep in mind that with the solution in the video you is using `order by random()` as well, so you will hit the same performance problem if your table is big.

---

<div class="post-metadata">

### Author: ![Riisud](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@Riisud](https://community.weweb.io/u/Riisud)
#### Post date: [August 8, 2024, 6:38pm UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/8 "2024-08-08T18:38:08Z")

</div>

Hey dorilama when is a table considered to big for this method of getting a random record? And what would be a better way when you have big table? Thanks in advance.

---

<div class="post-metadata">

### Author: ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)
#### Post date: [August 8, 2024, 7:04pm UTC](https://community.weweb.io/t/query-random-item-s-from-database-supabase/1159/9 "2024-08-08T19:04:33Z")

</div>

there isn’t a single answer for this. as you can read with a google search the problem depends on the specific details of your case (table schema, indexes etc)

what’s the size of your table? you can test multiple methods and compare performances with your data.
