Sub topic 10
Working with Result Tables and Visualizations
This chapter covers what you can do with a result after a query finishes: inspect the result table, shape it, export it, and turn it into charts. It assumes you can already run a query in the SQL editor and write basic GROUP BY queries. Both are covered in earlier chapters.
The result set is the unit everything works on
A query produces a result set. The result table, every visualization, and every export operate on that result set and not on the underlying table. This has one practical consequence. If your query returns 1,000 rows, a chart built from it can only show those 1,000 rows, whatever the source table holds.
Reading and adjusting the result table
After a run, the Results tab shows a table with one row per record and the column types in the headers. Without changing the query, you can:
- Sort by clicking a column header.
- Filter rows in the table view.
- Hide, reorder, or resize columns.
- Search within the displayed rows.
These actions change only how you view the result. They do not re-run the query and are not guaranteed to be saved. If you need the order to persist, put it in the SQL with ORDER BY.
Row limits
The editor limits how many rows it fetches and shows. Two limits matter:
- The Limit setting next to the Run button. When it is on, the editor adds a row cap (1000 by default) to queries that have no
LIMITof their own. ALIMITyou write yourself takes precedence. - A ceiling on how many rows the interface will display or download for a single result. The exact numbers change between releases, so check the current Databricks documentation before depending on them.
Exporting results
The Results toolbar lets you copy the table to the clipboard or download it, typically as CSV, TSV, or Excel. Downloads contain the result set as displayed to you, so they carry the same row cap. For large exports, write the data to a table or a cloud storage location with SQL and read it from there. The CREATE TABLE AS SELECT statement does this.
Creating a visualization
Click + next to the Results tab and choose Visualization. The editor opens a configuration panel with a live preview. A visualization belongs to the query. It is saved with it and reruns when the query reruns.
Common visualization types, and when to use them:
| Type | Use it for | Typical shape of data |
|---|---|---|
| Bar | Comparing categories | One category column, one numeric column |
| Line | Trends over time | One date or timestamp column, one or more numeric columns |
| Area | Cumulative or stacked trends | Same as line, with a series column |
| Pie | Parts of a whole, few slices | One category column, one numeric column |
| Scatter | Relationship between two numbers | Two numeric columns |
| Histogram | Distribution of one numeric column | One numeric column |
| Counter | A single headline number | One row, one value |
| Pivot table | Cross-tabulating two dimensions | Two category columns and one numeric column |
The configuration panel is organized around a few settings you will use repeatedly:
- X column: the horizontal axis, or the category.
- Y columns: the values to plot. You can add several.
- Group by: a column whose distinct values each become a separate series.
- Aggregation: how multiple rows that share an X value are combined (sum, average, count, and so on).
- Stacking and sorting: how series are laid out and how categories are ordered.
A worked example
Start with the aggregation from the spine of this tutorial. It counts trips per pickup ZIP code and returns the top 20.
SELECT
pickup_zip,
COUNT(*) AS trips,
ROUND(AVG(fare_amount), 2) AS avg_fare
FROM samples.nyctaxi.trips
GROUP BY pickup_zip
ORDER BY trips DESC
LIMIT 20;Create a bar visualization with pickup_zip as X and trips as Y. The chart may treat the ZIP code as a number and place it on a continuous axis, which gives gaps and odd spacing. A ZIP code is an identifier, not a quantity, so convert it to text in the query:
SELECT
CAST(pickup_zip AS STRING) AS pickup_zip,
COUNT(*) AS trips,
ROUND(AVG(fare_amount), 2) AS avg_fare
FROM samples.nyctaxi.trips
GROUP BY pickup_zip
ORDER BY trips DESC
LIMIT 20;The same rule applies to any numeric-looking identifier, such as customer IDs or product codes. Cast them to strings if you want them treated as categories.
Preparing data for a time series
A line chart needs a date or timestamp on the X axis and one row per point. Raw trip rows have one timestamp each, which would be thousands of points. Bucket the timestamps in SQL first:
SELECT
DATE_TRUNC('DAY', tpep_pickup_datetime) AS pickup_day,
COUNT(*) AS trips,
ROUND(AVG(fare_amount), 2) AS avg_fare
FROM samples.nyctaxi.trips
GROUP BY DATE_TRUNC('DAY', tpep_pickup_datetime)
ORDER BY pickup_day;Choose a line visualization with pickup_day as X and trips as Y. Add avg_fare as a second Y column only if the scales are comparable. A column in the hundreds next to one in single digits makes the smaller line look flat. In that case make two charts, or use a combo chart with a secondary axis if your workspace offers it.
Aggregate in SQL or in the chart?
The visualization editor can aggregate for you, but that happens on the rows already returned to the browser. The warehouse does the work in the other case. Use this as a rule:
| Situation | Better choice |
|---|---|
| Result has fewer than a few thousand rows and you want to try different groupings | Chart aggregation |
| Source table is large | SQL aggregation, so the warehouse does the work |
| Results may be capped by the row limit | SQL aggregation |
| The same numbers feed a dashboard or an alert | SQL aggregation, so every consumer sees the same values |
Common errors
- A chart looks empty or has one bar. The X column has one distinct value, or the Y column was returned as a string. Check the column types in the result table header and cast to a numeric type if needed.
- Totals do not match the source table. The row cap truncated the result. Add an explicit aggregation and check the row count.
- Series appear in a surprising order. The chart sorts by its own setting, not by your
ORDER BY. Change the sort option in the visualization panel. - A chart disappears after you edit the query. If you rename or remove a column that the visualization uses, its mapping breaks. Remap the column in the visualization settings.
- A visualization is slow to refresh. Each refresh reruns the query on the warehouse. If it is expensive, look at the query profile (covered later in this tutorial) before changing the chart.
Parameters and visualizations together
A parameterized query, such as one filtered by :min_distance, reruns its visualizations with whatever value you enter. Charts built on a parameterized query are therefore a quick way to explore, but remember that a saved chart shows the result of the most recent run. Another person who opens the saved query may see different data if they run it with different parameter values.
Exercise
You build a bar chart of total fares per pickup ZIP from a query that selects raw rows with the editor's Limit 1000 setting on, and you let the chart sum fare_amount per ZIP. The totals are far below the real totals. Why, and how do you fix it?
Show answer
The chart can only sum the 1,000 rows the query returned, so it misses every other row. Fix it by aggregating in SQL, for example SELECT CAST(pickup_zip AS STRING) AS pickup_zip, SUM(fare_amount) AS total_fare FROM samples.nyctaxi.trips GROUP BY pickup_zip. The result then has one row per ZIP, and the chart plots exact totals.
Exercise
A bar chart of trips per pickup_zip shows uneven spacing and gaps between bars. What causes this, and what is the smallest change that fixes it?
Show answer
The ZIP code is numeric, so the chart places it on a continuous axis. Cast it to a string in the query with CAST(pickup_zip AS STRING) AS pickup_zip. The chart then treats each ZIP as a separate category.