Sub topic 2
Getting Around the Databricks SQL Editor
Assume you know SELECT, WHERE, GROUP BY, and basic cloud compute. A SQL warehouse provides the compute that runs your query. The SQL editor provides the workspace for writing and inspecting SQL.
Editor layout
The editor usually includes:
- A query editor for SQL text.
- A warehouse selector for choosing the compute resource.
- Catalog and schema selectors for browsing Unity Catalog objects.
- A Run control and query status.
- Result tabs for data, visualizations, and query profiles.
Select a warehouse before running a query. The warehouse must be running and you must have permission to use it.
Addressing data with catalog.schema.table
Unity Catalog uses a three-level name:
| Part | Meaning | Example |
|---|---|---|
| Catalog | Top-level data container | `samples` |
| Schema | Group of related tables | `nyctaxi` |
| Table | The data object | `trips` |
Use the complete name when you want the query to be unambiguous:
SHOW CATALOGS;
SELECT *
FROM samples.nyctaxi.trips
LIMIT 10;The catalog and schema selectors can help you discover names, but selecting an object does not replace checking the generated SQL.
Running queries and reading results
Run a small query first to verify the warehouse and table. The result grid shows rows and columns. You can then inspect a query profile to find expensive scans, joins, or aggregations. A visualization can turn an aggregated result into a 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;Making queries reusable with parameters
A named parameter is a placeholder that receives a value when the query runs. This lets you reuse one query without editing its SQL.
SELECT
pickup_zip,
trip_distance,
fare_amount
FROM samples.nyctaxi.trips
WHERE trip_distance > :min_distance
ORDER BY fare_amount DESC
LIMIT 50;The editor prompts for min_distance. Use a numeric value such as 10, then rerun the same query with another value.
Exercise
Why use samples.nyctaxi.trips instead of only trips?
Show answer
The three-level name identifies the catalog, schema, and table, so the query targets the intended object.