WHERE vs HAVING and GROUP BY vs PARTITION BY Clause in SQL
In SQL the WHERE and HAVING clause is the most common keyword. The purpose for both is somewhat similar. They are very confusing if you consider the different databases like MS SQL, MySQL, and Oracle. That is the reason, they are most common in the interview. In this article, we will walk through the most common difference between them along with the GROUP BY vs PARTITION BY clause.
Please visit our YouTube Channel for more SQL Interview Questions and Answers videos by the below link:
Today's Interview Questions
Introduction
WHERE Clause and HAVING Clause in SQL
- The WHERE clause is applied first to the individual rows in the result set of query and it returns only the matching rows with the WHERE filter criteria.
- After that, the groups are created that meet the conditions in the WHERE clause.
- After that, the HAVING clause is applied to filter the groups from the result set. Here only the groups that meet the HAVING conditions appear in the final query output.
- At last, the ORDER BY clause will be executed if there is any.
HAVING Clause vs GRPUP BY Clause
DISTINCT Clause vs GRPUP BY Clause
GROUP BY Clause vs PARTITION BY Clause
Similarities Between WHERE Clause and HAVING Clause
- Both can be used with the SELECT clause.
- Both are optional clauses with the SELECT statement.
- Both can be used to filter the records.
- Both precede the ORDER BY clause.
- Both can be used with the GROUP BY Clause.
- Both can be used with the PARTITION BY Clause.
- Both can be used as isolated or together to achieve the desired result.
- Both can be used to UPDATE and DELETE the records in some databases like Oracle.
Difference Between WHERE Clause and HAVING Clause
- A WHERE clause is used to filter the records from the table based on the specified condition while the HAVING clause is used to filter records from the groups based on the specified condition.
- A WHERE clause can be used with SELECT, UPDATE, DELETE, INSERT statement, but HAVING Clause can only be used with SELECT statement in MS SQL.
- A WHERE clause cannot contain an aggregate function but a HAVING clause can contain an aggregate function. i.e.
- WHERE Clause is used only with single row function like UPPER, LOWER while HAVING Clause is used with multiple row function like SUM, COUNT.
- A WHERE clause can be used without GROUP BY Clause but generally HAVING Clause requires GROUP BY Clause.
- WHERE Clause is used before GROUP BY Clause, HAVING Clause is used after GROUP BY Clause.
- WHERE Clause implements in row operations while HAVING Clause implements in column operation.
- You can apply a HAVING clause only to columns that either appear in the GROUP BY clause or in an aggregate function. On the other hand, this is not mandatory for the WHERE clause.
Live Demo
Conclusion
SQL Scripts
-- Create Table
CREATE TABLE Employee
(
ID INT IDENTITY PRIMARY KEY,
Name VARCHAR(50) NULL,
RoleName VARCHAR(10) NULL,
Salary NUMERIC(18,2) NULL
);
-- Insert sample data into table
INSERT INTO Employee
VALUES ( 'Alex', 'SuperAdmin', 90000 ),
( 'Thomas', 'Admin', 80000 ),
( 'Peter', 'SuperAdmin', 95000 ),
( 'Sid', 'SuperAdmin', 70000 ),
( 'Maria', 'Admin', 60000 ),
( 'Andrew', 'Manager', 50000 ),
( 'Fedric', 'SuperAdmin', 45000 ),
( 'Anton', 'Supervisor', 93000 ),
( 'Mandy', 'Manager', 30000 );
-- Employee Table Data
SELECT * FROM Employee;
-- WHERE vs HAVING Clause
SELECT RoleName, Name FROM Employee
WHERE RoleName = 'SuperAdmin';
SELECT RoleName, Name FROM Employee
GROUP BY Name, RoleName
HAVING RoleName = 'SuperAdmin';
-- HAVING Clause with WHERE Clause
SELECT RoleName, Salary FROM Employee
WHERE Salary > 45000
GROUP BY RoleName, Salary
HAVING RoleName = 'SuperAdmin';
-- Interchanging Filter Criteria in WHERE vs HAVING Clause
SELECT RoleName, Salary FROM Employee
WHERE RoleName = 'SuperAdmin'
GROUP BY RoleName, Salary
HAVING Salary > 45000;
-- Using Aggregate Functions without HAVING Clause
SELECT
AVG(Salary) AS AvgSalary,
SUM(Salary) AS TotalSalary,
Min(Salary) AS MinSalary,
MAX(Salary) AS MaxSalary,
COUNT(*) AS TotalCount
FROM Employee
WHERE RoleName = 'SuperAdmin';
-- HAVING Clause without GROUP BY Clause
SELECT
AVG(Salary) AS AvgSalary,
SUM(Salary) AS TotalSalary,
Min(Salary) AS MinSalary,
MAX(Salary) AS MaxSalary,
COUNT(*) AS TotalCount
FROM Employee
HAVING COUNT(*) > 1;
-- PARTITION BY Clause for Aggregate Function with WHERE Clause
SELECT
Id,
RoleName,
Name AS EmployeeName,
Salary,
MIN(Salary) OVER(PARTITION BY RoleName) AS MinSalary,
MAX(Salary) OVER(PARTITION BY RoleName) AS MaxSalary,
SUM(Salary) OVER(PARTITION BY RoleName) AS TotalSalary,
COUNT(ID) OVER(PARTITION BY RoleName) AS TotalCount
FROM Employee
WHERE RoleName != 'Supervisor'
ORDER BY RoleName;
Recommended Articles
Most Frequently Asked SQL Interview Questions
Most Frequently Asked C# Interview Questions
Foreign Key Interview Questions
Unique Key Interview Questions
Primary Key Interview Questions
Derived Table (DT) Interview Questions
Unicode vs Non-Unicode Data Types Interview Questions
Table Variable (TV) Interview Questions
Table-Valued Type (TVT) Interview Questions
Common Table Expression(CTE) Interview Questions
Temp Table Interview Questions
Static Class Interview Questions and Answers
Sealed Class Interview Questions and Answers
Sealed Method Interview Questions and Answers
Abstract Class vs Interface
Interface Interview Questions and Answers
Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Telegram, WhatsApp, Youtube, and Quora for regular updates.
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.