w3resource
SQL exercises

SQL Subquery Exercises: Counts the customers with grades above New York's average

SQL SUBQUERY : Exercise-8 with Solution

8. Write a query to count the customers with grades above New York's average.

Sample table: Customer


 

Sample Solution:

SELECT grade, COUNT (DISTINCT customer_id)
FROM customer
GROUP BY grade
HAVING grade >
    (SELECT AVG(grade)
     FROM customer
     WHERE city = 'New York');

Sample Output:

grade	count
200		3
300		2

Practice Online


Inventory database model

Query Visualization:

Duration:

Query visualization of Counts the customers with grades above New York's average - Duration

Rows:

Query visualization of Counts the customers with grades above New York's average - Rows

Cost:

Query visualization of Counts the customers with grades above New York's average - Cost

Contribute your code and comments through Disqus.

Previous: Write a query to display all the customers whose id is 2001 bellow the salesman ID of Mc Lyon.
Next: Write a query to display all customers with orders on October 5, 2012.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming