How to Run a Query in Tableau
Building a visualization in Tableau feels intuitive - you drag fields onto a canvas, and a chart magically appears. However, behind that simple action, Tableau is acting as a sophisticated translator, converting your clicks and drags into precise database queries. Understanding this process, and knowing how to view or even write your own queries, is key to unlocking Tableau's full potential.
This tutorial will show you exactly how Tableau runs queries for you automatically. We’ll also cover how to look under the hood to see the exact code being generated and how you can take control by writing custom SQL queries when you need more flexibility.
How Tableau Automatically Runs Queries for You
The secret ingredient in Tableau’s recipe is a patented technology called VizQL (Visual Query Language). Think of VizQL as a universal translator that stands between you and your database. When you interact with the Tableau interface, you're not just moving pixels around, you're visually composing a question about your data. VizQL’s job is to take that visual question and translate it into a query that your connected data source can understand, like SQL, MDX, or other database-specific languages.
A Simple Example in Action
Imagine you're connected to a sales data source. You want to see total sales for each product category.
- You drag the Category dimension to the Columns shelf.
- You drag the Sales measure to the Rows shelf.
Instantly, a bar chart appears. But here’s what really happened:
- Your first action told VizQL you wanted to group your analysis by Category.
- Your second action told VizQL you wanted to aggregate the Sales data. Because it's a measure, Tableau automatically defaults to a SUM() aggregation.
VizQL then translated these intentions into an optimized SQL query that looks something like this:
SELECT
"Superstore"."Category",
SUM("Superstore"."Sales")
FROM "Superstore"
GROUP BY "Superstore"."Category"Tableau sent this query to your database, received the result (a table with two columns: Category and Sum of Sales), and then rendered that result as the bar chart you see. Every single action - adding a filter, changing a chart type, or drilling down - triggers this process of generating and running a new query.
How to See the Queries Tableau is Running
So, you know a query is happening in the background, but what if you want to see the exact SQL code? This is incredibly useful for troubleshooting a slow visualization, learning SQL by seeing examples, or validating that your chart is truly answering the question you intended. The best way to do this is with Tableau's built-in Performance Recorder.
Using the Performance Recorder
The Performance Recorder captures information about the events and processes involved in creating or interacting with a workbook, including the exact queries being sent to your database.
Here’s how to use it:
- Navigate to the top menu and select Help > Settings and Performance > Start Performance Recording.
- Now, perform the action you want to analyze. This could be loading a dashboard, building a new worksheet from scratch, or changing a filter that’s taking a long time to load.
- Once the action is complete, go back to Help > Settings and Performance > Stop Performance Recording.
- A new workbook will open automatically, displaying a dashboard with the recorded performance data. This dashboard is a visualization of the processes Tableau just completed.
Finding the Query
On the "Performance Summary" dashboard, you’ll see several panes. Look for the "Timeline" view. In the "Events" column, there will be an event called "Executing Query."
- Click on the green or blue bar for the "Executing Query" event in the timeline.
- In the pane below the timeline, you will see the exact query that was sent to your data source.
This allows you to copy the query and run it directly against your database for performance tuning or simply to understand the logic Tableau has generated on your behalf.
When Drag-and-Drop Isn't Enough: Using Custom SQL
While Tableau's automatic query generation is powerful, there are times when you need more direct control. You might need to restructure your data, perform a complex join, or use a specific database function that isn't available in Tableau's interface. This is when you can use Custom SQL to write your own query.
You might use Custom SQL to:
- Shape Your Data: Union tables together or unpivot data sets before they are brought into Tableau.
- Pre-Aggregate Data: If you're working with a massive dataset, you can write a query that sums or averages data to reduce the number of rows Tableau has to process, leading to better performance.
- Complex Joins: Perform joins on multiple criteria or use non-equi joins (e.g., joining on a date range) that aren't possible with the standard visual join canvas.
- Leverage Database Functions: Use powerful, database-specific functions for tasks like geospatial calculations or complex string parsing.
Step-by-Step: Adding a Custom SQL Query
You can add a custom SQL query directly in the data source connection window.
- In your Tableau workbook, click the Data Source tab at the bottom left.
- If you’re connecting to a new source, choose your database connector (e.g., Microsoft SQL Server, PostgreSQL, etc.). If you’re already connected, you’ll see your tables list on the left.
- Find the option labeled "New Custom SQL" in the left-hand pane and drag it onto the canvas.
- A dialogue box will appear, ready for you to write or paste your SQL code.
- As you type, you can use the "Update Automatically" button to preview the results and ensure your query is written correctly.
- Once your query is working, click OK. Tableau will now treat the results of your query as if they were a single, standard data table.
Best Practices for Custom SQL
While powerful, Custom SQL bypasses some of Tableau's built-in optimizations, so it’s important to be careful.
- Performance Comes First: A badly written Custom SQL query can make your entire workbook slow. Always test and tune your query directly in your database before bringing it into Tableau. Check its execution plan to ensure it's efficient.
- Comment Your Code: If your query is complex, add comments (-- This section joins product data) so your future self or colleagues can understand what you did.
- Use Parameters: You can make your Custom SQL dynamic by inserting Tableau Parameters. For example, instead of a static year filter
WHERE YEAR(order_date) = 2023, you can useWHERE YEAR(order_date) = <Parameters.Year Parameter>. This allows users to change the parameter from a filter in the dashboard, giving them control without an overly complex query.
Live vs. Extract: How It Affects Your Queries
Finally, your choice of data connection type - Live or Extract - has a profound impact on how and where queries are run.
- A Live connection means that every interaction in Tableau (like filtering) sends a new query directly to your database. This is great for real-time data but means dashboard performance is completely dependent on how fast your database can respond.
- An Extract connection takes a snapshot of your data and pulls it into Tableau's high-performance data engine, Hyper. All your queries are then run against this super-fast locally- or server-hosted file. This results in much faster dashboard performance, but your data is only as fresh as your last extract refresh.
Understanding this distinction is crucial. If a "query is running slow," the first step is to identify whether it’s a live query to a slow database or a query running against a Tableau extract.
Final Thoughts
Tableau’s ability to run queries automatically is what makes it such a powerful tool for visual data exploration. By understanding how VizQL translates your actions into code, using the Performance Recorder to debug, and leveraging Custom SQL for complex situations, you can move from a casual user to a true Tableau power user.
We believe that diving this deep into SQL and performance tuning shouldn't be a prerequisite for getting answers from your data. While powerful, mastering tools like Tableau involves a significant learning curve. At Graphed, we use AI to handle all the query-writing for you. Instead of dragging and dropping, you simply ask in plain English, "Show me last quarter's sales trends by product category" and instantly get a real-time visualization. We connect directly to your data sources, generate the complex queries on your behalf, and deliver the insights in seconds, all without needing to master a new tool.
Related Articles
How to Connect Facebook to Google Data Studio: The Complete Guide for 2026
Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.
Appsflyer vs Mixpanel: Complete 2026 Comparison Guide
The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.
DashThis vs AgencyAnalytics: The Ultimate Comparison Guide for Marketing Agencies
When it comes to choosing the right marketing reporting platform, agencies often find themselves torn between two industry leaders: DashThis and AgencyAnalytics. Both platforms promise to streamline reporting, save time, and impress clients with stunning visualizations. But which one truly delivers on these promises?