Updated February 28, 2023
Introduction to Oracle CARDINALITY Function
In general Cardinality is a mathematical term and can be defined as the number of elements in a group or a set as a property of that particular set, in case of oracle we can define it as a measure or calculation of a number of distinct elements present in a column or a nested table and the cardinality function returns the data type NUMBER and in case there nested table or column is empty then we will get the return output from this function as NULL.
Syntax:
CARDINALTY(nested_table)
Parameters:
nested_table: It refers to the column in the nested table for which we want the cardinality.
How CARDINALITY Functions Works in Oracle?
In the above section, we learned about the definition of cardinality in oracle. Let us now look into how it works in oracle. Cardinality function is only applied on the nested table column and not on any other column. So it should not be confused with distinct function as unlike distinct function it is not applied on any other column which is not a column of a nested table. When we execute the function with a SELECT statement the function first checks whether the column is a nested column or not and then it measures the number of elements present. The important point to note is that while measuring the elements inside each row of a nested column it does not check whether the elements are duplicate or not. The result set extracted by this function returns the data type number. It contains the numeric number equal to the number of elements in each row of the nested table column and in case there is no element than it returns NULL in the result set.
Examples of CARDINALITY Function in Oracle
Let us now look at the below examples to understand better the COMMIT statement.
1. Finding CARDINALITY of Column
As we have discussed that we can find the cardinality of a nested column using the CARDINALITY function. So, in this example, we will find the cardinality of the nested column subject which is present in table my_student. This table consists of both non nested column as well as a nested column. This table consists of three columns. Let us look at the table data once before we apply the function.
Query:
SELECT * FROM my_student;
Output:
As we can see in the above data that the third column is a nested table column and now we are going to measure the number of elements is present in each row of the subject column using the CARDINALITY function. Let us look at the query for the same.
Query:
select student_id, CARDINALITY(subject)
from my_student;
Output:
As we see in the above screenshot the query returns the student id and the measure of elements in each row of the subject column. One important point to see in the screenshot is that though the subjects mentioned for a student with id 103 has duplicate subjects since the total number of subject is three. Hence we have the cardinality as three.
2. Finding CARDINALITY of a Column with an order by Clause
In this scenario, we are going to use the CARDINALITY function to find the names of the students along with their subjects in descending alphabetical order. We will use the ORDER BY clause for the same. Let us first take a look at the table my_student.
Query:
SELECT * FROM my_student;
Output:
As we can see in the table it has three students and we need to arrange the student names in descending order along with the number of subjects (cardinality). Let us create a SELECT statement for the same.
Query:
select student_id, student_name, CARDINALITY(subject) AS SUBJECTS_NUMBER
from my_student ORDER BY student_name DESC;
In the above query, the keyword DESC is used for sorting the employee names in descending order.
Output:
As we can see in the above screenshot the output returns the name arranged in descending order along with the number of subjects for each name using the cardinality function.
Another case can be of using the ORDER BY without using the DESC keyword. In that case, by default, the sorting order will be ascending order. Let us look at the query without using DESC with ORDER BY.
Query:
select student_id, student_name, CARDINALITY(subject)AS SUBJECTS_NUMBER
from my_student ORDER BY student_name ;
Output:
As we can see in the above screenshot the output returns the name arranged in ascending order along with the number of subjects for each name using the cardinality function.
3. Finding CARDINALITY of a Column with Where Condition
In this scenario, we are going to restrict the result set of the SELECT statement using the WHERE condition. For example, we want to find the number of subjects of students whose native city is ‘Agra’. In this case, we cannot simply use the SELECT statement alone, we need to use a WHERE condition. Let us look at the data present in the my_student table.
Query:
SELECT * FROM my_student;
Output:
Let us now create a query where we get the cardinality of subjects only for students from Agra city.
Query:
select student_id, student_name, cardinality(SUBJECT) AS SUBJECTS_NUMBER, city
from my_student where city ='Agra';
As we can see in the above query we have given the city as ‘Agra’ in where condition.
Output:
As we can see in the above screenshot the output returns the student name, student id, number of subjects of the students who are from the city of Agra.
Conclusion
In this article, we discussed the definition of the CARDINALITY function in Oracle. We also discussed the syntax of the function and also the working of the function. Later on, we went through different examples to understand the implementation of the function better.
Recommended Articles
This is a guide to Oracle CARDINALITY. Here we discuss the definition of the Oracle CARDINALITY and its syntax. We also discussed how the CARDINALITY Function works along with parameters. You can also go through our suggested articles to learn more –