# How can we link Grafana time filter with our SQL query?

**URL:** <https://community.grafana.com/t/how-can-we-link-grafana-time-filter-with-our-sql-query/137324>\
**Category:** Dashboards\
**Created:** [November 29, 2024, 10:43am UTC](https://community.grafana.com/t/how-can-we-link-grafana-time-filter-with-our-sql-query/137324 "2024-11-29T10:43:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohanagrawal](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mohanagrawal](https://community.grafana.com/u/mohanagrawal)\
**Post date:** [November 29, 2024, 10:43am UTC](https://community.grafana.com/t/how-can-we-link-grafana-time-filter-with-our-sql-query/137324/1 "2024-11-29T10:43:11Z")

</div>

We have created a dashboard using SQL queries in Grafana. We want to link Grafana in-built time filter with our dashboard so that we can filter data as required. Kindly guide how can we map it? Thank you.

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/0/5/05e1392c059dfc93ea88fb7da22797bbea883c72.png)

---

<div class="post-metadata">

**Author:** ![sowdenraymond](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/sowdenraymond/32/38541_2.png) [@sowdenraymond](https://community.grafana.com/u/sowdenraymond)\
**Post date:** [November 29, 2024, 11:10am UTC](https://community.grafana.com/t/how-can-we-link-grafana-time-filter-with-our-sql-query/137324/2 "2024-11-29T11:10:07Z")

</div>

> **[Add variables | Grafana documentation](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables)**
>
> Learn about the types of variables you can add to dashboards and how

---

<div class="post-metadata">

**Author:** ![mohanagrawal](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mohanagrawal](https://community.grafana.com/u/mohanagrawal)\
**Post date:** [November 29, 2024, 11:53am UTC](https://community.grafana.com/t/how-can-we-link-grafana-time-filter-with-our-sql-query/137324/3 "2024-11-29T11:53:10Z")

</div>

Thanks. Can you also guide how to create global variable for this? And how to map it with our dashboard/Query?

---

<div class="post-metadata">

**Author:** ![sowdenraymond](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/sowdenraymond/32/38541_2.png) [@sowdenraymond](https://community.grafana.com/u/sowdenraymond)\
**Post date:** [November 29, 2024, 12:12pm UTC](https://community.grafana.com/t/how-can-we-link-grafana-time-filter-with-our-sql-query/137324/4 "2024-11-29T12:12:07Z")

</div>

they are built in, and you can call them from your query

---

<div class="post-metadata">

**Author:** ![grant2](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/grant2/32/42281_2.png) [@grant2](https://community.grafana.com/u/grant2)\
**Post date:** [December 1, 2024, 1:55pm UTC](https://community.grafana.com/t/how-can-we-link-grafana-time-filter-with-our-sql-query/137324/5 "2024-12-01T13:55:49Z")

</div>

For PostgreSQL, the syntax to include template variables looks like this:  
`SELECT * FROM "ElectricPowerOperations" WHERE "type" IN ('Demand') AND "region" IN (${region}) AND $__timeRange(time)`

More examples [here](https://play.grafana.org/d/f9a5b2d1-0227-4341-93fd-113fedba6481/217d6661-10d9-5c57-9e4c-31da5a341b15?orgId=1&from=now-15d&to=now&timezone=browser&var-region=$ __all&var-fueltype=$__ all).
