w3resource
SQL exercises

SQL Exercises: Find higher purchase amount of customers within a specific range

SQL Aggregate Functions: Exercise-13 with Solution

Write a SQL statement to find the highest purchase amount with their ID and order date, for those customers who have a higher purchase amount in a day is within the range 2000 and 6000.

Sample table: orders


Sample Solution:

SELECT customer_id,ord_date,MAX(purch_amt) 
FROM orders 
GROUP BY customer_id,ord_date 
HAVING MAX(purch_amt) BETWEEN 2000 AND 6000;

Output of the Query:

customer_id	ord_date	max
3007		2012-07-27	2400.60
3002		2012-09-10	5760.00
3009		2012-10-10	2480.40
3002		2012-04-25	3045.60

Explanation:

Syntax of find higher purchase amount of customers within a specific range

Pictorial presentation:

Find higher purchase amount of customers within a specific range

Query Visualization:

Duration:

Query visualization of Find higher purchase amount of customers within a specific range - Duration

Rows:

Query visualization of Find higher purchase amount of customers within a specific range - Rows

Cost:

Query visualization of Find higher purchase amount of customers within a specific range - Cost

 

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a SQL statement to find the highest purchase amount with their ID and order date, for only those customers who have highest purchase amount in a day is more than 2000.
Next: Write a SQL statement to find the highest purchase amount with their ID and order date, for only those customers who have a higher purchase amount in a day is within the list 2000, 3000, 5760 and 6000.

What is the difficulty level of this exercise?



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