CUSTOMISED
Expert-led training for your team
Dismiss
How to Query in Tableau

18 August 2023

How to Query in Tableau

Tableau offers powerful options for querying your data beyond just dragging fields to shelves. Learning how to write custom SQL queries gives you greater control and flexibility. JBI Training offers a number of options for you and your team to train in Tableau. Our blog material and guides are just the start of your Tableau training adventure. If you are considering training your team, get in contact to place them on our Tableau Course

In this article, you'll learn:

  • Basic SQL query syntax
  • Building queries on database views
  • Using parameters in queries
  • Query performance tips

Let's dive in!

SQL Query Basics

The underlying data for a Tableau workbook can come from an existing database or data extract. To query the data, you can write SQL SELECT statements.

Basic syntax:


SELECT columns
FROM tables 
WHERE conditions
ORDER BY columns

For example, to get product sales by category:


SELECT Category, SUM(Sales) as TotalSales
FROM ProductTable 
GROUP BY Category
ORDER BY TotalSales DESC

SQL knowledge helps construct custom queries to suit your analysis needs.

Querying Database Views

If your data is in a database, you can query pre-defined views rather than entire tables. This focuses the data and improves performance.

In the connection dialog, choose "Table" rather than "View" to see available views.

Data Source View

For example, query a Sales_By_Category view:


SELECT * FROM Sales_By_Category

Using Parameters in Queries

Parameters add flexibility to your queries, allowing users to filter or configure the data.

You can reference parameters in the WHERE clause:


SELECT *
FROM CustomerTable 
WHERE Country = @CountryParam

Or construct the query dynamically:


SELECT *
FROM {{ PARAM(table) }} 

Optimising Query Performance

  • Limit records with TOP N or FETCH FIRST clauses
  • Filter early with WHERE instead of HAVING
  • Aggregate detail data for overview dashboards

Proper queries ensure fast load times and responsiveness.

Visualising the Query

Once you've built your query, drag fields to the shelves like normal. Visualisations update as users change parameters or filters.

Filter Data from Your Views - Tableau

Conclusion

Querying databases directly gives you:

  • Flexibility to shape your data
  • Precision through exact filters and values
  • Fast load times by limiting data

SQL skills help you move beyond pre-defined data sources in Tableau.

With the ability to construct custom queries using parameters and database views, you can build robust data models for in-depth analysis.

Frequently Asked Questions

How do I run a query in Tableau?

In the data connection dialog, choose "Run Query" and enter your SQL code. Make sure to include the database or extract name with dot notation like DATABASE.TABLENAME.

Can I join tables in a query?

Yes, you can perform inner, left, right and full SQL joins on tables and database views in your queries.

What functions can I use?

Tableau supports many SQL aggregate, math, date, string and conditional functions. Common ones include SUM(), AVG(), COUNT(), DATEPART(), LEN(), CASE WHEN, etc.

Next Steps

  • Learn more Tableau SQL functions
  • Optimize slow-running queries
  • Create stored procedures as data sources
  • Read our article on how to create top n parameter in tableau
  • Or our how to guide on how to get z-score in tableau

Now get out there and start querying your data!

About the author: Daniel West
Tech Blogger & Researcher for JBI Training

CONTACT
+44 (0)20 8446 7555

[email protected]

SHARE

 

Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us

POPULAR

Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course