【正文】
no desc。 默認是升序 分組查詢 Group by 對表中的數據進行分組 例子 1:計算每個部門的平均工資 首先將現有數據按照部門進行分組,然后再計算每個組員工的平均薪水。 Select deptno, avg(sal) from emp group by deptno。 例子 2:計算每個部門的最大工資 Select deptno, max(sal) from emp group by deptno。 分組查詢 例子 3:按照部門,和職位 的最大薪水進行分組 Select deptno, job, max(sal) from emp group by deptno, job。 使用 group by 的規(guī)律:出現在 select列表中的字段,如果沒有出現在組函數中,則必須出現在 group by子句中 典型錯誤: select ename, deptno, max(sal) from emp group by deptno。 分組查詢 使用 Having對分組進行限制 如果我們要從分組數據中把某些特定的剔除去的時候,使用 Having關鍵字 例子 1:將平均薪水大于 1000的組的平均薪水從 emp這張表中選出來 Select avg(sal), deptno group by deptno having avg(sal) 1000。 分組查詢 例子 2:求薪水大于 1200的雇員,按照部門進行分組,而且這些分分組后組內平均薪水必須大于 1500,要查詢分組的平均工資 Select avg(sal) from emp where sal 1200 group by deptno having avg(sal) 1500 Order by avg(sal)。 總結 簡單 select語句 where 條件判斷 In語句 AND, OR, NOT 使用 模糊查詢 ORDER BY查詢 分組查詢 HAVING限制 作業(yè) 選出所用部門里工資最高的人的名字 作業(yè) Select ename, sal from emp where sal = ( Select max(sal) from emp )。 演講完畢,謝謝觀看!