Class NotExists
- Namespace
- YndigoBlue.Velocity.Functions
- Assembly
- YndigoBlue.Velocity.dll
Represents the NOT EXISTS operator that tests whether a subquery returns no rows.
public class NotExists : Function, ICheckItem, IDefaultItem, IFilterItem, IElement
- Inheritance
-
NotExists
- Implements
Remarks
The NOT EXISTS operator returns true if the subquery returns zero rows, and false if it returns one or more rows. Like EXISTS, it uses short-circuit evaluation for efficiency. This is useful for finding records without related data, implementing anti-joins, or filtering based on the absence of matching conditions. NOT EXISTS is often more readable and efficient than LEFT JOIN with NULL checks for finding unmatched records.
Examples
Find all orders that have not yet been shipped:
var schema = manager.LoadSchema("logistics");
var orders = schema["orders"];
var shipments = schema["shipments"];
var subQuery = new Query()
.Select([ shipments["id"] ])
.From(shipments)
.Where(new Criterion<Column>(shipments["order_id"], ConditionalType.Equals, orders["id"]));
var query = new Query()
.Select([ orders["id"], orders["status"] ])
.From(orders)
.Where(new Criterion<Expression>(new Expression(new NotExists(subQuery))));
var results = manager.Retrieve(query);
Constructors
- NotExists(IEnumerable<IElement>)
Initializes a new instance of NOT EXISTS for multiple elements.
- NotExists(IElement)
Initializes a new instance of NOT EXISTS for a single element.