Sub topic 8
Running queries and reading results
This chapter assumes you know basic SQL (SELECT, WHERE, GROUP BY), can open the SQL editor, and have a running SQL warehouse attached to it. It covers how a query runs, how to read the results, and the mistakes people make most often.
What happens when you press Run
When you select Run, the editor sends your statement to the SQL warehouse chosen in the compute picker. The warehouse plans the query, runs it on its clusters, and streams the result back to the editor. If the warehouse is stopped, it starts first. A serverless warehouse usually starts in seconds. A pro or classic warehouse can take several minutes.
Running one statement or several
The editor runs what you select. If you highlight text, only that text runs. If nothing is highlighted, the editor runs the statement where your cursor sits. If your file holds several statements, separate them with semicolons and highlight the ones you want. Use this to keep a scratch file of related queries without running all of them each time.
Start with two queries to confirm you can see data:
SHOW CATALOGS;
SELECT * FROM samples.nyctaxi.trips LIMIT 10;The first lists the catalogs you can access. The second reads a sample table by its full catalog.schema.table name. The LIMIT keeps the result small while you explore.
Reading the result
Below the editor, the result table shows rows and columns. Check these things each time:
- Row count and time. The result area reports how many rows came back and how long the query took.
- Truncation. The editor displays a limited number of rows. A message tells you when the display is cut off. Add your own
LIMITor aggregate the data instead of relying on the display cap. - Column types. Each column header shows its type. A number stored as a string sorts alphabetically, so check the type when an
ORDER BYlooks wrong.
Now run an aggregation:
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;The result has one row per ZIP code, with a trip count and an average fare. The ORDER BY trips DESC clause puts the busiest zones first. Without ORDER BY, row order is not guaranteed.
Previewing the plan with EXPLAIN
EXPLAIN shows the query plan without running the query. It is useful before you run something expensive on a large table.
EXPLAIN
SELECT pickup_zip, COUNT(*) AS trips
FROM samples.nyctaxi.trips
GROUP BY pickup_zip;The output lists the steps the engine plans to take, such as scanning the table and aggregating. After a query has run, the query profile gives the measured version of the same information.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `TABLE_OR_VIEW_NOT_FOUND` | Wrong catalog or schema, or no access | Use the full three-level name and check your privileges |
| Query waits a long time before starting | Warehouse is starting or queued behind other queries | Check the warehouse state and size |
| Only part of the data appears | Display row limit | Aggregate, filter, or add `LIMIT` |
| Wrong rows ran | Text was highlighted | Click in the editor to clear the selection |
Exercise
You highlight the last line of a file that contains three statements and press Run. Which statements run, and what would change if you clicked once to clear the highlight first?
Show answer
Only the highlighted statement runs. With nothing highlighted, the editor runs the single statement where the cursor sits, so after clearing the highlight only the statement under the cursor would run. In neither case do all three run.
Exercise
Why is EXPLAIN useful before running a query on a very large table, and what does it not tell you?
Show answer
It shows the planned steps without executing the query, so you can spot a full scan or an unexpected join before spending compute. It does not give measured times or rows processed. The query profile gives those after the query has run.