Skip to main content

Command Palette

Search for a command to run...

๐Ÿš€ Using HAVING Clause in Oracle SQL (Filtering Grouped Data)

Updated
โ€ข2 min read
๐Ÿš€ Using HAVING Clause in Oracle SQL (Filtering Grouped Data)

As part of my ongoing learning with Oracle Autonomous Database, I explored the HAVING clause in SQL, which is used to filter grouped data. This is especially useful when working with aggregate functions.

๐ŸŽฏ Objective

The goal of this activity was to:

Understand how to filter grouped results Use HAVING with aggregate functions Perform advanced SQL queries

๐Ÿ’ป Step 1: Creating the Table

CREATE TABLE employees (

emp_id NUMBER,

emp_name VARCHAR2(50),

department VARCHAR2(50),

salary NUMBER );

๐Ÿงช Step 2: Inserting Data

INSERT INTO employees VALUES (1, 'Ali', 'IT', 50000);

INSERT INTO employees VALUES (2, 'Ahmed', 'HR', 70000);

INSERT INTO employees VALUES (3, 'Sara', 'IT', 60000);

INSERT INTO employees VALUES (4, 'Zara', 'HR', 65000);

INSERT INTO employees VALUES (5, 'Usman', 'IT', 55000);

๐Ÿ“Š Step 3: Grouping Data

SELECT department, COUNT( ) AS total_employees

FROM employees GROUP BY department;

๐Ÿ” Step 4: Filtering Groups using HAVING

SELECT department, COUNT() AS total_employees

FROM employees GROUP BY department HAVING COUNT(*) > 2;

This query filters only those departments that have more than 2 employees.

๐Ÿ’ก What I Learned Difference between WHERE and HAVING

Filtering grouped data using HAVING Writing more advanced SQL queries

๐Ÿš€ Conclusion

This activity helped me understand how to filter grouped data effectively using the HAVING clause. It is a powerful feature when working with aggregated results in Oracle SQL.

2 views

More from this blog

๏ฟฝ

๐Ÿš€ Getting Started with Oracle Autonomous Database (Free Tier) โ€“ My First Hands-on Experience

15 posts