Example 12 — Averaging with GROUP BY
Examples — click to view question
Example 1
A slow full toss
Find the names of employees in the Marketing department.
Example 2
Skinning a cat
Find the items sold by a department on the second floor.
Example 3
Another full toss
Find the names of items sold on floors other than the second floor.
Example 4
Subtracting from all
For items that have been sold, find the items sold by no department on the second floor.
Example 5
Dividing
Find the items sold by all departments on the second floor.
Example 6
At least some number
Find the items sold by at least two departments on the second floor.
Example 7
A friendly IN for an SQL traveler
Find the name and salary of Clare's manager.
Example 8
Joining a table with itself
Find numbers and names of employees who make more than their manager.
Example 9
A combination of subtract from all and a self-join
Find the departments where all the employees earn less than their manager.
Example 10
Self-join with GROUP BY
Report the number of direct employees of each manager. Report the boss's name.
Example 11
A self-join with two matching conditions
Find the names of employees who are in the same department as their manager. Report the employee, department, and manager name.
Example 12
Averaging with GROUP BY
List the departments having an average salary over $25,000.
Example 13
Inner query GROUP BY and HAVING
List the departments where the average salary of the employees of each manager is more than $25,000. Exclude the manager's salary.
⚠ Claude does not generate correct SQL for this prompt.
Example 14
An IN with GROUP BY and COUNT
List the name and salary of the managers with more than two employees.
Example 15
A self-join with some conditions
List the name, salary, and manager of the employees of the Marketing department who have a salary over $25,000.
Example 16
Making comparisons
List the names of the employees who earn more than any employee in the Marketing department.
Example 17
An IN with GROUP BY and SUM
Among all the departments with total salary greater than $25,000, find the departments that sell Sextants.
Example 18
A double divide!
List the items delivered by every supplier that delivers all items of type N.
⚠ Claude does not generate correct SQL for this prompt.
Example 19
A slam dunk
Find the names of suppliers that deliver compasses.
Example 20
A 6-inch putt for a birdie
Find the suppliers that do not deliver compasses.
Example 21
Making the count
Find the names of suppliers that deliver both compasses and an item other than compasses.
Example 22
Minus and divide
List the departments that have not recorded a sale for all the items of type N.
Example 23
Division with copies
For departments that sold items, list those departments that have at least one sale of all the items delivered to them.
⚠ Claude does not generate correct SQL for this prompt.
Example 24
A difficult pairing
List the supplier-department pairs where the department sells all items delivered to it by the supplier.
Example 25
Two divides and an intersection
For departments that have sold items, list those items delivered to all departments by all suppliers.
⚠ Claude does not generate correct SQL for this prompt.
Example 26
A divide with a matching condition
List the items sold only by departments that sell all the items delivered to them.
Example 27
Restricted divide
Who are the suppliers that deliver all the items of type N?
Example 28
A NOT IN variation on divide
List the names of suppliers that deliver only the items sold by the Books department.
Example 29
All and only
List the names of suppliers that deliver all and only the items sold by the Equipment department.
Example 30
Divide with an extra condition
List the names of suppliers that deliver every item of type C to the same department on the second floor.
Example 31
At least some COUNT
List the names of suppliers that deliver at least two items of type N to departments.
Example 32
Double divide with a restriction
List the names of suppliers that deliver all the items of type B to departments on the second floor who sell all the items of type R.
Example 33
Triple divide with an intersection
List the names of suppliers that deliver all the items of type B to the departments that also sell all the items of type N.
Example 34
An easy COUNT
List the items delivered by exactly one supplier.
Example 35
The only one
List the distinct names of suppliers and items where the supplier is the only deliverer of that item.
Example 36
At least some number
List the suppliers that deliver at least 10 items.
Example 37
A three-table join
For each item, give its type, the departments that sell it, and the floor locations of those departments.
Example 38
Using NOT IN like NOT EXISTS
List the departments for which each item delivered to the department is also delivered to some other department.
⚠ Claude does not generate correct SQL for this prompt.
Example 39
Minus after GROUP BY
List each item delivered to at least two departments by each supplier that delivers it.
⚠ Claude does not generate correct SQL for this prompt.
Example 40
Something to all
List the items that are delivered only by the suppliers that deliver something to all the departments.
⚠ Claude does not generate correct SQL for this prompt.
Example 41
Intersection (AND)
List the items delivered by Nepalese Corp. and sold in the Navigation department.
Example 42
Union (OR)
List the items delivered by Nepalese Corp. or sold in the Navigation department.
Example 43
Intersection/union
List the departments selling type E items that are delivered by Nepalese Corp. and/or sold by the Navigation department.
Example 44
Averaging with a condition
Find the average salary of the employees in the Clothes department.
Example 45
Averaging with grouping
Find, for each department, the average salary of the employees.
Example 46
Average with a join, condition, and grouping
Find, for each department on the second floor, the average salary of the employees.
Example 47
Averaging with multiple joins
Find, for each department that sells items of type E, the average salary of the employees.
Example 48
Complex counting
What are the number of different items delivered by each supplier that delivers to all departments that have sold any item?
⚠ Claude does not generate correct SQL for this prompt.
Example 49
Summing with joins and conditions
Find the total number of items of type E sold by the departments on the second floor.
Example 50
Summing with joins, conditions, and grouping
Find, for each item, the total quantity sold by the departments on the second floor.
Example 51
Advanced summing
List suppliers that deliver a total quantity of items of types C and N altogether greater than 100.
Example 52
Comparing to the average with a join
List the employees in the Accounting department and the difference between their salaries and the average salary of the department.
Example 53
Comparing to the average with a product
List the employees in the Accounting department and the difference between their salaries and the average salary of all employees.
Example 54
Averaging with multiple grouping
What is, for each supplier, the average number of all items per department that the supplier delivers? Report the supplier's and department's name and average delivery.
Example 55
More than the average with grouping
For each department, find the average salary of the employees who earn more than the average salary of the department.
Example 56
The simplest average
Give the overall average of the salaries in all departments.
Example 57
Difference from the average
List each employee's salary, the average salary within their department, and the difference between the employee's salary and the department average.
Example 58
Averaging with multiple joins, multiple grouping, and a condition
What is the average delivery quantity of items of type N delivered by each company? List the supplier's and item's name.
Example 59
Detailed averaging
List the name of the supplier and department where the average delivery quantity of items of type N is given per supplier per department.
Example 60
Counting pairs
What is the number of supplier-department pairs in which the supplier delivers at least one item of type E to the department?
Example 61
Boolean results
Is it true that all the departments that sell items of type C are located on the third floor?
Playbook Schema
qdept
deptnamedeptfloor 12 rows
qemp
empnoempfnameempsalarydeptnamebossno 13 rows
qitem
itemnameitemtypeitemcolor 14 rows
qspl
splnosplname 6 rows
qsale
deptnameitemnamesaleqty 26 rows
qdel
splnoitemnamedeptnamedelqty 46 rows
Example 12 — Averaging with GROUP BY
List the departments having an average salary over $25,000.
Write your SQL above and click Run Query.