# Data Explorer Tutorial - Part 1 - Writing Your First Query

**URL:** https://meta.discourse.org/t/data-explorer-tutorial-part-1-writing-your-first-query/277925
**Category:** Data & reporting
**Tags:** data-explorer, sql-tutorial
**Created:** [September 5, 2023, 10:03pm UTC](https://meta.discourse.org/t/data-explorer-tutorial-part-1-writing-your-first-query/277925 "2023-09-05T22:03:38Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![SaraDev](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saradev/32/335139_2.png) [@SaraDev](https://meta.discourse.org/u/SaraDev)
#### Post date: [September 5, 2023, 10:03pm UTC](https://meta.discourse.org/t/data-explorer-tutorial-part-1-writing-your-first-query/277925/1 "2023-09-05T22:03:38Z")

</div>

> :discourse: Want to extract some data from your Discourse site but not sure where to start? You’ve come to the right place! This is a #tutorial guide for beginners who are new to the [Data Explorer](https://meta.discourse.org/t/32566). If you’re already familiar with SQL and how to use the [Data Explorer](https://meta.discourse.org/t/32566), then you will most likely not need to read these topic, and may want to check out our #data-reporting::category category instead.

The goal of this guide is to help you get a basic grasp of how to use the [Data Explorer](https://meta.discourse.org/t/32566) to create reports on your your Discourse site, and is the first part of our [Data Explorer](https://meta.discourse.org/t/32566?silent=true) Tutorial series.

## What is the [Data Explorer](https://meta.discourse.org/t/32566)?

The [Data Explorer](https://meta.discourse.org/t/32566) is a Discourse plugin that allows you to run SQL queries against your database and export the results. It’s a powerful tool for extracting and analyzing data from your Discourse site.

Before you can use the [Data Explorer](https://meta.discourse.org/t/32566), you’ll need to install it on your site. See [Install plugins on a self-hosted site](https://meta.discourse.org/t/install-plugins-in-discourse/19157) if you need any assistance with this.

> :discourse: The [Data Explorer](https://meta.discourse.org/t/32566?silent=true) is pre-installed on Discourse’s [Business](https://www.discourse.org/pricing) and [Enterprise](https://www.discourse.org/enterprise) hosted plans.

Once you’ve installed the [Data Explorer](https://meta.discourse.org/t/32566), you can start using it to run SQL queries.

## Creating our first SQL Query

The best way to get familiar with the [Data Explorer](https://meta.discourse.org/t/32566?silent=true), is to get some hands on experience!

Let’s get started by creating our first query:

1. Head to the admin on your site and click on the `Plugins` tab.
2. Find the `Data Explorer` plugin and click on it.
3. Click on the `+` button to view a textbox and button for creating a new SQL query.
4. Type in the name of your query.
5. Click `Create New` to create the query.

 ![image](https://global.discourse-cdn.com/meta/original/4X/8/f/e/8fe52be1fc4b122339691f7039ab27146338ba33.png)

From here we can start building out our first query.

_The Default New Query Page:_

 ![image](https://global.discourse-cdn.com/meta/original/4X/b/7/8/b78e4aa440c319e0fdb212c0f2e9734473515f87.png)

## Hello World

For our first query let’s create a report that will show us: **The Most Viewed Topics on the Site** ✨

Thinking of how we’ll need to create this, let’s start by selecting the `title`, and `views` fields from the `topics` table:

```sql
SELECT
    title, views
FROM 
    topics

```

Which will show us a list of all topics on the site and the number of views they have. Click `Run` to see this in action:

 ![image](https://global.discourse-cdn.com/meta/original/4X/3/c/c/3cca6c6365421c6b7e52f23c2ec34ae60484e816.png)

## Sorting Results

Looking at these results, you’ll notice they aren’t ordered - this is because we need to explicitly use an `ORDER BY` statement to sort our results as desired. Let’s add this in now:

```sql
SELECT
    title, views
FROM 
    topics
ORDER BY views DESC

```

Let’s click `Run` again to see what changed:

 ![image](https://global.discourse-cdn.com/meta/original/4X/6/d/5/6d58e08b953bb62e95337c69a1fe390311ae0d40.png)

There we go - our results are now sorted in `descending` order by views!

## Formatting Results

With this query, wouldn’t it be nice if we could click directly on the name of the topic and be taken to the topic? Good thing we can do this with _Formatted Table Results_!

Let’s change the `title` section of our query to `id as topic_id` to see this in action:

```sql
SELECT
    id as topic_id,
    views
FROM 
    topics
ORDER BY views DESC

```

 ![image](https://global.discourse-cdn.com/meta/original/4X/c/e/5/ce501b93b9add9ba1a4db7ee85b7b32c34dee782.png)

Now the titles of the topics are displayed as hyperlinks!

> :discourse: The numbers you’re seeing next to the topic are the number of `replies` the topic has.

## Adding Parameters and `WHERE` statements

For our final addition to the query, let’s modify the query so that we only see topics with _more than X number of views_. To do this we’ll need to add a _parameter_, and a `WHERE` statement. For our parameter, we’ll use an `int` (integer), and the `WHERE` statement will filter the results so that only topics with more the minimum number of `views` are displayed.

```sql
-- [params]
-- int :min_views= 10

SELECT
    id as topic_id,
    views
FROM 
    topics
WHERE views > :min_views
ORDER BY views DESC

```

When running this query, you’ll now see that there’s a `min_views` parameter that you can use to change the minimum number of views topics are required to have to be displayed in the results.

 ![image](https://global.discourse-cdn.com/meta/original/4X/2/2/6/226fab49b57440a6727ce2908d0a76d028cb9f17.png)

## Wrapping Up

Hopefully you’re starting to get an idea of how [Data Explorer](https://meta.discourse.org/t/32566?silent=true) queries are built after going through these steps, however, we’ve barely scratched the surface in this topic!

The [Data Explorer](https://meta.discourse.org/t/32566), and more generally, SQL is a very, very broad subject. If you’re interested in learning more about the [Data Explorer](https://meta.discourse.org/t/32566) and SQL Queries, we highly recommend reading the other topics in this series.

### More Topics in this Series

- **This Topic**
- [Data Explorer Tutorial - Part 2 - Discourse SQL Basics](https://meta.discourse.org/t/data-explorer-tutorial-part-2-discourse-sql-basics/277927)
- [Utilizing Parameters in Data Explorer Queries](https://meta.discourse.org/t/utilizing-parameters-in-data-explorer-queries/277934)
- [Formatting Data Explorer Table Results](https://meta.discourse.org/t/formatting-data-explorer-table-results/277939)
