oracle group by和having用法
2013-08-29 22:59
399 查看
这是我遇到的一道原题 Examine the description of the EMPLOYEES table: EMP_ID NUMBER(4) NOT NULL LAST_NAME VARCHAR2(30) NOT NULL FIRST_NAME VARCHAR2(30) DEPT_ID NUMBER(2) JOB_CAT VARCHAR2(30) SALARY NUMBER(8,2) Which statement shows the department ID, minimum salary, and maximum salary paid in that department, only if the minimum salary is less than 5000 and maximum salary is more than 15000? 查找到所有部门中,工资最大值大于15000和工资最小值小于5000的部门。
然后给的解答是:SELECT dept_id, MIN(salary), MAX(salary) FROM employees GROUP BY dept_id H***ING MIN(salary) < 5000 AND MAX(salary) > 15000;为什么这里只要group by dept_id就可以?能通俗的为我解释一下么,group by 和having到底是怎么回事?
MIN(), MAX()是聚合函数. group by 后面是要跟着的 select 中所有不是聚合函数的字段。 ex1: select count(*) from emp; //只是查询总总数 emp这张表里一共有多少条记录 所以不用group by ex2: select count(*) , deptno from emp group by deptno; // 根据deptno 分组, 查到的数据就是 列出 不同部门 记录总数 select count(*) , deptno , comm from emp group by deptno , comm; // 根据deptno 和 comm 分组 以此类推 group by 后面是要跟着的 select 中所有不是聚合函数的字段 否则会报错。 having 相当于where 与where的唯一区别是 当查询语句中有 聚合函数 的时候 就不能用where 了 只能用having
相关文章推荐
- Oracle Group By 用法之 —— Having
- Mysql group by以及having的用法
- sql group by 与 having的用法
- mysql group by having 用法理解
- oracle查询语句中select from where group by having order by的解释与应用
- Oracle group by 用法实例详解
- GROUP BY HAVING用法
- sql group by和having 的用法
- Oracle SQL Having Where Group by
- oracle查询语句中select from where group by having order by的解释与应用
- sql group by 与 having的用法
- oracle group by 的各种用法
- Oracle Group By 用法之 —— Rollup
- oracle查询语句中select from where group by having order by的解释与应用
- Oracle Group By 用法之 —— Rollup
- oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by
- Oracle Group By 用法之 —— Cube
- 初学oracle--groupby+where与having
- sql group by 与 having的用法 (转)
- sql分组查询 group by having(Oracle 10g宝典第六章读书笔记)