If you are learning Databricks for Data Engineering, one of the first things you should understand is Databricks Notebooks.
In our previous blog, we discussed the Databricks Workspace and its important components. Now, let’s explore one of its most commonly used features: Notebooks.
Whether you want to write Python code, execute SQL queries, perform data transformations using PySpark, or analyse large datasets, Databricks Notebooks provide an interactive environment to do all these activities.
But what exactly is a Databricks Notebook? How do you create one? Can you write Python and SQL in the same notebook?
Don’t worry! We will cover these questions step by step.
In this blog, you will learn:
- What is a Databricks Notebook?
- How to create a notebook in Databricks?
- Understanding Code Cells and Markdown Cells.
- How to execute notebook cells?
- Different programming languages supported by Databricks.
- How to use Magic Commands?
- How to work with PySpark and SQL?
- How to view and analyse notebook outputs?
- Notebook parameters, collaboration, and version history.
- Best practices for using Databricks Notebooks.
Let’s get started!
1. What is a Databricks Notebook?
A Databricks Notebook is a web-based interactive development environment where you can write code, execute it, view results, and document your work.
It is widely used by Data Engineers, Data Analysts, Data Scientists, and Machine Learning Engineers.
Using Databricks Notebooks, you can:
- Write and execute Python, SQL, Scala, and R code.
- Perform data transformations using Apache Spark.
- Read data from different storage locations.
- Create and analyse Spark DataFrames.
- Execute SQL queries on tables.
- Visualize data using charts and graphs.
- Test and debug your code.
- Collaborate with other developers.
For example, imagine you are working as a Data Engineer.
Your requirement is to read customer data from cloud storage, remove duplicate records, perform some transformations, and store the final output in a Delta table.
You can write the complete transformation logic inside a Databricks Notebook using PySpark.
You can also divide the logic into multiple cells so that you can execute and validate each step separately.
In simple words: A Databricks Notebook is like a coding workspace where you can write, execute, test, and document your data processing logic.

