Sub topic 10
Working with result tables and visualizations
This chapter assumes you can write basic SQL and run a query in the SQL editor against a running SQL warehouse. It covers what you can do with a result after a query finishes: inspect it, filter it, download it, and turn it into a chart.
The result table
When a query finishes, the editor shows the rows in a result table below the query text. Each column header shows a type icon. You can sort by clicking a header and resize or hide columns from the table settings. These changes affect only how you view the result. They do not change the query or rerun it.
The editor also displays a limited number of rows. Queries without a LIMIT can return far more rows than the table shows, and the editor tells you when the display is truncated. Add a LIMIT while exploring, and use aggregation to shrink a result before you chart it.
Filtering and downloading
Use the result filter to narrow the rows you see without editing the SQL. This is useful for a quick check, but it works on the rows already returned. If you need a different slice of the data, change the WHERE clause and rerun.
You can download the result as a CSV or Excel file, or copy it to the clipboard. A download contains only the rows the editor retrieved, so a truncated result gives you a truncated file.
Building a visualization
A visualization is a chart or table tied to one query. To add one, click the + next to the result tab and choose Visualization. Pick a type, then map query columns to the chart's roles.
Run this query first. It returns one row per pickup ZIP code, which suits a bar chart:
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;Then create a bar chart with these settings:
- Set X column to
pickup_zip. - Set Y columns to
trips. - Give the chart a clear title and axis labels.
| Chart type | Use it for | Needs |
|---|---|---|
| Bar | Comparing categories | One category column, one numeric column |
| Line | Change over time | A date or timestamp column, one or more numeric columns |
| Pie | Parts of a whole, with few categories | One category column, one numeric column |
| Scatter | Relationship between two numbers | Two numeric columns |
| Counter | One headline number | A single-row result |
Each visualization reads from the query's result. If you change the query, the visualization updates the next time the query runs.
Common problems
- The chart looks wrong because the data is not aggregated. A chart plots the rows it receives. Do the grouping in SQL, not in the chart.
- Categories appear in a strange order. Sort in the query with
ORDER BY, or set the sort option on the chart's X axis. - A date column is treated as text. Cast it in the query, for example with
CAST(col AS DATE), so a line chart can order it as time. - The chart is slow or crowded. A chart with thousands of points is hard to read. Reduce the rows with
LIMITor a coarser grouping.
Where visualizations go next
A visualization lives with its query. You can add it to a dashboard, which a later chapter covers, so the same result serves many readers without each of them rerunning SQL by hand.
Exercise
A bar chart of trips per ZIP code shows 40,000 bars and is unreadable. The query has no GROUP BY. What should you change, and where?
Show answer
Change the query, not the chart. Add GROUP BY pickup_zip with COUNT(*) AS trips, and add ORDER BY and LIMIT to keep the top categories. The chart then receives one row per ZIP code, with a small number of bars.
Exercise
You download a result as CSV, but the file has fewer rows than you expected from the table. What is a likely cause?
Show answer
The editor truncated the displayed result, and the download contains only the retrieved rows. Add a filter or aggregation to reduce the result below the display limit, or use another method, such as the Statement Execution API, to retrieve the full data.