Difference Between Distinct and Group By in PL/SQL
Last Updated :
12 Mar, 2024
In PL/SQL, knowing the difference between DISTINCT and GROUP BY is important for working with data effectively. Although DISTINCT and GROUP BY might seem similar, they serve different purposes. In this article, we'll explore DISTINCT and GROUP BY Clause with the syntax and various examples along the difference between them for better understanding.
What is the DISTINCT Clause?
The DISTINCT clause is used to retrieve unique values from a specific column or combination of columns in a result set. It eliminates duplicate rows and ensures that only distinct values are returned. DISTINCT can improve query performance by reducing the amount of data that needs to be processed and returned. This can lead to faster query execution times, especially when dealing with large datasets.
Syntax:
SELECT DISTINCT column1, column2
FROM table_name;
Explanation: This query selects unique combinations of values from specified columns in the table.
What is GROUP BY Clause?
The GROUP BY clause is used in conjunction with aggregate functions like SUM, AVG, COUNT, MIN, and MAX to group the result set by one or more columns. It is used to divide the rows returned from the SELECT statement into groups based on the specified columns.
Syntax:
GROUP BY: SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;
Explanation: This query groups rows based on the values in column1 and applies the aggregate function to column2 for each group.
Examples of DISTINCT and GROUP BY Clause
To understand DISTINCT and GROUP BY Clause in PL/SQL we need a table on which we will perform various operations and queries. Here we will consider a table called employees which contains employee_id, employee_name, salary, age and department_name as Columns.
Query:
-- Create table with new fields
CREATE TABLE employees (
employee_id INT,
employee_name VARCHAR(50),
department_name VARCHAR(50),
salary DECIMAL(10, 2),
age INT
);
-- Insert values into the table
INSERT INTO employees (employee_id, employee_name, department_name, salary, age)
SELECT 1, 'John', 'Sales', 62000, 30 FROM DUAL
UNION ALL
SELECT 2, 'Alice', 'IT', 68000, 35 FROM DUAL
UNION ALL
SELECT 3, 'Bob', 'Sales', 65000, 28 FROM DUAL
UNION ALL
SELECT 4, 'Emma', 'HR', 48000, 40 FROM DUAL
UNION ALL
SELECT 5, 'Alice', 'Marketing', 62000, 32 FROM DUAL
UNION ALL
SELECT 6, 'Jane', 'IT', 65000, 29 FROM DUAL;
-- Show table
SELECT * FROM EMPLOYEES;
After Inserting some records into the employees, the table looks:
Employee tableExample of DISTINCT Clause
Example 1: Find Distinct Department Names
Suppose we have a table named employees with duplicate entries. To fetch unique department names, we use the DISTINCT clause.
Query:
SELECT DISTINCT department_name
FROM employees;
Output:

Explanation: This query retrieves unique department names from the employees table. Each department name appears only once in the output.
Example 2: Find Distinct employee Names
Query:
SELECT DISTINCT employee_name
FROM employees;
Output:

Explanation: This query retrieves unique employee names from the employees table. Each employee name appears only once in the output.
Example of GROUP BY Clause
Example 1: Calculating Sum of Salaries of employees According to Deparment
Calculating the sum of salaries of employees of each department using SUM() function.
Query:
SELECT department_name, SUM(salary) AS total_salary
FROM employees
GROUP BY department_name;
Output:

Explanation: This query calculates the total salary for each department by summing up the salaries of all employees within each department.
Example 2: Calculating Average Age of employees According to Department
Calculating the average age of employees of each department using AVG() function.
Query:
SELECT department_name, AVG(age) AS avg_age
FROM employees
GROUP BY department_name;
Output:

