What is SQL (11)

HAVING Clause

After learning about aggregate functions, we can select data that based on aggregate value. However, WHERE clause cannot be used with aggregate functions. We need to use HAVING clause to achieve this. Here is the Syntax:

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;

‘US_City’:

To select state that has more than 5,000 cities, we can write something like:

SELECT State, COUNT(Cities) AS Number_of_City
FROM US_City
GROUP BY State
HAVING COUNT(Cities) > 5000
ORDER BY State;

Comments

When we write down the queries to get all the data, we may want to add comments to help people understand. Comments will not be execute by system. Comments can also be used to prevent execution when testing SQL statements.

Syntax for single line comments:

-- Select All (This will not be executed, just a comment)
SELECT * FROM US_City; (This will be executed since it is not the same line with --)

Syntax for Multi-line comments:

/*This will not be executed.
This is just a comment.
Still a comment.
Final comment.*/
SELECT * FROM US_City; (This will be executed)
Show your support

Clapping shows how much you appreciated Rachel Fu’s story.