Azure SQL Database query ideas - lookup and anti-lookup

Azure SQL Database query ideas - lookup and anti-lookup

Does this data exist in another table?

This post is part of the blog series created primarily for database engineers, with the hope that it might help you get a head start on your development tasks. With that being said, let us dive in...


There may be situations when you need to process certain data only if it exists in another table (lookup scenario), and there may also be situations when you need to process certain data only if it does not exist in another table (anti-lookup scenario). In this blog post we will take a look at both.

Lookup Scenario

Say you have a table called LKP_LOCATION that maintains the list of location identifiers and corresonding geographical coordinates of different field devices. The transaction data only has geographical coordinates. You need to bring in the location identifiers by looking-up the LKP_LOCATION table. This seems straightforward. We can do a join of transaction table with LKP_LOCATION table on geographical coordinates and get corresponding location identifiers. If there is a new device in the field that does not have a row in the LKP_LOCATION table, and you still want the transaction to be processed, you can make this a left (or right, based on how the query is written) join such that new devices will get a NULL value for location id. An inner join will cause such transactions for the new device to be dropped if they do not have a match in the LKP_LOCATION table.

Anti-Lookup Scenario

Consider that you have a table called LKP_TRANSACTION that maintains the list of unique transaction identifiers for all the transactions processed. You must process new data from your transactions table only if it is a valid new transaction, i.e. the transaction unique identifier does not exist in LKP_TRANSACTION table. One way to achieve this is by performing a left (or right, based on how the query is written) join and checking for a NULL value on the resulting join to check for a value that exists in one table but does not exist in another.

select a.*
from
    transactions a
left join
    lkp_transaction b
    on  a.transaction_date = b.transaction_date
    and a.uuid = b.uuid
where b.uuid is NULL

In the series of posts titled "Azure SQL Database query ideas", I will cover some of the topics that I usually encounter as part of my projects.

Did you find this article valuable?

Support Sneh Bhatt by becoming a sponsor. Any amount is appreciated!