Explanation: This query calculates the average age of employees in each department by averaging the ages of all employees within each department.
Difference Between DISTINCT and GROUP BY
The Differnce between "DISTINCT" and "GROUP BY" is as follows:
DISTINCT
| GROUP BY
|
---|
Retrieve unique values from one or more columns in the result set.
| Group rows based on specified columns in the result set.
|
Eliminates duplicate rows from the result set, ensuring each value appears only once.
| Allows for the application of aggregate functions like SUM(), AVG(), COUNT(), etc., to calculate summary information for each group.
|
Operates on individual columns or combinations of columns specified in the SELECT statement.
| Rows with identical values in the specified columns are grouped together.
|
Has a simple syntax and is easy to use in SQL queries.
| GROUP BY can operate on multiple columns and allows for complex grouping criteria.
|
Results in a single column or multiple columns with unique values.
| Results in multiple groups, each with its own summary information based on the aggregated data.
|
Does not perform any calculations or aggregations on the data that focuses on uniqueness.
| Can be combined with conditional expressions or CASE statements for more complex aggregations.
|
Often used to filter out duplicate records when querying databases.
| Suitable for summarizing data based on certain criteria, such as department-wise sales totals or average test scores per student.
|
Generally faster for small result sets as it only needs to eliminate duplicates without any calculations.
| May be slower, especially with large data sets, due to the grouping and potential aggregations involved.
|
Ideal for obtaining a list of unique values from specific columns, such as unique product names or customer IDs.
| Essential for performing complex analyses, such as identifying trends or patterns within data sets.
|
Conclusion
Overall, understanding the differences between DISTINCT and GROUP BY clauses in PL/SQL is crucial for efficient data manipulation. DISTINCT is used to retrieve unique values from columns, eliminating duplicates and ensuring each value appears only once. On the other hand, GROUP BY is used to group rows based on specified columns, allowing for the application of aggregate functions to calculate summary information for each group. While DISTINCT is suitable for obtaining unique values, GROUP BY is essential for performing complex analyses and summarizing data based on certain criteria.
Similar Reads
Difference between EXISTS and IN in PL/SQL
PL/SQL is a procedural language designed to enable developers to combine the power of procedural language with Oracle SQL. Oracle develops and serves as one of the three key programming languages embedded in the Oracle database, alongside SQL and Java. PL/SQL includes procedural language elements su
7 min read
Difference Between PARTITION BY and GROUP BY in PL/SQL
In Oracle PL/SQL, effective data manipulation is crucial for achieving optimal performance in database operations. Two essential SQL clauses often used to organize data are PARTITION BY and GROUP BY. Although both are used for structuring and analyzing data, they function quite differently and are u
5 min read
Group By Vs Distinct Difference In SQL Server
Distinct is a relational database management system. SQL Server offers a wide range of features and tools that handle different needs, from small-scale applications to large-scale application solutions. GROUP BY has performance features, especially when dealing with large datasets and complex aggreg
5 min read
Difference Between Cube and Rollup in SQL Server
In SQL Server, both ROLLUP and CUBE are sub-clause of the GROUP BY clause and are used in conjunction with aggregate functions to produce summary reports. It helps to generate multiple group sets using the hierarchy. To enhance the capabilities of grouping and aggregation, SQL Server provides two po
3 min read
Difference Between Order By and Group By Clause in SQL
SQL provides powerful tools for organising and analysing data, and two commonly used clauses are ORDER BY and GROUP BY. The ORDER BY clause is used to sort data, while the GROUP BY clause is used to group rows with similar values, often combined with aggregate functions like SUM(), AVG(), or COUNT()
4 min read
Difference Between EXISTS and IN in PostgreSQL
PostgreSQL is one of the most advanced general-purpose object-relational database management systems and is open-source. Being an open-source software, its source code is available under the PostgreSQL license, a liberal open-source license. In this article, we will learn about the EXISTS and IN Con
6 min read
Difference Between Where and Group By
WHERE and GROUP BY clauses are essential tools for filtering and organizing data in SQL queries. While both are used to refine the output of a query, they serve distinct purposes. In this article, we will explore the differences between WHERE and GROUP BY clauses, including their syntax, use cases,
4 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to pe
3 min read
Difference between Where and Having Clause in SQL
In SQL, the WHERE and HAVING clauses are essential for filtering data and refining query results. While both serve the purpose of applying conditions, they are used at different stages of query execution and for distinct purposes. Understanding the differences between the WHERE and HAVING clauses is
4 min read
Difference Between Having Clause and Group by Clause
SQL is a powerful tool for data analysis, and mastering the nuances of the GROUP BY and HAVING clauses is essential for writing efficient queries. These clauses work together to group and filter data, enabling users to derive meaningful insights from datasets. In this article, we will explore the de
4 min read