Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data. We need
to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI-ISO Standard SQL.
Now we have to guess at the DDL because you have bad manners. And do the typing for you, too.
Please think about how silly your EXISTS (SELECT <column list>..) is. Just write EXISTS (SELECT * ..) instead. EXISTS() is a predicate; it will only return TRUE or FALSE. Please Google why we do not use camelCase any more.
Your next_inspection_date is wrong. It is not an attribute, but a calculation. It changes from day to day! The prefix next_ is called a role, the inspection is an attribute and the postfix _date is an attribute property. You need to learn data modeling,
but do not try to read ISO-11179 until you have at least one basic book or course on this topic.
I will guess that the inspections are in a schedule for each piece of equipment. SQL is a database language, not a computational language for weird temporal math. Is a 100 years big enough?
CREATE TABLE Equipment_Inspections
(inspection_date DATE NOT NULL,
equipment_id CHAR(15) NOT NULL
REFERENCES Equipment(equipment_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
PRIMARY KEY (inspection_date, equipment_id),
inspection_status CHAR(5) DEFAULT 'schd' NOT NULL
CHECK (inspection_status IN ('schd', 'miss', 'done', ..)),
..);
To find the next scheduled inspection is now a simple query with the CURRENT_TIMESTAMP.
SELECT equipment_id, MIN(inspection_date) AS next_inspection_date
FROM Equipment_Inspections
WHERE inspection_date >= CURRENT_TIMESTAMP
GROUP BY equipment_id;
Most of the work in SQL is done in the DDL, not the DML.