This post will discuss different methods that can be used to compare two rows in Oracle:
- Selecting Rows Based on Specific Values
- Selecting Rows Depending on Multiple Conditions
- Selecting Rows Based on Inequality Operators
- Selecting Rows Based on a Comparison Operator
- Using the MINUS Operator
- Using the EXCEPT Operator
- Using the JOIN Operator
- Using the IN and NOT IN Operator
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:
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:
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ā:
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:
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:
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:
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:
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:
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.