18 August 2023
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:
Let's dive in!
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.
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.
For example, query a Sales_By_Category
view:
SELECT * FROM Sales_By_Category
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) }}
TOP N
or FETCH FIRST
clausesWHERE
instead of HAVING
Proper queries ensure fast load times and responsiveness.
Once you've built your query, drag fields to the shelves like normal. Visualisations update as users change parameters or filters.
Querying databases directly gives you:
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.
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.
Yes, you can perform inner, left, right and full SQL joins on tables and database views in your queries.
Tableau supports many SQL aggregate, math, date, string and conditional functions. Common ones include SUM(), AVG(), COUNT(), DATEPART(), LEN(), CASE WHEN, etc.
Now get out there and start querying your data!
CONTACT
+44 (0)20 8446 7555
Copyright © 2024 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