DISTINCT Keyword in PL/SQL
The DISTINCT keyword is a keyword which is used to fetch unique or distinct records from a table.
Syntax:
SELECT DISTINCT expression1, expression2, ... expression_n
FROM table
[WHERE conditions]
Explanation:
- expression1, expression2, … expression_n: These are expressions that will be used to de-duplicate records
- table: The table from which to perform distinct operation.
- conditions: The condition on which to filter the table optionally.
Example:
The following query retrieves all the distinct values in the val1 column:
SELECT DISTINCT(val1) FROM sample;
Output:
Explanation: We get the output according to the above query.
How to Count Distinct Values in PL/SQL?
PL/SQL is a procedural language designed to allow users to combine the power of procedural language with Oracle SQL. PL/SQL includes procedural language elements such as conditions and loops and can handle exceptions (run-time errors). It also allows the declaration of constants and variables, procedures, functions, packages, types and variables of those types, and triggers.
In this article, we are going to see how we can count distinct values in PL/SQL.