Flipside Docs
  • Welcome to Flipside!
    • Welcome to Flipside
    • Growth Services
    • Data
      • Choose your Flipside plan
        • Free
        • Builder
        • Pro
          • Get Started in Snowflake
          • Incremental Table Pattern
          • Copy Data from Snowflake to AWS
          • Copy Data from Snowflake to GCP
          • Copy Data from Snowflake to Azure
        • Snowflake Data Shares
          • Mounting a Snowflake Data Share
    • Earn
    • Community
  • Data
    • Flipside Data
      • Table Docs by Chain
      • Data Modeling Approach
      • Labels
        • Centralized Exchange Label Type
        • Decentralized Exchange Label Type
        • Operator Label Type
        • Chain Admin Label Type
        • Decentralized Finance Label Type
        • NonFungible Tokens Label Type
        • Games Label Type
        • Bridge Label Type
        • Decentralized Applications Label Type
        • Token Label Type
        • Flotsam Label Type
      • Tags
      • Table Freshness Targets
      • Contribute to Our Data
        • Contract Decoding & ABIs
        • How to Add Your Own Tags
        • Community Curation
          • Getting Started
            • Contribution Workflow
          • Model Standards
            • dbt Tips
          • PR Checklist
    • Data Products
      • Data Studio (SQL Analysts)
        • Get Started
          • Write/fork your first query
        • Flipside AI
        • Studio in Depth
          • Query Editor
            • Query Referencing
            • Pro Tips for Querying
            • Keyboard Shortcuts
            • Hex-to-Integer Function
          • Create a Chart
            • Advanced Visualization
          • Build a Dashboard
            • Markdown Reference
          • Team Accounts
        • Tutorials
          • How to Analyze Web3 Data
          • Video Tutorials
            • Overview of Schemas & Tables
            • Ethereum Tutorials
              • Getting Started with Decoded Ethereum Events
              • Getting Started with Ethereum Balances
              • Block Level and Daily Balances
              • Finding Centralized Exchange Flows
            • Solana Tutorials
              • Solana Schema & Tables: Video Walkthrough
              • Solana Specialty Tables: Video Walkthrough
              • Exploring Transactions in solana.events
            • THORChain Tutorials
              • THORChain Schema & Tables
              • Calculating IL for THORChain
          • More Tools & Resources
        • Get Inspired
      • LiveQuery
        • Get Started
        • LiveQuery in Depth
          • Functions
            • 🤝Partner Functions
            • 🧙‍♂️EVM Functions
              • 💡Seaport: Real-time Orders
              • 💡WETH Pool Balances
              • 💡General EVM Node Queries
            • 🤖API Functions
              • 💡Query TheGraph
              • 💡Query Defi Llama
            • ⚙️Utility Functions
              • 💡Hex Converters
              • 💡JSON RPC Request Builder
              • 💡EVM Logs Decoder
              • 💡Keccak256 Encoder
          • QuickNode Setup Guide
        • Secrets Manager
        • Get Inspired
      • Snowflake
      • API/SDK (Developers)
        • Get Started - Your first API call in < 2 min
        • SDK in Depth
          • JSON RPC methods
          • Run A Query
          • Query Results
          • Pagination
          • Sorting
          • Filtering
          • Query Seconds
          • Caching (maxAgeMinutes)
          • Rate Limits
          • Errors
          • More Examples
          • Archive
            • [ARCHIVED] JS/TS SDK
            • ShroomDK Migration Guide
            • [LEGACY] R
        • Tutorials
          • Have Questions?
        • Get Inspired
    • Insights and Tools
  • Earn
    • Flipside Earn
    • Onchain Rewards
      • Quests
    • Analyst Rewards
      • Top Dashboards
        • Top Dashboard Guidelines
        • Skill Tree
      • Ambassador Program
      • Direct to Analyst Commissions
    • Wallet Management
    • Tax Center
  • Support
    • General Support
    • Open a Ticket
      • That email doesn't look quite right
    • 🌟Product Special Releases
      • 2025
        • 2025-02-06 | EVM Blockchain Standardization
          • Table Change Overview
      • 2024
        • 2024-06-13 | Solana native and wrapped addresses
        • 2024-05-01 | Improvements to Pricing Data
          • Actions for Data Studio Users
          • Actions for API Users
          • Action for Data Share Users
    • 🗒️Release Notes
      • 2025
        • 2025-05-01 | Release Notes
        • 2025-04-17 | Release Notes
        • 2025-04-03 | Release Notes
        • 2025-03-20 | Release Notes
        • 2025-03-06 | Release Notes
        • 2025-02-20 | Release Notes
        • 2025-02-06 | Release Notes
        • 2025-01-23 | Release Notes
      • 2024
        • 2024-12-24 | Release Notes
        • 2024-12-12 | Release Notes
        • 2024-11-27 | Release Notes
        • 2024-11-14 | Release Notes
        • 2024-10-31 | Release Notes
        • 2024-10-03 | Release Notes
        • 2024-10-17 | Release Notes
        • 2024-09-19 | Release Notes
        • 2024-09-05 | Release Notes
        • 2024-08-22 | Release Notes
        • 2024-08-08 | Release Notes
        • 2024-07-25 | Release Notes
        • 2024-07-11 | Release Notes
        • 2024-06-27 | Release Notes
        • 2024-06-14 | Release Notes
        • 2024-05-30 | Release Notes
        • 2024-05-16 | Release Notes
        • 2024-05-02 | Release Notes
        • 2024-04-18 | Release Notes
        • 2024-04-05 | Release Notes
        • 2024-03-18 | Release Notes
        • 2024-03-05 | Release Notes
        • 2024-02-20 | Release Notes
        • 2024-02-05 | Release Notes
    • 📊Studio Change log
  • Flipside Community
    • Get Started
    • Ambassador Program
    • Join Our Discord
    • Join Our Guild
