sales with columns book_title (text) and copies_sold (integer).sales table.book_title and sums copies_sold.HAVING clause to filter groups where total copies sold is greater than 100.Jump into concepts and practice - no test required
sales with columns book_title (text) and copies_sold (integer).sales table.book_title and sums copies_sold.HAVING clause to filter groups where total copies sold is greater than 100.sales table and insert datasales with columns book_title as TEXT and copies_sold as INTEGER. Then insert these exact rows: ('The Alchemist', 50), ('The Alchemist', 60), ('1984', 80), ('1984', 30), ('Brave New World', 90), ('Brave New World', 20).Use CREATE TABLE to define the table and INSERT INTO with multiple rows to add the data.
sales_threshold and set it to 100 to use as the minimum total copies sold for filtering.SQL does not always support variables in all environments; you can just note the threshold value 100 for use in the next step.
book_title and the sum of copies_sold as total_copies from the sales table, grouping by book_title.Use GROUP BY book_title and SUM(copies_sold) to get total copies sold per book.
HAVING clause to the previous query to show only books where SUM(copies_sold) is greater than 100.Use HAVING SUM(copies_sold) > 100 after the GROUP BY clause to filter groups.
What is the main purpose of the HAVING clause in SQL?
GROUP BYGROUP BY clause groups rows based on column values.HAVINGHAVING filters these groups using aggregate functions like SUM or COUNT.GROUP BY based on aggregate conditions -> Option CHAVING filters groups, not rows [OK]Which of the following is the correct syntax to filter groups with HAVING?
SELECT department, COUNT(*) FROM employees GROUP BY department _______ COUNT(*) > 5;
GROUP BY, filtering groups requires HAVING, not WHERE.HAVING COUNT(*) > 5 filters groups with more than 5 employees.Given the table sales with columns region and amount, what will this query return?
SELECT region, SUM(amount) FROM sales GROUP BY region HAVING SUM(amount) > 1000;
region and calculates total amount per region.HAVING clause keeps only regions where the sum is greater than 1000.Identify the error in this query:
SELECT category, COUNT(*) FROM products HAVING COUNT(*) > 10 GROUP BY category;
GROUP BY first, then HAVING.HAVING before GROUP BY, causing syntax error.You have a students table with columns class and score. You want to find classes where the average score is at least 75 and the number of students is more than 10. Which query achieves this?
GROUP BY class to group rows by class.HAVING AVG(score) >= 75 AND COUNT(*) > 10 to keep classes meeting both conditions.