EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Oracle CARDINALITY

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » Oracle Tutorial » Oracle CARDINALITY

Oracle CARDINALITY

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

Oracle CARDINALITY Example 1

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:

Oracle CARDINALITY Example 1

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:

Oracle CARDINALITY Example 2

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:

Student Details Example 2

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.

Popular Course in this category
Sale
Oracle Training (14 Courses, 8+ Projects)14 Online Courses | 8 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,901 ratings)
Course Price

View Course

Related Courses
Oracle DBA Database Management System Training (2 Courses)All in One Financial Analyst Bundle - 250+ Courses, 40+ Projects

Query:

select student_id, student_name, CARDINALITY(subject)AS SUBJECTS_NUMBER
from my_student ORDER BY student_name ;

Output:

Student Details Example 2

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:

StudentCARDINALITY Example 3

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:

StudentCARDINALITY Example 3

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 –

  1. Introduction to Oracle Aliases 
  2. How Does Left Join Works in Oracle?
  3. What Are Oracle Database | Applications
  4. To 10 Oracle Database Interview Questions

Oracle Training (14 Courses, 8+ Projects)

14 Online Courses

8 Hands-on Projects

120+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - Oracle Training (14 Courses, 8+ Projects) Learn More