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

494 questions
2
votes
2 answers

Using 2 Aggregation in Having Clause

If I have table that contains data for all the teams from many leagues, how I can get the first team from each league? The first team in each league should be the team with most point, or if points are equal the team with best goal difference…
Liron C
  • 171
  • 1
  • 12
2
votes
2 answers

Using join and group with sum

I am fairly new to MySQL and have this theoretical problem given to me. I am given these…
2
votes
2 answers

Use HAVING or WHERE?

I am confused about when to use HAVING and when to use WHERE. I need to Find all of the bugs on software Debugger that pertain to the /main.html This is my query select Tickets.TicketID, b.Data from Bugs b Inner Join Tickets On b.TicketID =…
barcaman
  • 107
  • 11
2
votes
1 answer

MySQL: Join three tables, comparing two values

First of all, I'm an amateur on SQL. Here it is the example. From this three tables I would like to know who are the teachers that make more money than Mike Table1: LessonName TeacherID Maths 3 Biology 2 Biology …
Jusep
  • 187
  • 8
2
votes
1 answer

mySQL using where on newly created column

So, I think this is fairly simple, but I am messing something up. I have the query SELECT AVG(price), food_type FROM instructor GROUP BY food_type Which produces this: Price | food_type | 2.25 | Drink | 1.50 | Candy | 3 | Soup …
2
votes
1 answer

MySQL possible team vs. team matches where one player can be part of multiple teams

What I have in a database is players and teams. One player can join multiple teams as described in the table below: player_id | team_id 1 | 1 2 | 1 2 | 2 2 | 3 3 | 2 3 | 3 4 | 4 5 | 4 6…
MasterSmack
  • 363
  • 1
  • 9
2
votes
5 answers

SQL - replacement for HAVING COUNT(*) == 0

So I have to make a query to return all the reciept numbers that don't contain an "apple" item. The data is as follows. EG If you go to the shops and buy apples and bananas the data will be: (table reciepts) recieptNumber productCode 12345 …
user13292868
  • 87
  • 1
  • 7
2
votes
1 answer

How to compare values with the same ID but different other attribute?

+------+------+------+ | ID |letter|number| +------+------+------+ |1 |A | 23 | +------+------+------+ |1 |B | 45 | +------+------+------+ |2 |A | 37 | +------+------+------+ |2 |B | 24 …
Daniel Lin
  • 33
  • 4
2
votes
1 answer

SQL:Using group by to check existence of a value against a separate list of strings

I am trying to group by OrderID and lineID and return only the unique orderID and lineID from groups that include any values in a separate list of strings. Here is some sample data. OrderID lineID positionID fieldOfInterest somefield1…
Joseph Hailu
  • 357
  • 2
  • 8
2
votes
1 answer

The CREATE UNIQUE INDEX statement terminated because a duplicate key was found

This is unfinished database for selling train tickets. I want to create primary key for RouteId in Route table, but i got an exception: The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.Route'…
Roomey
  • 716
  • 2
  • 9
  • 16
2
votes
2 answers

Cannot use window function 'count' in having statement

I'm new to MYSQL, and I'm trying to validate the number of data which have the same name from 2 column that occurs more than one time, which I already try to use 'having' statement in this case and it throws me an error like this Error Code: 3593.…
greendino
  • 416
  • 3
  • 17
2
votes
3 answers

WINDOW function alternative in HAVING Clause

I'm trying to get the oldest processing date on each product like this: select prod_id, prod_name, prod_date, min(prod_date) over (partition by prod_id) as min_prod_date from dim_product where prod_name in ('xxx', 'yyy', 'zzz') having prod_date =…
Pramod
  • 1,411
  • 11
  • 35
  • 66
2
votes
1 answer

MySQL HAVING clause not working with ' ' nor " ", instead works without AS statement

SELECT department_id, ROUND(MIN(salary), 2) AS 'Min Salary' FROM employees GROUP BY department_id HAVING 'Min Salary' > 800; This doesn't seem to work, but instead this: SELECT department_id, ROUND(MIN(salary), 2) AS min_salary FROM …
Tom
  • 37
  • 8
2
votes
2 answers

How to using group by in strict mode correct?

I have the table for messages like this: CREATE TABLE `message` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `from_id` bigint(20) unsigned NOT NULL, `to_id` bigint(20) unsigned NOT NULL, `body` text COLLATE utf8mb4_unicode_ci…
2
votes
2 answers

Correctly join multiple many-to-many tables - MySQL query

a seemingly generic SQL query really left me clueless. Here's the case. I have 3 generic tables (simplified versions here): Movie id | title ----------------------- 1 | Evil Dead ----------------------- 2 | Bohemian Rhapsody .... Genre id |…
Denis
  • 322
  • 1
  • 4
  • 15