Oracle Database

How to Compare Two Rows in Oracle?

Oracle database is a popular, convenient, and efficient relational database management system widely used by organizations to store their data in tables. While talking about tables, comparing two rows is a common task in Oracle. It is considered challenging especially when dealing with huge datasets. However, Oracle offers several methods to compare two rows, individual method has their strengths and weaknesses.

This post will discuss different methods that can be used to compare two rows in Oracle:

Method 1: Selecting Rows Based on Specific Values

To compare two rows in Oracle, the user can select the rows based on the specific value. For instance, compare the rows of a table named ā€œPRODUCTSā€ depending upon the specified values of the ā€œPRODUCT_IDā€ column:

SELECT * FROM PRODUCTS
WHERE PRODUCT_ID = 310 OR PRODUCT_ID = 311;

In the command above, only the rows will be returned where the ā€œPRODUCT_IDā€ column value is ā€œ310ā€ and ā€œ311ā€.

Output

The output depicted the result containing the rows based on specified values for easy comparison.

Method 2: Selecting Rows Depending on Multiple Conditions

The logical operators (ā€œORā€ and ā€œANDā€) can be utilized in the query to select the rows based on multiple conditions for comparison. Execute the command given below to extract a row based on multiple conditions:

SELECT * FROM PRODUCTS
WHERE (PRODUCT_ID = 310 OR PRODUCT_ID = 311)
  AND STANDARD_COST = LIST_PRICE;

The command above compares two rows from the table ā€œPRODUCTSā€ with ā€œPRODUCT_IDā€ equals ā€œ310ā€ or ā€œ311ā€ that have the same standard cost and list price.

Output

The output displayed the rows extracted based on multiple conditions.

Method 3: Selecting Rows Based on Inequality Operators

To compare two rows with not equal values, utilize the ā€œNOT EQUAL TOā€ (<>) operator in the WHERE clause. Run the command given below to compare the rows of a table named ā€œPRODUCTSā€:

SELECT * FROM PRODUCTS
WHERE (PRODUCT_ID = 310 OR PRODUCT_ID = 311)
  AND STANDARD_COST  LIST_PRICE;

The above command compares two rows that have ā€œPRODUCT_IDā€ equal to ā€œ310ā€ or ā€œ311ā€ and have distinct standard costs and list prices.

Output

The output returned the result after comparing rows using the ā€œNOT EQUAL TOā€ (<>) operator.

Method 4: Selecting Rows Based on a Comparison Operator

In Oracle, the comparison operators can be utilized to extract rows for comparison. For this post, letā€™s run the query given below to compare rows using comparison operators:

SELECT *
FROM PRODUCTS
WHERE PRODUCT_ID > 287

The above command selects the rows from the table ā€œPRODUCTSā€ where the ā€œPRODUCT_IDā€ values are greater than ā€œ287ā€.

Output

The output depicts the rows for comparison that are extracted using the comparison operator.

Method 5: Using the MINUS Operator

In Oracle, the MINUS operator can be utilized to compare two rows in Oracle. This operator compares the output of two ā€œSELECTā€ statements and returns the rows that are present in the first ā€œSELECTā€ statement:

SELECT *
FROM PRODUCTS
WHERE PRODUCT_ID > 287

MINUS

SELECT *
FROM PRODUCTS
WHERE PRODUCT_ID = 311;

The above command compares rows with the ā€œPRODUCT_IDā€ greater than ā€œ287ā€ but excludes rows where the ā€œPRODUCT_IDā€ equals ā€œ311ā€.

Output

The output displayed the result using the MINUS operator for the comparison of rows.

Method 6: Using the EXCEPT Operator

In Oracle, the ā€œEXCEPTā€ operator can also be utilized to compare two rows. This operator is like the MINUS operator, but it returns the non-redundant rows that are in present the first ā€œSELECTā€ statement but not in the second:

SELECT *
FROM PRODUCTS
WHERE PRODUCT_ID > 287

EXCEPT

SELECT *
FROM PRODUCTS
WHERE PRODUCT_ID = 311;

The above command returns results to compare rows with ā€œPRODUCT_IDā€ greater than ā€œ287ā€ but exclude rows with ā€œPRODUCT_IDā€ value equal to ā€œ311ā€ and remove duplicates.

Output

The output depicted the rows for comparison using the ā€œEXCEPTā€ operator.

Method 7: Using the JOIN Operator

The JOIN operator can be utilized to unify rows from two or more tables based on correlated columns. Letā€™s execute this command to use the JOIN operator on the ā€œPRODUCTSā€ table for row comparison:

SELECT *
FROM PRODUCTS t1
JOIN PRODUCTS t2
ON t1.STANDARD_COST = t2.LIST_PRICE
WHERE t1.PRODUCT_ID > 288
AND t2.CATEGORY_ID >= 4;

The above command selects all columns and joins them with itself utilizing the ā€œSTANDARD_COSTā€ and ā€œLIST_PRICEā€ columns. Moreover, it returns results by selecting only rows where the ā€œPRODUCT_IDā€ is greater than ā€œ288ā€ and the ā€œCATEGORY_IDā€ of ā€œt2ā€ is greater than or equal to ā€œ4ā€.

Output

The output depicted the result of row comparison using the JOIN operator.

Method 8: Using the IN and NOT IN Operator

In Oracle, the ā€œINā€ and ā€œNOT INā€ operators can be utilized for comparing rows by evaluating whether the values of multiple columns in one row match the values in another row or set of rows. To do so, run this command:

SELECT *
FROM PRODUCTS
WHERE (PRODUCT_ID, PRODUCT_NAME, CATEGORY_ID) IN (
  SELECT PRODUCT_ID, PRODUCT_NAME, CATEGORY_ID
  FROM PRODUCTS
  WHERE PRODUCT_ID > 280
)
AND (PRODUCT_ID, PRODUCT_NAME, CATEGORY_ID) NOT IN (
 SELECT PRODUCT_ID, PRODUCT_NAME, CATEGORY_ID
 FROM PRODUCTS
 WHERE PRODUCT_ID > 288
);

The above command selects all columns from the table named “PRODUCTS” where “PRODUCT_ID” is greater than ā€œ280ā€ for the combination of “PRODUCT_ID“, “PRODUCT_NAME“, and “CATEGORY_ID” columns. Additionally, it excludes rows where the ā€œPRODUCT_ID” is greater than ā€œ288ā€ for the same combination of columns.

Output

The output depicted the rows that fulfill the conditions in the query.

Conclusion

Oracle offers inequality, comparison, MINUS, EXCEPT, JOIN, IN, and NOT IN operators to compare two rows. Users effectively compare two rows in Oracle and extract the desired result from one or multiple tables based on specific values, multiple conditions, inequality, comparison, and logical operators. This post has demonstrated different methods to compare two rows in Oracle.

About the author

Nimrah Ch

Iā€™m a technical writer with a Bachelor's in Software Engineering and Certification in Cyber Security. Through my research and writing, I strive to provide readers with in-depth, informative articles that can help them make informed decisions about their technological needs.