Course contentsShow
← Databases & SQL
Lesson 147 of 1,9532. Querying and Data RetrievalFree lesson

Handling Encrypted Data in Filters

Retrieve sensitive information safely without compromising underlying encryption standards.

In this lesson, you will learn how to search through encrypted columns without exposing sensitive raw data or weakening your security protocols.

The Encryption Challenge

When data in a database is stored using encryption (like AES), it is transformed into unreadable "ciphertext." If you run a standard query like SELECT * FROM users WHERE email = 'alice@example.com', the database compares the raw string against the encrypted blob. Because the encrypted version of "alice@example.com" looks like random gibberish, your filter will return no results, even if the record exists.

Decryption vs. Deterministic Filtering

There are two common ways to handle this. The first is decryption on-the-fly, where you use a function to decrypt the column before the filter is applied. However, this is computationally expensive and potentially insecure if logs capture the decrypted values.

A better approach for filtering is Deterministic Encryption. In this method, the same input always produces the exact same ciphertext. If you encrypt "alice@example.com" and get aX92jL, that string will always result in aX92jL. This allows you to filter the data without ever needing to decrypt it during the search process.

Illustrative Example

Imagine you have a table of customers where the tax_id is encrypted. Instead of decrypting the column to find a specific person, you encrypt your input first in your application layer, then pass that encrypted value to the SQL query:

-- We calculate the ciphertext for the ID in our app, then query:
SELECT customer_name 
FROM customers 
WHERE encrypted_tax_id = 'x8f921...a2b9';

By querying for the ciphertext directly, the database engine simply performs a high-speed match against the stored binary data. The database never sees the plaintext, the CPU avoids the heavy lifting of decryption, and your sensitive information remains locked safely behind your security standards.

When you need to perform more complex lookups, you can combine this with Common Table Expressions to handle the encrypted inputs safely before performing your final JOIN or FILTER operations.

Key Takeaway: To filter encrypted data securely, use deterministic encryption to query against ciphertext rather than decrypting sensitive data within your SQL statements.