Skip to main content
Log inGet a demo
Logical operators

SQL LNNVL

What is SQL LNNVL?

The SQL LNNVL (Logical Not Null Value Logic) function is used to evaluate a condition that checks for the absence of NULL values. It returns true for rows where the specified condition is not true or when the condition contains NULL values. LNNVL is often used for testing conditions that involve NULL values and returning results that do not contain NULL.

When you would use it

You would use the SQL LNNVL function when you want to evaluate conditions and exclude rows that contain NULL values from the result. It is especially useful when working with complex queries where NULL values need to be handled differently or when you need to ensure that results do not include NULL.

Syntax

The syntax for the SQL LNNVL function is as follows:

LNNVL(condition)
  • condition: The condition you want to evaluate. If the condition is not true or contains NULL values, LNNVL returns true.

Parameter values

  • condition: The condition to evaluate. If the condition is not true or contains NULL values, LNNVL returns true.

Example query

Suppose we have a table named "employees" with columns "employee_id," "employee_name," and "department." We want to retrieve employees who are not assigned to a specific department ('Unassigned' department), including those with NULL department values. Here's the SQL query using the LNNVL function to achieve this:

SELECT employee_id, employee_name, department
FROM employees
WHERE LNNVL(department = 'Unassigned');

In the above query, we use the LNNVL function to exclude rows with 'Unassigned' in the department column and include rows with NULL in the department column.

Example table response

Assuming the "employees" table contains the following data:

| employee_id | employee_name | department   |
|------------ |-------------- | -----------  |
| 1          | John         | NULL         |
| 2          | Alice        | Sales        |
| 3          | Bob          | Unassigned   |
| 4          | Carol        | Marketing    |
| 5          | David        | NULL         |

The query mentioned earlier would return the following result:

| employee_id | employee_name | department   |
|------------ |-------------- | -----------  |
| 1          | John         | NULL         |
| 2          | Alice        | Sales        |
| 4          | Carol        | Marketing    |
| 5          | David        | NULL         |

This result includes employees who are not assigned to the 'Unassigned' department and those with NULL department values, achieved using the LNNVL function.

Use cases

  • Excluding rows that contain NULL values from query results.
  • Handling conditions involving NULL values in complex queries.
  • Ensuring that results do not include rows with specific values or NULL values.

SQL languages this is available for

The SQL LNNVL function is not a standard SQL feature, and its availability may vary between database management systems. It is primarily used in Oracle Database. Other database systems may have equivalent functions or use different approaches to achieve similar results when dealing with NULL values. If you are using a specific database system, consult its documentation for the equivalent function or approach for handling NULL values in conditions.


Keep reading

SQL NULLIF

Read article

SQL NANVL

Read article

SQL INTERVAL

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.