The HAVING clause is used like search condition for a group or an aggregate function used in an select statement.
SQL Query
SELECT c.course_id, c.name, c.description, c.price,
SUM(c.price) AS amount, COUNT(c.course_id) AS quantity
FROM course c, orders o
WHERE o.course_id = c.course_id
GROUP BY c.course_id, c.name, c.description, c.price;
Output
COURSE_ID |
NAME |
DESCRIPTION |
PRICE |
AMOUNT |
QUANTITY |
1 |
SQL 1 |
SQL course for beginners |
10 |
20 |
2 |
4 |
PHP |
PHP course |
75 |
75 |
1 |
5 |
CSS |
Learn CSS |
20 |
40 |
2 |
Having example
SELECT c.course_id, c.name, c.description, c.price,
SUM(c.price) AS amount, COUNT(c.course_id) AS quantity
FROM course c, orders o
WHERE o.course_id = c.course_id
GROUP BY c.course_id, c.name, c.description, c.price
HAVING COUNT(c.course_id) > 1
AND SUM(c.price) > 20;
Output
COURSE_ID |
NAME |
DESCRIPTION |
PRICE |
AMOUNT |
QUANTITY |
5 |
CSS |
Learn CSS |
20 |
40 |
2 |