Questions tagged [having]

About the HAVING keyword in SQL.

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

Reference

Related tags

1156 questions
7
votes
5 answers

select persons who like apple and banana both

How to select people like apple and banana both from the data below? table: MyTable persons | fruit ----------------------------- P1 Apple P1 Banana P1 Mango P2 Banana P2 Apple P3 Mango P3…
Fahad Khan
  • 1,635
  • 4
  • 23
  • 37
7
votes
8 answers

Using the MIN function in the having clause

I want to get the name of the employee who has the minimum salary. Is there a way to do this using only one query? I have given my query below, it doesn't work because the having clause requires a condition. Is there any way to give a condition in…
Sindu_
  • 1,347
  • 8
  • 27
  • 67
7
votes
1 answer

Implementing group_by and having in Laravel using Eloquent

I am having trouble implementing the group_by and having queries using Eloquent in Laravel. Here is the scenario: orders - id - qty deliveries - id - qty - order_id I want to use a join to display the orders with incomplete deliveries as…
howellmartinez
  • 1,195
  • 1
  • 13
  • 24
6
votes
3 answers

MongoDB group by Functionalities

In MySQL select a,b,count(1) as cnt from list group by a, b having cnt > 2; I have to execute the group by function using having condition in mongodb. But i am getting following error. Please share your input. In MongoDB > res =…
Kumaran
  • 219
  • 5
  • 17
6
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…
KarthikS
  • 883
  • 1
  • 11
  • 17
6
votes
5 answers

MySQL Group By and HAVING

I'm a MySQL query noobie so I'm sure this is a question with an obvious answer. But, I was looking at these two queries. Will they return different result sets? I understand that the sorting process would commence differently, but I believe they…
kimmothy
  • 63
  • 1
  • 4
6
votes
4 answers

Difference between WHERE and HAVING in SQL

Possible Duplicate: SQL: What's the difference between HAVING and WHERE? I have seen various discussions on WHERE and HAVING. I still have a question: is HAVING used only when considering aggregates, or can it be used in more general terms:…
CuriousKen
  • 69
  • 1
  • 1
  • 2
6
votes
2 answers

WHERE vs. HAVING performance with GROUP BY

So I was assigned to estimate the performance of two queries and came to a surprising result. I was told beforehand that HAVING is slower than WHERE because it only filters results after accessing rows. This seems reasonable, and this question on…
Felix
  • 2,548
  • 19
  • 48
6
votes
3 answers

SQL aggregate function alias

I'm a beginner at SQL and this is the question I have been asked to solve: Say that a big city is defined as a place of type city with a population of at least 100,000. Write an SQL query that returns the scheme…
Paul Benn
  • 83
  • 1
  • 4
6
votes
2 answers

SQL Query to get common records

I have a table as below ID Username GroupID 1 venkat 2 2 venkat 3 3 ramu 1 4 ramu 2 Using the sql statement I want to retrieve all username's that are available in both the groupids 2,3 In this case only Venkat is the…
Tarak
  • 221
  • 3
  • 16
6
votes
5 answers

Difference between "HAVING ... GROUP BY" and "GROUP BY ... HAVING"

I have got the table MYTABLE with 2 columns: A and B I have got the following pieces of the code: SELECT MYTABLE.A FROM MYTABLE HAVING SUM(MYTABLE.B) > 100 GROUP BY MYTABLE.A and SELECT MYTABLE.A FROM MYTABLE GROUP BY MYTABLE.A …
user3006279
  • 115
  • 2
  • 8
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
  • 1
  • 6
6
votes
2 answers

Select all rows containing duplicate values in one of two columns from within distinct groups of related records

I'm trying to create a MySQL query that will return all individual rows (not grouped) containing duplicate values from within a group of related records. By 'groups of related records' I mean those with the same account number (per the sample…
purefusion
  • 943
  • 1
  • 15
  • 23
6
votes
1 answer

Sql to Linq difficulty - group by, having

I have the following query in SQL which I would like to convert to LINQ: select profile_id from t where child_id in (1, 2 ,3, ...) //this will be a list of integers CHILDREN group by profile_id having count(distinct child_id) = 3 I am having a…
test
  • 2,538
  • 4
  • 35
  • 52
5
votes
1 answer

sql - find average salary for each department with more than five members

Not quite sure how to get this one. I have a staff table and I need to find the average salary. I know I can use use avg(). But the trick is I need to find the average for departments that have more than 5 staff members. I'm not sure if I should use…
user1225281
  • 51
  • 1
  • 1
  • 3
1 2
3
76 77