# Querying Survey fields and responses

**URL:** <https://meta.discourse.org/t/querying-survey-fields-and-responses/308112>\
**Category:** Data & reporting\
**Tags:** sql-query, surveys\
**Created:** [May 15, 2024, 6:28pm UTC](https://meta.discourse.org/t/querying-survey-fields-and-responses/308112 "2024-05-15T18:28:43Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![isaac](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/isaac/32/235758_2.png) [@isaac](https://meta.discourse.org/u/isaac)\
**Post date:** [May 15, 2024, 6:42pm UTC](https://meta.discourse.org/t/querying-survey-fields-and-responses/308112/2 "2024-05-15T18:42:25Z")

</div>

The value for

> [@jordan-violet](#):
>
> radio buttons, checkboxes, and dropdowns

are stored in a separate table.

This should get you were you need to be 🙂

```sql
-- [params]
-- null int :user_id
-- string :name

SELECT s.name, s.post_id, sf.question, COALESCE(sr.value, sfo.html) AS value, sr.user_id, sr.created_at as responded_at
FROM surveys s
JOIN survey_fields sf ON sf.survey_id = s.id
JOIN survey_responses sr ON sr.survey_field_id = sf.id
LEFT JOIN survey_field_options sfo ON sfo.id = sr.survey_field_option_id
WHERE s.name = :name
AND (:user_id IS NULL OR sr.user_id = :user_id)
ORDER BY sr.user_id

```

I have added a per user query option (when left empty it queries all user per survey), but feel free to drop

> AND (:user\_id IS NULL OR sr.user\_id = :user\_id)

if you are not interested in that.

---

_[View the full topic](https://meta.discourse.org/t/querying-survey-fields-and-responses/308112)._
