Questions tagged [having-clause]

About the HAVING keyword in SQL.

(Should really be: )
The HAVING clause is the equivalent of WHERE after applying aggregate functions.

Reference

Related tags

489 questions
6
votes
2 answers

How does the HAVING clause really work?

We able to use HAVING clause in SQL-query to filtrate groups of row. When we use GROUP BY clause it work directly in this way. But, let's look to this query: select 1 where 1!=1 having count(*)=0; (or append it with 'from dual' for Oracle). If…
potapuff
  • 1,393
  • 3
  • 15
  • 32
6
votes
2 answers

MySQL SELECT multiple rows based on specific field value and number of rows

I have three tables: author (columns: aut_id, aut_name) book (columns: book_id, book_title) authorbook (linking table, columns: aut_id, book_id) Each author can be associated with one or more books. Each book can be associated with one or more…
user1894374
  • 233
  • 3
  • 18
6
votes
1 answer

difference between where and having with respect to aliases

If I create an alias in the select clause then I cannot use it in the where clause because according to the order of execution of sql queries where comes before select. But I can create an alias in the select clause and use it in a having clause…
pooja
  • 73
  • 6
5
votes
1 answer

LINQ TO SQL GROUP BY HAVING generated only as subquery

according to all samples, the SQL conversion to LINQ for HAVING clause as in this example: SELECT NAME FROM TABLES GROUP BY NAME HAVING COUNT(*) > 1 is: (vb.net) from t in tables group t by t.NAME into g = Group where g.count > 1 select g BUT…
holy
  • 51
  • 3
5
votes
4 answers

What's the difference between HAVING and WHERE in MySQL Query?

I have a view (viewX) based on joins of some tables: When I use WHERE, the query is delayed, processor usage goes to 50% and finally I need to close mysqld.exe service and restart to try to solve the problem again. When I use HAVING, the query…
CRISHK Corporation
  • 2,840
  • 5
  • 35
  • 52
5
votes
1 answer

HAVING clause without GROUP BY in Oracle database using developer desktop and developer web

My understanding as per standard practice is that HAVING is to be used along with GROUP BY for filtering conditions, while WHERE is supposed to be used for general row-wise filtering conditions. However, there are online discussions with mixed…
5
votes
1 answer

LINQ with Group By and Having Clause with Min(string)

There are examples with group by - having count or select minimum date with linq on the web but I couldn't find a particular solution for my question. And also I don't have an advanced linq understanding to combine these solutions that I found so…
Baz Guvenkaya
  • 1,362
  • 3
  • 16
  • 25
5
votes
5 answers

Oracle SQL - is there a standard HAVING EVERY workaround?

I am having trouble figuring out a way around Oracle's lack of support for the HAVING EVERY clause. I have two tables, Production and Movie, with the following schema: Production (pid, mid) Movie(mid, director) where 'pid' is in integer…
Dan
  • 2,112
  • 3
  • 17
  • 25
5
votes
3 answers

How do you construct the predicate for NSFetchRequest setHavingPredicate:?

For the life of me I can not seem to get this to work. Assume our entity is an managed object with a status field and an order field. How would I go about getting all orderedEntries having more than one order that are the same? Please no answers…
thewormsterror
  • 1,568
  • 13
  • 26
5
votes
1 answer

Multiple conditions in HAVING clause with NHibernate Criteria?

I'm trying to use NHibernate's Criteria API to write the equivalent of this: select foo_id from foo_history group by foo_id having sum(bar_in) > 0 or sum(baz_in) > 0; with this mapping:
wes
  • 1,557
  • 1
  • 14
  • 30
4
votes
2 answers

Difference between HAVING and WHERE clause in SQL

SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value What is the difference between having and where
user909058
  • 188
  • 1
  • 1
  • 11
4
votes
0 answers

How do I write a GROUP BY or HAVING clause in Diesel?

I'm trying to convert the following SQL query into corresponding Rust Diesel code: SELECT COUNT(*) FROM BookStore WHERE BookName IN ('Lord of the Rings', 'Hobbit') GROUP BY StoreId HAVING COUNT(DISTINCT BookName) = 2 I was able to translate it thus…
user1842633
  • 247
  • 1
  • 2
  • 12
4
votes
2 answers

SQL Select Count(person_id) > 3 From

Can someone convert this english to SQL I have tried several things but no luck. SELECT CASE WHEN COUNT(person_id) > 3 THEN person_id end FROM table I am trying to only get the person_id(s) that occur > 3 times in the table.
Daniel
  • 4,267
  • 2
  • 16
  • 9
4
votes
1 answer

Can I use HAVING instead of WHERE in SQL queries?

I always thought that I could not, but MSDN says otherwise. When GROUP BY is not used, HAVING behaves like a WHERE clause. I had checked and got the error: Msg 8121: Column '...' is invalid in the HAVING clause because it is not contained in…
Mark Shevchenko
  • 7,297
  • 1
  • 21
  • 25
3
votes
1 answer

doctrine: HAVING clause in DQL query

I am working on a simple forum in symfony2 and have the following DQL query to get the last posts in each forum: SELECT p, f.id FROM MyBundle:Post p JOIN p.forum f GROUP BY p.forum HAVING p.created_at >= MAX(p.created_at) Without the HAVING clause…
maiwald
  • 851
  • 12
  • 26
1
2
3
32 33