show me a sample sql using the 'having' clause on ...
Tạo vào: 14 tháng 1, 2026
Trả lời bằng GPT-5.2 Thinking bởi Chat01
Tạo vào: 14 tháng 1, 2026
Trả lời bằng GPT-5.2 Thinking bởi Chat01
show me a sample sql using the 'having' clause on ibm's 'sample' db tables
Here’s a Db2 “SAMPLE” example that uses HAVING to filter aggregated results (total sales per salesperson), using the SALES and STAFF sample tables. (IBM)
sql-- Salespeople whose total unit sales in 2006 are >= 25 -- (Qualify the tables with your schema if needed, e.g. DB2INST1.SALES / DB2INST1.STAFF) SELECT st.id, st.name, st.dept, st.salary, SUM(sa.sales) AS total_units_sold_2006 FROM staff st JOIN sales sa ON UPPER(sa.sales_person) = UPPER(st.name) -- SALES_PERSON is last name in SAMPLE WHERE sa.sales_date >= DATE('2006-01-01') AND sa.sales_date < DATE('2007-01-01') GROUP BY st.id, st.name, st.dept, st.salary HAVING SUM(sa.sales) >= 25 ORDER BY total_units_sold_2006 DESC;
WHERE limits rows before grouping (here: only 2006 rows), while HAVING limits the groups after SUM(...) is computed.