Skip to content

Lesson 3 of 6

Run your first query in a notebook

Create a notebook, attach it to your cluster, and run SQL to read a CSV file from cloud storage.

4 minDatabricks

After this lesson, you will be able to create a notebook in your Databricks workspace, attach it to a cluster, and run SQL to read a CSV file from cloud storage.

Create a notebook

In the Databricks sidebar, click New and select Notebook. Give the notebook a name, for example first_query. Choose SQL as the default language. Leave the cluster dropdown as is for now; you will attach the notebook to a cluster in the next step. Click Create.

The notebook opens with an empty cell. A cell is where you type code or SQL. You can run the cell by pressing Shift+Enter or clicking the run icon.

Attach the notebook to a cluster

At the top of the notebook, you see a dropdown that says Detached. Click it and select the cluster you started in the previous lesson. If you do not have a running cluster, start one now from the Compute section.

When the notebook is attached, the dropdown shows the cluster name and a green dot. The cluster is now ready to run your SQL.

Read a CSV file from cloud storage

Databricks can read files directly from cloud object storage. You need the path to a CSV file. If you do not have one, create a small CSV file and upload it to your cloud storage. For example, a file named people.csv with these contents:

name,age,city
Alice,30,London
Bob,25,Paris
Carol,35,Berlin

Upload the file to a location your cluster can access. If your workspace was created with a cloud storage location, you can use that. Otherwise, use a public or private bucket or container that your cluster has permission to read.

In the notebook cell, type the following SQL. Replace <path-to-csv> with the full path to your file, for example s3://my-bucket/people.csv or abfss://[email protected]/people.csv.

SELECT * FROM csv.`<path-to-csv>`

Run the cell. The result table shows the rows from the CSV file. The csv. prefix tells Databricks to use the CSV data source. The backticks around the path are required because the path contains special characters.

Understand the query

The query reads the whole CSV file and returns all rows and columns. The column names come from the header row in the file. The data types are inferred automatically: name and city are strings, age is an integer.

You can filter the data with a WHERE clause, just like in any SQL query. For example:

SELECT name, city FROM csv.`<path-to-csv>` WHERE age > 28

This returns only the rows where age is greater than 28.

Save the notebook

Your work is saved automatically, but you can also click File and then Save to be sure. The notebook is now part of your workspace and can be shared with others later.

Key takeaways

  • A notebook is a place to write and run SQL or code in Databricks.
  • You must attach a notebook to a running cluster before you can execute cells.
  • Use the csv. prefix and backticks to read a CSV file from cloud storage with SQL.
  • The result of a query appears directly below the cell in the notebook.

Try it

Create a new notebook and attach it to your cluster. Upload a CSV file with at least five rows and three columns to your cloud storage. Write a query that selects only two of the columns and filters the rows using a condition on one column. Run the query and check that the result shows only the expected rows and columns. Then modify the query to sort the result by one of the selected columns in descending order and run it again.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.

    Run your first query in a notebook | Databricks Lakehouse Foundations: Workspaces and Clusters | Rolebound