Powered by GitBook
On this page
  • Overview
  • How to reference a query?
  • Leveraging Query Referencing for Performance Optimization
  • How to decide which query to reference?
  • Troubleshooting Errors
  • Limitations

Was this helpful?

  1. Data
  2. Data Products
  3. Data Studio (SQL Analysts)
  4. Studio in Depth
  5. Query Editor

Query Referencing

Easily reference results from exiting queries!

PreviousQuery EditorNextPro Tips for Querying

Last updated 4 months ago

Was this helpful?

Query Referencing allows you to streamline your analysis workflow by reusing the results of previously executed queries as data sources for new queries. This feature enhances query organization, promotes reusability of complex result sets, and significantly boosts query performance.

Overview

This section will cover:


How to reference a query?

  1. Identify the Query:

    1. Navigate to the desired query and click "Copy Reference" button to obtain the query ID.

    2. To reference your own query, find the query you want to reference under "My Work". Click the 3-dot menu, and select "Add to Query".

  2. Reference the Query:

    • In your new query, use the following syntax to reference the query:

      SELECT * 
      FROM $query('query_id')
  3. Join with other tables:

    • You can join the referenced query with other tables in your dataset:

      SELECT * 
      FROM bitcoin.core.fact_transactions t
      JOIN $query('query_id') AS my_query 
        ON my_query.blockheight = t.blockheight

Leveraging Query Referencing for Performance Optimization

A significant advantages of Query Referencing is its ability to dramatically improve the performance of parameterized queries.

  1. Pre-calculate the 100,000 most relevant addresses:

    select 
        from_address,
        count(1) as n_tx   -- can safely count(1) b/c this table is 1 row per tx_hash
    from ethereum.core.fact_transactions 
    where block_timestamp >= '2024-01-01'
    group by from_address
    having n_tx < 20001 -- assume > 20k is bots 
    order by n_tx desc 
    limit 100000;
  2. Reference the pre-calculated result:

    select * from 
    $query('0a0d54ca-f8de-43fe-8e5f-c270272982f9') -- query id taken from studio URL
    where from_address = '{{wallet_address}}';

By pre-calculating the relevant addresses and referencing the result, you can significantly improve query performance, especially when dealing with large datasets and frequent parameter changes.

This approach requires the query to be re-executed for each user, leading to potential performance bottlenecks.

select 
from_address, 
count(tx_hash) as n_tx 
from base.core.fact_transactions
where block_timestamp >= '2024-01-01'
and from_address = '{{wallet_address}}'

How to decide which query to reference?

  1. Do Your Own Research (DYOR): Always verify the logic and accuracy of the referenced query.

Since all public queries can be referenced, make sure you trace query dependencies! Hover over any $query('query_id') and click on the query title in the popover to view all information about the underlying query.

  1. Consider Query Refresh Rates: Ensure that the referenced query is refreshed at an appropriate interval to provide up-to-date results.

  • To ensure up-to-date results, set a "Refresh Rate" on your base query

  • Any new query referencing the results will automatically use the refreshed data

  • Refreshes follow standard query refresh capabilities

  1. Understand Cache Expiration: Be aware of the cache duration and expiration rules for referenced queries.

  • Results are cached and available for 24 hours by default

  • Each reference within the 24-hour window extends availability by another 24 hours

  • Maximum cache duration is 31 days


Troubleshooting Errors

Error: Result for query {ID} has expired.

This error occurs when the results of a referenced query has expired.

How to resolve:

  1. If you own the referenced query:

    1. Set a refresh rate of at least once every 24 hours on the referenced query. This ensures that its results are consistently available to access.

  2. If you do NOT own the referenced query:

    1. Hover over the query ID in your SQL to see the details of the referenced query, click on the query title to open it.

    2. Fork the referenced query to create a copy under your ownership and set a refresh rate of at least once every 24 hours.

    3. Update the query ID in your current query with the ID of the forked query to ensure uninterrupted access to the results.


Limitations

Base queries that utilize parameters will always execute with their default values when referenced. Currently, there’s no mechanism to dynamically pass parameters into the $query() function. It is best to avoid using parameters in your base queries.

How to reference a query?
How to leverage query referencing to speed up parameter runs?
How to decide which query to reference?
How to troubleshoot errors?
Limitations