PL/SQL NOT EQUAL Operator
Last Updated :
23 Jul, 2025
In PL/SQL, the NOT EQUAL operator is used to compare two values and determine if they are not equal. If the values are different, the result of the comparison is true; otherwise, it is false.
This operator is often used in conditional statements and queries to filter data based on inequality. In this article, We will learn about PL/SQL NOT EQUAL Operator with the help of various examples and so on.
What is PL/SQL NOT EQUAL Operator?
The NOT EQUAL operator in PL/SQL is represented by != or <>. It is used to check if two expressions or values are not equal to each other.
This operator can be used in WHERE clauses, IF statements, and other conditional expressions.
Syntax:
Using != Operator:
expression1 != expression2
Using <> Operator:
expression1 <> expression2
Explanation:
- expression1: The first value or column to compare.
- expression2: The second value or column to compare.
- != or <>: The NOT EQUAL operator that performs the comparison.
Examples of PL/SQL NOT EQUAL Operator
We create two tables, employees
and departments
, to demonstrate the use of the NOT EQUAL operator. The employees
table stores employee details, while the departments
table holds department information.
1. Employees Table
The employees
table is set up with emp_id
, emp_name
, and dept_id
, while the departments
table includes dept_id
and dept_name
. We insert sample data into both tables for demonstration purposes.
Query:
-- Create `employees` table
CREATE TABLE employees (
emp_id NUMBER PRIMARY KEY,
emp_name VARCHAR2(50),
dept_id NUMBER
);
-- Insert data into `employees`
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (1, 'John Doe', 101);
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (2, 'Jane Smith', 102);
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (3, 'Emily Davis', 103);
Output:
emp_id | emp_name | dept_id |
---|
1 | John Doe | 101 |
2 | Jane Smith | 102 |
3 | Emily Davis | 103 |
Explanation:
- The
CREATE TABLE
statement defines the employees
table with columns for employee ID, name, and department ID.
- The
INSERT INTO
statements add three records to this table: John Doe in department 101, Jane Smith in department 102, and Emily Davis in department 103.
- This setup creates a basic structure for storing employee details and their departmental affiliations.
2. Department Table
The CREATE TABLE
statement establishes the departments
table with two columns: dept_id
(the unique identifier for each department) and dept_name
(the name of the department).
The INSERT INTO
statements add three records to the table, specifying department IDs and their corresponding names: HR (101), Finance (102), and IT (104).
Query:
-- Create `departments` table
CREATE TABLE departments (
dept_id NUMBER PRIMARY KEY,
dept_name VARCHAR2(50) );
-- Insert data into `departments`
INSERT INTO departments (dept_id, dept_name) VALUES (101, 'HR');
INSERT INTO departments (dept_id, dept_name) VALUES (102, 'Finance');
INSERT INTO departments (dept_id, dept_name) VALUES (104, 'IT');
Output:
dept_id | dept_name |
---|
101 | HR |
102 | Finance |
104 | IT |
Explanation:
The above department table is the output of the above query displaying the department information .This table represents the department information with unique IDs and names, capturing the organizational structure relevant to the employees' departmental affiliations.
Example 1: Basic Comparison
This SQL query retrieves the names of employees whose department ID is not equal to 101. It uses the != operator in the WHERE clause to filter out employees from department 101 and return only those who belong to other departments.
Query:
SELECT emp_name
FROM employees
WHERE dept_id != 101;
Output:
emp_name |
---|
Jane Smith |
Emily Davis |
Explanation:
The output includes names of employees with dept_id
values other than 101, excluding those with dept_id
equal to 101.
- Includes: Names of employees with dept_id values other than 101.
- Excludes: Employees with dept_id equal to 101.
Example 2: Using <> in a Join
This SQL query performs a join between the employees and departments tables using a condition that filters out rows where the dept_id values do not match. The join is based on e.dept_id <> d.dept_id, meaning it will include rows where the department IDs are different.
Query:
SELECT e.emp_name, d.dept_name
FROM employees e
JOIN departments d ON e.dept_id <> d.dept_id;
Output:
emp_name | dept_name |
---|
John Doe | Finance |
John Doe | IT |
Jane Smith | HR |
Jane Smith | IT |
Emily Davis | HR |
Emily Davis | Finance |
Emily Davis | IT |
Explanation:
- Includes: Pairs of employee names and department names where the dept_id values do not match between the two tables.
- Excludes: Pairs where the dept_id values are the same.
Example 3: Conditional Logic in PL/SQL Block
This SQL query uses a CASE statement to check if the name of the employee with emp_id = 2 is 'Jane Smith'. If it is not 'Jane Smith', the query returns 'Employee name is not Jane Smith'; otherwise, it returns 'Employee name is Jane Smith'.
Query:
SELECT
CASE
WHEN (SELECT emp_name FROM employees WHERE emp_id = 2) != 'Jane Smith'
THEN 'Employee name is not Jane Smith'
ELSE 'Employee name is Jane Smith'
END AS result;
Output:
result |
---|
Employees name is Jane Smith |
Explanation:
- If the employee's name is 'Jane Smith': Employee name is Jane Smith.
- If the employee's name is not 'Jane Smith': Employee name is not Jane Smith.
Example 4: Filtering Rows in a Subquery
This SQL query retrieves the names of employees who work in departments that are not named 'IT'. The inner query selects dept_id values from the departments table where the department name is not 'IT'.
The outer query then uses these department IDs to filter employees from the employees table.
Query:
SELECT emp_name
FROM employees
WHERE dept_id IN (SELECT dept_id FROM departments WHERE dept_name <> 'IT');
Output:
emp_name |
---|
John Doe |
Jane Smith |
Explanation:
- Includes: Names of employees whose department ID is associated with departments that do not have the name 'IT'.
- Excludes: Employees who work in the 'IT' department.
Conclusion
The NOT EQUAL operator (!=
or <>
) in PL/SQL is essential for filtering and comparing data where values are not equal. It is versatile for use in queries and conditional statements to manage data based on inequality.
Similar Reads
PL/SQL Tutorial Explore this PL/SQL tutorial to effortlessly learn PL/SQL â It is perfect for beginners and experienced ones. Whether you're new to it or diving deep, this interactive guide simplifies database programming.Learn hands-on with practical examples, making your journey fun and effective. Learn PL/SQL's
8 min read
PL/SQL Fundamentals
PL/SQL Control & Loops
Decision Making in PL/SQLPL/SQL (Procedural Language/Structured Query Language) is Oracle's extension to SQL that allows for procedural programming within databases. It features various conditional statements to control the flow of execution based on specific conditions.In this article, We will learn about the various PL/SQ
5 min read
PL/SQL LoopsPL/SQL stands for Procedural Language Extension to the Structured Query Language and it is designed specifically for Oracle databases it extends Structured Query Language (SQL) capabilities by allowing the creation of stored procedures, functions, and triggers. It is a block-structured language that
5 min read
PL/SQL For LoopPL/SQL stands for Procedural Language/ Structured Query Language. It has block structure programming features. With PL/SQL, you can fetch data from the table, add data to the table, make decisions, perform repetitive tasks, and handle errors.PL/SQL supports SQL queries. To fetch records, process dat
4 min read
PL/SQL While LoopOracle PL/SQL provides various loop structures that help developers execute a block of code multiple times based on certain conditions. The main loop structures include LOOP ... END LOOP, WHILE ... END LOOP, and FOR ... END LOOP. In this article, we will explore the WHILE loop in detail, including i
5 min read
PL/SQL Queries & Clauses
PL/SQL SELECT INTO Existing TablePL/SQL is a programming language that is used alongside SQL for writing procedural code such as stored procedures, functions, triggers, and packages within the Oracle Database. It was developed by Oracle Corporation and is widely used in database programming.PL/SQL is a programming language that has
5 min read
PL/SQL INSERT StatementThe PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets
3 min read
PL/SQL UPDATE StatementThe UPDATE statement in the PL/SQL(Procedural Language/ Structural Query Language) is the powerful SQL (Structured Query Language) command used to modify the existing data in the database table. In this article, we will explain the PL/SQL UPDATE Statement, its syntax, and examples in detail.PL/SQL U
6 min read
PL/SQL DELETE StatementIn PL/SQL(Procedural Language/Structured Query Language), the DELETE statement is the powerful command used to remove one or more records from the database table. It is an essential part of database management and enables the users to efficiently manage and maintain the data integrity by selectively
6 min read
PL/SQL WHERE ClauseThe WHERE clause in PL/SQL is essential for filtering records based on specified conditions. It is used in SELECT, UPDATE, and DELETE statements to limit the rows affected or retrieved, allowing precise control over data manipulation and retrieval.In this article, We will learn about the WHERE Claus
3 min read
PL/SQL ORDER BY ClauseIn PL/SQL, the ORDER BY clause is a vital tool that allows for the sorting of query results by one or more columns, either in ascending or descending order. In this article, We will learn about ORDER BY clause in PL/SQL, its syntax, functionality, and practical usage through examples.Understanding O
7 min read
PL/SQL GROUP BY ClauseThe GROUP BY clause in PL/SQL is a powerful tool used to organize data into aggregated groups based on one or more columns. It is essential for performing summary operations on large datasets, enabling efficient data analysis by grouping rows that share common values.In this article, We will learn a
7 min read
PL/SQL Operators
PLSQL : || OperatorThe string in PL/SQL is actually a sequence of characters with an optional size specification. The characters could be numeric, letters, blank, special characters or a combination of all. The || Operator in PLSQL is used to concatenate 2 or more strings together. The result of concatenating two char
2 min read
PL/SQL AND OperatorThe PL/SQL AND operator is used to combine multiple conditions in a WHERE clause of an SQL query. It allows you to refine your query by ensuring that all specified conditions are met. AND queries which help in filtering data more precisely and can be crucial for retrieving accurate results from a da
7 min read
PL/SQL LIKE OperatorThe PL/SQL LIKE operator is a powerful tool used in SQL queries to search for patterns in character data. It allows you to match strings based on specific patterns defined by wildcards. This operator is commonly used in SELECT, UPDATE, and DELETE statements to filter records based on partial or comp
6 min read
PL/SQL NOT OperatorPL/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 c
6 min read
PL/SQL IS NULL OperatorThe IS NULL operator is a fundamental tool in PL/SQL used to determine the presence of NULL values in database columns. Understanding how to effectively use the IS NULL operator is crucial for database management, as it allows developers and analysts to identify and handle records with missing or un
4 min read
PL/SQL CASE StatementPL/SQL stands for Procedural Language Extension to the Structured Query Language and it is designed specifically for Oracle databases it extends Structured Query Language (SQL) capabilities by allowing the creation of stored procedures, functions, and triggers. The PL/SQL CASE statement is a powerfu
4 min read
PL/SQL Program Units
PL/SQL Data Structures & Error Handling
Index in PL/SQLPL/SQL, Oracle's extension to SQL, combines SQL with procedural programming features like loops, conditionals, and exception handling. It enables developers to create stored procedures, functions, triggers, and other database applications. As a block-structured language, PL/SQL allows seamless integ
5 min read
Exception Handling in PL/SQLAn exception is an error which disrupts the normal flow of program instructions. PL/SQL provides us the exception block which raises the exception thus helping the programmer to find out the fault and resolve it. There are two types of exceptions defined in PL/SQL User defined exception. System defi
7 min read
PL/SQL RecordsPL/SQL stands for Procedural Language/Structured Query Language. It is an extension of the Structured Query Language (SQL). A core feature of PL/SQL is its ability to work with complex data types, including PL/SQL records. PL/SQL records enable developers to group related data elements, creating a s
10 min read
Cursors in PL/SQLA Cursor in PL/SQL is a pointer to a context area that stores the result set of a query. PL/SQL CursorsThe cursor is used to retrieve data one row at a time from the results set, unlike other SQL commands that operate on all rows at once. Cursors update table records in a singleton or row-by-row man
3 min read