2. How to Create a Databricks Notebook?
Now that we understand what a notebook is, let’s see how to create one.
Follow these steps:
- Log in to your Databricks Workspace.
- Navigate to the Workspace section.
- Click on the New option.
- Select Notebook.
- Provide a name for your notebook.
- Select the default programming language, such as Python or SQL.
- Connect the notebook to an appropriate compute resource if required.
Once the notebook is created, you will see an editor where you can start writing your code.
For example, let’s create a notebook named:
Introduction_to_Databricks_Notebooks
Select Python as the default language.
Now you can start writing Python or PySpark code.
Note: The exact navigation and available compute options may differ depending on your Databricks workspace configuration.
3. Understanding Notebook Cells
One of the most important concepts in Databricks Notebooks is Cells.
A notebook is divided into multiple cells. Each cell can contain code or documentation.
Databricks mainly provides two types of cells:
- Code Cells
- Markdown Cells
Let’s understand both.
3.1 Code Cells
Code Cells are used to write and execute programming code.
You can write Python, SQL, Scala, or R code in these cells.
For example, create a Python cell and write:
print("Welcome to Databricks!")
print("Learning Databricks Notebooks")
When you execute the cell, you will get the following output:
Welcome to Databricks!
Learning Databricks Notebooks
You can create multiple code cells and execute them independently.
For example:
Cell 1:
a = 10
b = 20
Cell 2:
result = a + b
print(result)
Output:
30
Here, the second cell uses variables created in the first cell.
This makes it easier to divide your code into smaller and manageable sections.
3.2 Markdown Cells
Have you ever opened a notebook where the developer has written explanations, headings, and notes between code sections?
Those sections are generally created using Markdown Cells.
Markdown Cells are used to document your code and explain what each section does.
For example:
%md
# Customer Data Transformation
In this section, we will:
1. Read customer data.
2. Remove duplicate records.
3. Transform customer information.
4. Save the final output.
The %md command tells Databricks to render the cell content as Markdown.
You can use Markdown to add:
- Headings.
- Bullet points.
- Numbered lists.
- Tables.
- Links.
- Images.
- Code explanations.
Why are Markdown Cells useful?
Imagine you are working on a project with five other developers.
If your notebook contains only code, understanding the complete business logic might become difficult.
But if you add proper headings and explanations, other developers can easily understand what each section does.
This makes notebooks easier to maintain and collaborate on.
4. Supported Programming Languages in Databricks Notebooks
One of the interesting features of Databricks Notebooks is that they support multiple programming languages.
The four commonly supported languages are:
| Language | Usage |
|---|---|
| Python | Data processing, automation, and PySpark development |
| SQL | Querying and analysing structured data |
| Scala | Spark application development |
| R | Statistical analysis and data science |
When creating a notebook, you can select a default language.
For example, if you select Python, newly created code cells will use Python by default.
But what if you want to execute SQL queries inside the same notebook?
Databricks provides a feature called Magic Commands.
Let’s understand it.
5. What are Magic Commands in Databricks?
Magic Commands allow you to execute different types of code and perform special operations inside a notebook.
They are generally written using the % symbol.
Some commonly used Magic Commands are:
| Magic Command | Purpose |
|---|---|
%python |
Execute Python code |
%sql |
Execute SQL queries |
%scala |
Execute Scala code |
%r |
Execute R code |
%md |
Display Markdown content |
%sh |
Execute shell commands |
%fs |
Access supported filesystem operations |
%pip |
Install Python packages in the notebook environment |
Let’s understand a few examples.
5.1 Python Magic Command
%python
print("Hello from Python")
This tells Databricks to execute the cell using Python.
5.2 SQL Magic Command
Suppose you have a table named employees.
You want to retrieve all employee records.
You can write:
%sql
SELECT *
FROM employees;
Databricks will execute the SQL query and display the result.
5.3 Scala Magic Command
If you want to execute Scala code, you can use:
%scala
println("Hello from Scala")
5.4 Markdown Magic Command
You can use %md to add headings and explanations.
%md
# Employee Data Analysis
In this section, we will analyse employee records.
5.5 Shell Magic Command
You can execute shell commands using %sh.
For example:
%sh
pwd
This displays the current working directory on the notebook’s driver environment.
5.6 File System Magic Command
You can use %fs to perform supported filesystem operations.
For example:
%fs ls
This lists files and directories in the default filesystem location.
Important: Magic Commands such as %python, %sql, %scala, and %r allow you to switch the language for an individual cell.
Variables created in one language’s execution environment are not automatically available in another language’s environment. You may need to exchange data through tables, files, or other shared resources.
6. Can We Use Python and SQL in the Same Notebook?
Yes!
This is one of the most useful features of Databricks Notebooks.
You don’t need to create separate notebooks just because you want to execute Python and SQL code.
Let’s understand this with an example.
Suppose you have a Spark DataFrame containing employee information.
First, create the DataFrame using Python.
Cell 1: Python
%python
data = [
(1, "Ankit", "IT", 50000),
(2, "Rahul", "HR", 40000),
(3, "Priya", "IT", 60000),
(4, "Amit", "Finance", 45000)
]
columns = ["id", "name", "department", "salary"]
df = spark.createDataFrame(data, columns)
display(df)
Now, you want to analyse the employee data using SQL.
You can create a temporary view from the DataFrame.
Cell 2: Python
%python
df.createOrReplaceTempView("employee_data")
Now execute a SQL query.
Cell 3: SQL
%sql
SELECT
department,
AVG(salary) AS average_salary
FROM employee_data
GROUP BY department;
The output will look something like this:
| department | average_salary |
|---|---|
| IT | 55000 |
| HR | 40000 |
| Finance | 45000 |
Here, we used Python to create a DataFrame and SQL to analyse it.
This is particularly useful in Data Engineering projects where you might use PySpark for complex transformations and SQL for data validation or analysis.
7. How to Execute a Databricks Notebook?
Writing code is only one part of working with notebooks.
The next step is executing the code.
Databricks provides different options to execute notebook cells.
7.1 Execute a Single Cell
If you want to execute only one cell, click the Run button associated with that cell.
Databricks will execute that cell and display its output.
7.2 Execute Multiple Cells
You can execute multiple selected cells or run the entire notebook.
This is useful when your code contains multiple dependent steps.
For example:
Cell 1: Import Libraries
Cell 2: Read Source Data
Cell 3: Clean Data
Cell 4: Apply Transformations
Cell 5: Save Output
You can execute these cells in sequence.
7.3 Run All Cells
Databricks also provides a Run All option.
When you select this option, all notebook cells are executed in notebook order.
This is useful when you want to execute the complete notebook from beginning to end.
Useful Keyboard Shortcuts
Some commonly used notebook shortcuts are:
| Action | Shortcut (Windows) |
|---|---|
| Run current cell | Shift + Enter |
| Run selected cell(s) | Ctrl + Enter |
| Enter Edit Mode | Enter |
| Exit Edit Mode | Esc |
These shortcuts can make notebook development faster.
8. Understanding Notebook Outputs
After executing a cell, Databricks displays the result below that cell.
Depending on the code, the output can be:
- Text output.
- Tables.
- DataFrames.
- Charts and graphs.
- Error messages.
- Other supported visualizations.
For example:
data = [
("IT", 10),
("HR", 5),
("Finance", 8)
]
df = spark.createDataFrame(data, ["department", "employee_count"])
display(df)
The display() function shows the DataFrame in a tabular format.
You can also explore visualization options for supported outputs.
For example, you can create a bar chart to compare the number of employees in different departments.
This is useful when analysing large datasets and presenting results.
9. Notebook Parameters Using Widgets
Sometimes, you may want to execute the same notebook with different input values.
For example, imagine you have a notebook that processes sales data.
Today, you want to process data for January.
Tomorrow, you want to process data for February.
Instead of modifying the code every time, you can use Widgets to accept input values.
Widgets allow you to create interactive input parameters in notebooks.
For example:
dbutils.widgets.text(
"month",
"January",
"Enter Month"
)
month = dbutils.widgets.get("month")
print(f"Processing data for {month}")
Here:
dbutils.widgets.text()creates a text input widget."month"is the widget name."January"is the default value.dbutils.widgets.get()retrieves the selected input value.
You can use the input parameter in your processing logic.
For example:
month = dbutils.widgets.get("month")
filtered_df = df.filter(
df.month == month
)
display(filtered_df)
Now the same notebook can process different months based on the input value.
Where are Widgets useful?
- When creating reusable notebooks.
- When passing parameters to data processing logic.
- When executing notebooks through automated workflows.
- When developing parameter-driven ETL pipelines.
10. Sharing and Collaboration in Databricks Notebooks
In real-world projects, multiple developers often work together.
Databricks provides collaboration features that make it easier to share and maintain notebooks.
Some useful capabilities include:
1. Sharing Notebooks
You can share notebooks with other users or groups, subject to workspace permissions.
2. Real-Time Collaboration
Supported collaboration features allow multiple users to work together on notebooks.
3. Comments
You can use comments to discuss code and provide feedback.
4. Version History
Databricks maintains notebook revision history, allowing you to review previous versions and restore an earlier version when needed.

