sql 子查詢

sql 子查詢
2011年07月01日
   利用oracle中自帶的幾個表。
  求每個部門的部門名稱,員工數,員工平均工資,以及每個部門工資最低的員工姓名。sql如下:
  select dd.dname, tt.deptno, tt.empnum, tt.avgsal, e.ename from scott.dept dd, (select e.deptno deptno, count(e.empno) empnum, avg(e.sal) avgsal, min(sal) minsal from scott.emp e group by e.deptno) tt, scott.emp e where tt.deptno = dd.deptno and e.sal = tt.minsal
  表的結構如下:
  員工表:
  create table SCOTT.EMP ( EMPNO NUMBER(4) not null, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2), COMM NUMBER(7,2), DEPTNO NUMBER(2) ) -- Create/Recreate primary, unique and foreign key constraints alter table SCOTT.EMP add constraint PK_EMP primary key (EMPNO) ; alter table SCOTT.EMP add constraint FK_DEPTNO foreign key (DEPTNO) references SCOTT.DEPT (DEPTNO);
  部門表:
  -- Create table create table SCOTT.DEPT ( DEPTNO NUMBER(2) not null, DNAME VARCHAR2(14), LOC VARCHAR2(13) ) -- Create/Recreate primary, unique and foreign key constraints alter table SCOTT.DEPT add constraint PK_DEPT primary key (DEPTNO);
  
  
發佈了22 篇原創文章 · 獲贊 0 · 訪問量 2538
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章