Member-only story

#76 Elevate Your SQL Skills in Azure Databricks: Tips for Writing More Efficient Queries

Hang Nguyen
4 min readJan 3, 2023

--

Let’s dive into some of tricks and tips to write better SQL query, especially in Azure Databricks!

SELECT 1

SELECT 1 is a simple way of selecting a constant value 1 from a table. It is often used as a placeholder in a SELECT statement when you need to select something, but the value itself is not important. For example, you might use SELECT 1 in a query to check if a record exists in a table, like this:

SELECT 1 FROM users WHERE username = 'abc';

This query will return a single row with a value of 1 if a record with the username of abc exists in the users table, and no rows if it does not.

It’s useful when you want to cheaply determine if record matches your where clause and/or join.

UPSERT

UPSERT is a term used to describe the combination of the UPDATE and INSERT statements. It allows you to either update a record if it already exists, or insert a new record if it does not.

The exact syntax for UPSERT varies depending on the database you are using, in Databricks as follows.

MERGE INTO target_table USING source_table
ON target_table.key_column = source_table.key_column
WHEN MATCHED THEN
UPDATE SET target_table.column1 = source_table.column1, target_table.column2 = source_table.column2, ...
WHEN NOT MATCHED THEN
INSERT (column1, column2, ...)…

--

--

Hang Nguyen
Hang Nguyen

Written by Hang Nguyen

Just sharing (data) knowledge

No responses yet