Filtering Group Results By Tiationg Kho |
Thursday, June 13, 2024
Filtering Group Results SELECT col1_name , SUM ( col2_name )
FROM table_name
GROUP BY col1_name
HAVING condition ;
-- the condition here will be involed the aggregate function
-- eg. HAVING SUM(revenue) > 1500
/* Write your T-SQL query statement below */
SELECT email AS Email
FROM Person
GROUP BY email
HAVING COUNT ( * ) > 1 ;
/* Write your T-SQL query statement below */
SELECT class
FROM Courses
GROUP BY class
HAVING COUNT ( * ) >= 5 ;
/* Write your T-SQL query statement below */
SELECT actor_id , director_id
FROM ActorDirector
GROUP BY actor_id , director_id
HAVING COUNT ( actor_id ) >= 3 ;
/* Write your T-SQL query statement below */
SELECT p . product_id , p . product_name
FROM Product p
INNER JOIN Sales s
ON p . product_id = s . product_id
GROUP BY p . product_id , p . product_name
HAVING MAX ( s . sale_date ) <= '2019-03-31' AND MIN ( s . sale_date ) >= '2019-01-01' ;
/* Write your T-SQL query statement below */
WITH cte AS (
SELECT u . account , SUM ( t . amount ) AS balance
FROM Users u
INNER JOIN Transactions t
ON u . account = t . account
GROUP BY u . account
HAVING SUM ( t . amount ) > 10000
)
SELECT u . name , cte . balance
FROM cte
INNER JOIN Users u
ON cte . account = u . account ;