Open In App

PL/SQL NOT Operator

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PL/SQL, an extension of SQL in Oracle, offers various operators that allow us to perform logical operations on data. One such operator is the NOT operator, which is used to negate a condition, meaning it will return true if the condition is false and vice versa.

The NOT operator is commonly used in conjunction with other logical operators to filter data or control the flow of execution in PL/SQL programs. In this article, we will explore the concept of PL/SQL NOT Operator providing detailed explanations and examples to illustrate their usage and benefits.

What is PL/SQL NOT Operator?

The NOT operator is a unary logical operator used to reverse the result of a condition. If a condition evaluates to true, applying the NOT operator will make it false, and if a condition is false, NOT will make it true.

It is particularly useful when we need to exclude certain rows or conditions in SQL queries and PL/SQL blocks.

Syntax

NOT condition

Here, condition refers to any condition that can be evaluated as true or false

In a WHERE Clause:

SELECT column_name

FROM table_name

WHERE NOT condition;

The NOT operator is used within the WHERE clause to filter out rows that meet a specific condition.

Example of PL/SQL NOT Operator

In this example, the Employees table is created with three columns: EmployeeID, Name, and Department. The EmployeeID column is the primary key, ensuring that each employee has a unique identifier.

Here, we are also inserting four records into the Employees table, each representing an employee with a unique EmployeeID, their name, and their respective department.

Employees Table:

CREATE TABLE Employees (
EmployeeID NUMBER PRIMARY KEY,
Name VARCHAR2(50),
Department VARCHAR2(50)
);

INSERT INTO Employees (EmployeeID, Name, Department) VALUES (1, 'John Doe', 'IT');
INSERT INTO Employees (EmployeeID, Name, Department) VALUES (2, 'Jane Smith', 'HR');
INSERT INTO Employees (EmployeeID, Name, Department) VALUES (3, 'Robert Brown', 'Finance');
INSERT INTO Employees (EmployeeID, Name, Department) VALUES (4, 'Emily Davis', 'IT');

Output:

employees-table

Projects Table:

The Projects table is created with three columns: ProjectID, ProjectName, and AssignedTo. The AssignedTo column is a foreign key referencing the EmployeeID from the Employees table, establishing a relationship between employees and the projects they are assigned to

We insert four records into the Projects table, where each project is assigned to a specific employee based on their EmployeeID. One of the projects (Project Gamma) has no employee assigned, indicated by the NULL value in the AssignedTo column.

CREATE TABLE Projects (
ProjectID NUMBER PRIMARY KEY,
ProjectName VARCHAR2(100),
AssignedTo NUMBER REFERENCES Employees(EmployeeID)
);

INSERT INTO Projects (ProjectID, ProjectName, AssignedTo) VALUES (1, 'Project Alpha', 1);
INSERT INTO Projects (ProjectID, ProjectName, AssignedTo) VALUES (2, 'Project Beta', 3);
INSERT INTO Projects (ProjectID, ProjectName, AssignedTo) VALUES (3, 'Project Gamma', NULL);
INSERT INTO Projects (ProjectID, ProjectName, AssignedTo) VALUES (4, 'Project Delta', 4);

Output:

project-not-table
Project Table

Example 1: Exclude Specific Department

This query retrieves the names of all employees whose department is not 'HR' by using the NOT operator to exclude any records where the department is 'HR'. The result includes only employees from other departments, such as 'IT' and 'Finance'.

Query:

SELECT Name
FROM Employees
WHERE NOT Department = 'HR';

Output:

not-ex1
Exclude Specific Department

Explanation:

  • The output shows names of employees who are not part of the 'HR' department. Specifically:

    • John Doe (Department: 'IT')
    • Robert Brown (Department: 'Finance')
    • Emily Davis (Department: 'IT')
  • The query effectively excludes the employee in the 'HR' department, Jane Smith, from the results.

Example 2: Find Employees Not Assigned to Projects

This query retrieves the names of employees who are not assigned to any project. The NOT EXISTS clause ensures that for each employee, there is no corresponding record in the Projects table, effectively identifying those who are not associated with any project.

Query:

SELECT Name
FROM Employees e
WHERE NOT EXISTS (SELECT 1 FROM Projects p WHERE p.AssignedTo = e.EmployeeID);

Output:

not-ex2
Find Employees Not Assigned to Projects

Explanation:

  • The output lists Jane Smith because she does not have any associated projects in the Projects table.
  • The NOT EXISTS clause ensures that only employees with no entries in the Projects table (i.e., no project assignments) are included in the result.
  • Other employees who have been assigned to projects (e.g., John Doe, Robert Brown, and Emily Davis) are excluded from the result set.

Example 3: Projects Not Assigned to Any Employee

This query fetches the names of all projects that are assigned to an employee. The NOT operator is used here with the IS NOT NULL condition to exclude projects that do not have an assigned employee.

Query:

SELECT ProjectName
FROM Projects
WHERE AssignedTo IS NOT NULL;

Output:

not-ex3
Projects Not Assigned to Any Employee

Explanation:

  • The output includes Project Alpha, Project Beta, and Project Delta because these projects have a non-NULL value in the AssignedTo column, indicating that they are assigned to employees.
  • The project Project Gamma is excluded from the results because its AssignedTo column is NULL, meaning it has no assigned employee.
  • The query does not return "Jane Smith" because Jane Smith is an employee, not a project. The output correctly lists projects that have been assigned to employees, not employees themselves.

Example 4: Use of NOT in an IF Condition

This query checks if the employee with ID 2 is not in the IT department. Depending on the result, it returns a message indicating whether the employee is or isn't in the IT department. The NOT operator is used within a CASE statement to evaluate the condition.

Query:

SELECT CASE 
WHEN NOT Department = 'IT' THEN 'Employee is not in IT department'
ELSE 'Employee is in IT department'
END AS Department_Status
FROM Employees
WHERE EmployeeID = 2;

Output:

not-ex4
Use of NOT in an IF Condition

Explanation :

This query checks if the employee with `EmployeeID = 2` is in the 'IT' department. If the department is not 'IT', it returns 'Employee is not in IT department'; otherwise, it returns 'Employee is in IT department'. The result is displayed as `Department_Status`.

Conclusion

The PL/SQL NOT operator is a powerful tool for filtering and managing data in Oracle databases. By using NOT with logical conditions, we can exclude specific records that meet certain criteria, enhancing the precision of our queries. Whether used in WHERE clauses or in combination with other operators, NOT helps us refine our data retrieval and manipulation processes.


Next Article
Article Tags :

Similar Reads