The SQL Server SELECT statement in 60 seconds – the HAVING Clause #shorts



The SQL Server SELECT statement in 60 seconds – the HAVING Clause #shorts

The SQL Server SELECT statement in 60 seconds - the HAVING Clause #shorts

In 60 seconds we’ll explore the HAVING clause in SQL.
My SQL Server Udemy courses are:
70-461, 70-761 Querying Microsoft SQL Server with T-SQL: https://rebrand.ly/querying-microsoft-sql-server
98-364: Database Fundamentals (Microsoft SQL Server): https://rebrand.ly/database-fundamentals
70-462 SQL Server Database Administration (DBA): https://rebrand.ly/sql-server-dba
Microsoft SQL Server Reporting Services (SSRS): https://rebrand.ly/sql-server-ssrs
SQL Server Integration Services (SSIS): https://rebrand.ly/sql-server-ssis
SQL Server Analysis Services (SSAS): https://rebrand.ly/sql-server-ssas-mdx
Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): https://rebrand.ly/microsoft-powerpivot-ssas-tabular-dax
—-
In this short video, I’m going to show you how to use the SQL HAVING clause. I’ll explain what it is and how to use it, in just 60 seconds!

If you’re new to SQL, then this video is for you! I’ll show you how to use the SQL HAVING clause, which is a very important part of the SQL language. By the end of this video, you’ll be able to use the HAVING clause to filter your data in a smart way!

The HAVING clause is the fifth clause in the SELECT statement in SQL, after the SELECT, FROM, WHERE, and GROUP BY.

The optional WHERE clause looks at individual rows retrieved in the FROM clause, and creates a reduced dataset. You should use it when looking at individual rows.

SELECT schema_id, type, COUNT(*) AS NumberOfObjects
FROM sys.objects
WHERE type like ‘%P%’ — this works, as it looks at individual rows
GROUP BY schema_id, type

It does not work when using aggregations.

SELECT schema_id, type, COUNT(*) AS NumberOfObjects
FROM sys.objects
WHERE COUNT(*)=10 — does not work, as it uses an aggregation
GROUP BY schema_id, type

The HAVING clause looks at the response after the GROUP BY clause. You should use it based on aggregations, such as SUM, COUNT, AVG, MIN and MAX, such as those shown in the SELECT clause.

SELECT schema_id, type, COUNT(*) AS NumberOfObjects
FROM sys.objects
GROUP BY schema_id, type
HAVING COUNT(*)=10 — this works, using an aggregation

However, you may be able to use other aggregations as well.

SELECT schema_id, type, COUNT(*) AS NumberOfObjects
FROM sys.objects
GROUP BY schema_id, type
HAVING SUM(schema_id)=10

The HAVING clause is the least used clause in the SELECT statement ā€“ use it to focus based on the results after the GROUP BY clause.

Iā€™m Phillip Burton from IDoData.com, where we have our courses on SQL and more. Please see our other videos and Keep learning!

—-
Links to my website are:
70-461, 70-761 Querying Microsoft SQL Server with T-SQL: http://idodata.com/querying-microsoft-sql-server-with-t-sql/
98-364: Database Fundamentals (Microsoft SQL Server): http://idodata.com/database-fundamentals-it-specialist-its-201-certification/
SQL Server Essential in an Hour: http://idodata.com/sql-server-essentials-in-an-hour-the-select-statement/
70-462 SQL Server Database Administration (DBA): http://idodata.com/sql-server-database-administration-dba/
DP-300: Administering Relational Databases: http://idodata.com/dp-300-administering-relational-databases/
Microsoft SQL Server Reporting Services (SSRS): http://idodata.com/microsoft-sql-server-reporting-services-ssrs/
SQL Server Integration Services (SSIS): http://idodata.com/sql-server-integration-services-ssis-an-introduction/
SQL Server Analysis Services (SSAS): http://idodata.com/sql-server-ssas-multidimensional-mdx-an-introduction/
Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): https://rebrand.ly/microsoft-powerpivot-ssas-tabular-dax
1Z0-071 Oracle SQL Developer ā€“ certified associate: http://idodata.com/iz0-071-oracle-sql-developer-certified-associate/
SQL for Microsoft Access: http://idodata.com/sql-for-microsoft-access/
DP-900: Microsoft Azure Data Fundamentals: http://idodata.com/dp-900-microsoft-azure-data-fundamentals/