5. Git Integration
You can use Git Folders to manage notebooks through a Git repository.
This is useful when working on projects with multiple developers and maintaining code changes.
11. Can We Schedule a Databricks Notebook?
Yes!
You can automate notebook execution using Databricks Jobs (Lakeflow Jobs).
Suppose you have created a notebook that performs the following activities:
- Reads raw customer data.
- Removes duplicate records.
- Applies business transformations.
- Loads the final data into a Delta table.
You don’t want to execute this notebook manually every day.
You can create a Job, configure the notebook as a task, and define a schedule.
For example:
Databricks Job
|
v
Notebook Task
|
v
Read Raw Data
|
v
Apply Transformations
|
v
Save Output
You can configure:
- Job schedule.
- Task dependencies.
- Parameters.
- Retry settings.
- Notifications.
- Execution monitoring.
This makes Databricks Notebooks useful not only for development but also as part of automated data engineering workflows.
12. Best Practices for Using Databricks Notebooks
Here are some simple practices that can help you write better and more maintainable notebooks.
1. Use Meaningful Notebook Names
Avoid names like test1, demo2, or final_final.
Use meaningful names such as:
customer_data_transformationsales_data_analysisemployee_etl_pipeline
2. Divide Code into Multiple Cells
Avoid writing your complete logic in a single cell.
Divide your code into logical steps such as reading data, cleaning data, transforming data, and writing output.
3. Add Markdown Documentation
Use Markdown cells to explain your business logic and processing steps.
This makes your notebook easier for other developers to understand.
4. Avoid Repeating Code
Create reusable functions for common operations.
This makes your code cleaner and easier to maintain.
5. Use Appropriate Compute Resources
Select compute resources based on your workload.
Using unnecessarily large compute resources can increase costs.
6. Use Parameters for Reusable Notebooks
Use widgets and job parameters when the same notebook needs to process different inputs.
7. Maintain Code Using Git
For team projects, use Git integration to manage changes and maintain version history.
8. Avoid Hardcoding Sensitive Information
Never store passwords, access keys, or other sensitive credentials directly inside notebook code.
Use supported secret-management and identity mechanisms instead.
13. Conclusion
Databricks Notebooks are one of the most important features of the Databricks platform.
They provide an interactive environment where you can write code, execute queries, process large datasets, visualize results, and collaborate with other developers.
In this blog, we covered:
- What Databricks Notebooks are.
- How to create a notebook.
- Code Cells and Markdown Cells.
- Supported programming languages.
- Magic Commands.
- Executing Python and SQL in the same notebook.
- Running notebook cells.
- Viewing notebook outputs.
- Using Widgets and parameters.
- Collaboration and version history.
- Scheduling notebooks using Jobs.
If you are starting your Databricks journey, try creating a notebook, writing a few Python commands, executing a SQL query, and exploring PySpark DataFrames.
Once you become comfortable with these basics, you can start building real-world ETL pipelines using Databricks.
Official References
For more information, you can explore the official Databricks documentation:
- Databricks Notebooks
- Basic Editing in Databricks Notebooks
- Develop Code in Databricks Notebooks
- Databricks Notebook Widgets
- Run Databricks Notebooks
Note: Databricks interface options and certain features may vary depending on your cloud provider, workspace configuration, permissions, and Databricks Runtime version.