SQL-Aggregate Functions

  1. Write a SQL statement to find the total purchase amount of all orders.
    SELECT SUM(purch_amt)
    FROM orders;
    
  2. Write a SQL statement to find the average purchase amount of all orders.
    SELECT AVG(purch_amt)
    FROM orders;
    
  3. Write a SQL statement to find the number of salesmen currently listing for all of their customers.
    SELECT COUNT (DISTINCT salesman_id) 
    FROM orders;
    
  4. Write a SQL statement know how many customer have listed their names.
    SELECT COUNT (DISTINCT cust_name) 
    FROM consumer;
    
  5. Write a SQL statement find the number of customers who gets at least a gradation for his/her performance.
  • 标准答案在count里使用了all,没发现它的作用是什么
    SELECT COUNT (grade) 
    FROM customer; 
    ------ 标准答案
    SELECT COUNT (ALL grade) 
    FROM customer;
    
  1. Write a SQL statement to get the maximum purchase amount of all the orders.
    SELECT MAX(purch_amt)
    FROM customer;
    
  2. Write a SQL statement to get the minimum purchase amount of all the orders.
    SELECT MIN(purch_amt)
    FROM customer;
    
  3. Write a SQL statement which selects the highest grade for each of the cities of the customers.
    SELECT city, MAX(grade)
    FROM customer
    GROUP BY city;
    
  4. Write a SQL statement to find the highest purchase amount ordered by the each customer with their ID and highest purchase amount.
    SELECT customer_id, MAX(purch_amt)
    FROM orders
    GROUP BY customer_id
    ORDER BY customer_id, MAX(purch_amt);
    
  5. Write a SQL statement to find the highest purchase amount ordered by the each customer on a particular date with their ID, order date and highest purchase amount.
    SELECT customer_id,ord_date,MAX(purch_amt) 
    FROM orders 
    GROUP BY customer_id,ord_date;
    
  6. Write a SQL statement to find the highest purchase amount on a date ‘2012-08-17’ for each salesman with their ID.
    SELECT customer_id, MAX(purch_amt)
    FROM orders
    WHERE ord_date='2012-08-17'
    GROUP BY salesman_id;
    
  7. 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.
    • 使用having,在group by之后进行条件筛选,
    SELECT customer_id, ord_date, MAX(purch_amt)
    FROM orders
    GROUP BY customer_id, ord_date
    HAVING MAX(purch_amt)>2000;
    
  8. 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.
    SELECT customer_id, ord_date, MAX(purch_amt)
    FROM orders
    GROUP BY customer_id, ord_date
    HAVING MAX(purch_amt) BETWEEN 2000 AND 6000;
    
  9. 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.
    SELECT customer_id, ord_date, MAX(purch_amt)
    FROM orders
    GROUP BY customer_id, ord_date
    HAVING MAX(purch_amt) IN (2000, 3000, 5760, 6000);
    
  10. Write a SQL statement to find the highest purchase amount with their ID, for only those customers whose ID is within the range 3002 and 3007.
  • 使用having进行筛选在此处和where效果一致,原因如下:
    having子句是在分组后对组进行筛选,where子句是在分组前对记录进行筛选。
    对于本题来说,分组前后都有customer_id字段,因此where和having都可以成功进行筛选过滤。

  • 此处应该使用where,原因如下:

    SELECT customer_id, MAX(purch_amt)
    FROM orders
    GROUP BY customer_id
    HAVING customer_id BETWEEN 3002 AND 3007;
    ------ 标准答案
    SELECT customer_id,MAX(purch_amt) 
    FROM orders 
    WHERE customer_id BETWEEN 3002 and 3007 
    GROUP BY customer_id;
    
  1. Write a SQL statement to display customer details (ID and purchase amount) whose IDs are within the range 3002 and 3007 and highest purchase amount is more than 1000.
    无效:只可以使用where
    SELECT customer_id, purch_amt
    FROM orders
    GROUP BY customer_id
    HAVING
    ((salesman_id BETWEEN 3002 AND 3007)
    AND MAX(purch_amt)>1000);
    ------ 标准答案
    SELECT customer_id, purch_amt
    FROM orders
    WHERE salesman_id BETWEEN 3002 AND 3007
    GROUP BY customer_id
    HAVING MAX(purch_amt)>1000;
    
  2. Write a SQL statement to find the highest purchase amount with their ID, for only those salesmen whose ID is within the range 5003 and 5008.
    无效:只可以使用having
    SELECT customer_id, MAX(purch_amt)
    FROM orders
    WHERE salesman_id BETWEEN 5003 AND 5008
    GROUP BY customer_id;
    ------ 标准答案
    SELECT salesman_id,MAX(purch_amt) 
    FROM orders 
    GROUP BY salesman_id 
    HAVING salesman_id BETWEEN 5003 AND 5008;
    
  3. Write a SQL statement that counts all orders for a date August 17th, 2012.
    SELECT COUNT(*)
    FROM orders
    WHERE ord_date='2012-08-17';
    
  4. Write a SQL statement that count the number of salesmen for whom a city is specified. Note that there may be spaces or no spaces in the city column if no city is specified.
    SELECT COUNT(*) 
    FROM salesman 
    WHERE city IS NOT NULL;
    
  5. Write a query that counts the number of salesmen with their order date and ID registering orders for each day.
    SELECT ord_date, customer_id, COUNT(*)
    FROM orders
    GROUP BY ord_date, customer_id;
    
  6. Write a SQL query to calculate the average price of all the products.
    SELECT AVG(PRO_PRICE)
    FROM item_mast;
    
  7. Write a SQL query to find the number of products with a price more than or equal to Rs.350.
    SELECT COUNT(*)
    FROM item_mast
    WHERE PRO_PRICE>=350;
    
  8. Write a SQL query to display the average price of each company’s products, along with their code.
    SELECT PRO_COM, AVG(PRO_PRICE)
    FROM item_mast
    GROUP BY PRO_COM;
    
  9. Write a query in SQL to find the sum of the allotment amount of all departments.
    SELECT SUM(DPT_ALLOTMENT)
    FROM emp_department;
    
  10. Write a query in SQL to find the number of employees in each department along with the department code.
    SELECT EMP_DEPT, COUNT(*)
    FROM emp_details
    GROUP BY EMP_DEPT;
    

来源:w3resource

你可能感兴趣的:(SQL-Aggregate Functions)