---
title: Query data with SQL
description: Learn to query data in InfluxDB 3 Core using SQL.
url: https://docs.influxdata.com/influxdb3/core/query-data/sql/
estimated_tokens: 3871
product: InfluxDB 3 Core
version: core
---

# Query data with SQL

Learn to query data in InfluxDB 3 Core using SQL.

-   [Explore your schema with SQL](#explore-your-schema-with-sql)
-   [Perform a basic SQL query](#perform-a-basic-sql-query)
-   [Aggregate data with SQL](#aggregate-data-with-sql)
-   [Cast values to different types](#cast-values-to-different-types)
-   [Compare values in SQL queries](#compare-values-in-sql-queries)
-   [Fill gaps in data](#fill-gaps-in-data)
-   [Use parameterized queries with SQL](#use-parameterized-queries-with-sql)

### [Explore your schema with SQL](/influxdb3/core/query-data/sql/explore-schema/)

Use SQL to explore your data schema in your InfluxDB 3 Core database.

##### List tables

```sql
SHOW TABLES
```

##### List columns in a table

```sql
SHOW COLUMNS IN table
```

[Read more](/influxdb3/core/query-data/sql/explore-schema/)

### [Perform a basic SQL query](/influxdb3/core/query-data/sql/basic-query/)

A basic SQL query that queries data from InfluxDB 3 Core most commonly includes `SELECT`, `FROM`, and `WHERE` clauses.

```sql
SELECT temp, room FROM home WHERE time >= now() - INTERVAL '1 day'
```

[Read more](/influxdb3/core/query-data/sql/basic-query/)

### [Aggregate data with SQL](/influxdb3/core/query-data/sql/aggregate-select/)

Use aggregate and selector functions to perform aggregate operations on your time series data.

##### Aggregate fields by groups

```sql
SELECT
  mean(field1) AS mean,
  selector_first(field2)['value'] as first,
  tag1
FROM home
GROUP BY tag
```

##### Aggregate by time-based intervals

```sql
SELECT
  DATE_BIN(INTERVAL '1 hour', time, '2022-01-01T00:00:00Z'::TIMESTAMP) AS time,
  mean(field1),
  sum(field2),
  tag1
FROM home
GROUP BY 1, tag1
```

[Read more](/influxdb3/core/query-data/sql/aggregate-select/)

### [Cast values to different types](/influxdb3/core/query-data/sql/cast-types/)

Use the `CAST` function or double-colon `::` casting shorthand syntax to cast a value to a specific type.

```sql
-- CAST clause
SELECT CAST(1234.5 AS BIGINT)

-- Double-colon casting shorthand
SELECT 1234.5::BIGINT
```

[Read more](/influxdb3/core/query-data/sql/cast-types/)

### [Compare values in SQL queries](/influxdb3/core/query-data/sql/compare-values/)

Use SQL window functions to compare values across different rows in your time series data. Learn how to calculate differences, percentage changes, and compare values at specific time intervals.

##### Calculate difference from previous value

```sql
SELECT
  time,
  room,
  temp,
  temp - LAG(temp) OVER (
    PARTITION BY room
    ORDER BY time
  ) AS temp_change
FROM home
ORDER BY room, time
```

[Read more](/influxdb3/core/query-data/sql/compare-values/)

### [Fill gaps in data](/influxdb3/core/query-data/sql/fill-gaps/)

Use [`date_bin_gapfill`](/influxdb3/core/reference/sql/functions/time-and-date/#date_bin_gapfill) with [`interpolate`](/influxdb3/core/reference/sql/functions/misc/#interpolate) or [`locf`](/influxdb3/core/reference/sql/functions/misc/#locf) to fill gaps of time where no data is returned.

```sql
SELECT
  date_bin_gapfill(INTERVAL '30 minutes', time) as time,
  room,
  interpolate(avg(temp))
FROM home
WHERE
    time >= '2022-01-01T08:00:00Z'
    AND time <= '2022-01-01T10:00:00Z'
GROUP BY 1, room
```

[Read more](/influxdb3/core/query-data/sql/fill-gaps/)

### [Use parameterized queries with SQL](/influxdb3/core/query-data/sql/parameterized-queries/)

Use parameterized queries to prevent injection attacks and make queries more reusable.

```sql
SELECT * FROM home
WHERE time >= $min_time
  AND temp >= $min_temp
  AND room = $room
```

[Read more](/influxdb3/core/query-data/sql/parameterized-queries/)

[query](/influxdb3/core/tags/query/) [sql](/influxdb3/core/tags/sql/)
