Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

HAVING SQL operator

1 July 2020

HAVING SQL operator

HAVING SQL operator is used in combination with GROUP BY operator to limit groups of returned strings only to those whose condition is TRUE.

Syntax of the HAVING operator in SQL

SELECT expr1,2, ..._n,
 aggreg_function (aggreg_expression)
 FROM tabs
 [WHERE conds]
 GROUP BY1, 2, ..._n
 HAVING cond;

where:

  • expr1,2,… – Expressions that are not contained in the aggregate function should be included in the GROUP BY offer at the end of the SQL statement.
  • aggreg_function – This is an aggregate function such as SUM, COUNT, MIN, MAX or AVG.
  • aggreg_expression – This is the column or expression for which aggregate_function will be used.
  • tabs – The tables from which you want the records. The FROM sentence must contain at least one table.
  • WHERE conds – It’s optional. These are the conditions for selecting records.
  • HAVING cond – This is another condition that applies only to aggregate results to limit groups of returned strings. Only groups whose state is evaluated as true will be included in the result set.

HAVING operator – using SUM function

Let’s look at the SQL example HAVING, which uses the SQL function SUM.

You can also use SUM SQL function to return the name of department and total sales in a related department. The HAVING SQL proposal will filter the results so that only department with sales over $1000 will be returned.

SELECT depart, SUM(sales_id) AS "Total sales_id"
    FROM order_dets
  GROUP BY depart
HAVING SUM(sales_id) > 1000;

HAVING SQL – use COUNT function

Let’s see how we can use HAVING with the COUNT SQL function.

You can use the COUNT SQL function to return the name of a department and the number of employees in the corresponding department whose sales are over $25000 per year.

The HAVING SQL offer will filter the results, so only departments with more than 10 employees will be returned.

SELECT depart,
    COUNT(*) AS "Number of empls_id"
   FROM empls_id
 WHERE salary_id > 25000
 GROUP BY depart
HAVING COUNT(*) > 10;

HAVING SQL – use MIN function

Let’s now see how we could use HAVING with the MIN SQL function.

You can also use the SQL function MIN to return department names and MIN(salary_id). The HAVING SQL offer will only return departments where the MIN(salary_id) exceeds $55000.

SELECT department, MIN(salary_id) AS "Lowest salary_id"
   FROM empls
  GROUP BY depart
HAVING MIN(salary_id) > 55000;

HAVING SQL – use the MAX function

Finally, consider using HAVING with the SQL MAX function.

For example, you can use the SQL function MAX to return the department and the maximum salary in the department. The SQL proposal HAVING will return only those departments whose maximum salary is less than $35000.

SELECT depart,
    MAX(salary_id) AS "Highest salary_id"
   FROM empls
  GROUP BY depart
HAVING MAX(salary_id) < 50000;

HAVING clause and difference with GROUP BY & WHERE clause in SQL statement

Using HAVING clause and difference with GROUP BY & WHERE clause in SQL statement:

 
Tags: , , , , , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...