Skip to content
Advertisement

list records which do not meet a certain criteria in Oracle SQL

I got 3 tables please see the below. Currently, a car has a unique tag assigned to them. I have employees with multiple roles. There are only 3 roles. 1 = Surveyor, 2 = Admin or 3 = Engineer.

A car can be assigned to more than one employee. One car can have a surveyor, admin and engineer using it. But I would like to have a list of all car tags that do not have a surveyor.

CarTags
tag
company_name
account_id
Employees
id
name
email
account_id
role_id
Roles
employee_id
role_name
role_id

For the column role_id in the Roles table, there can be 3 options. 1 = Surveyor, 2 = Admin or 3 = Engineer

The below Oracle SQL query returns me a list of all car tags, company names, employee id, employee names, employee emails and role id’s but how can I get a list of all car tags which do not have a surveyor assigned to it?

Advertisement

Answer

I used CTE (common table expression) to get a list of account_id with a Surveyor in it. Then on your query, I will use that cte table to exclude accounts_ids in the where condition.

See sample data here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement