---
title: "oracle 统计表中数据信息"
author: "lpm528"
date: "2012-06-15T11:53:26Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2012-06-18T09:41:11Z"
id: 1561521
url: "https://lpm528.iteye.com/blog/1561521"
---

```
select e.deptno as 部门编号,
       d.dname as 部门名称,
       count(*) as 部门人数,
       sum(case 
          when e.sal>=2000 then 1 else 0 
          end) as 工资大于等于2000人数,
       sum(case
          when e.sal<2000 then 1 else 0
          end) as 工资小于2000人数
from emp e,dept d
where e.deptno=d.deptno
group by e.deptno,d.dname
```

==============================================================

```
姓名        体征       数值 
--------- ---------- -------- 
美男1       身高       1.60 
美男1       体重       60 
美男1       胸围       79 
美男2       身高       1.69 
美男2       体重       55 
美男2       胸围       77 

转换为如下结果：

姓名    身高     体重      胸围 
--------- ---------- -------- 
美男1   1.60     60        79 
美男2   1.69     55        77 

SQL语句：

SELECT 姓名，
CASE WHEN 体征=""身高"" THEN MAX（数值） END 身高，
CASE WHEN 体征=""体重"" THEN MAX（数值） END 体重，
CASE WHEN 体征=""胸围"" THEN MAX（数值） END 胸围 
FROM 
美男 GROUP BY 姓名，体征

```

 

==============================================================

查询哪个部门没有员工：

```
select d.deptno
from emp e,dept d
where d.deptno=e.deptno(+) and e.deptno is null

select d.deptno
from dept d  left join emp e
on d.deptno=e.deptno 
where e.deptno is null
```

===================================================================  

 学生表student(学号，姓名，性别，年龄),课程表course(编号，课程名称),选课表sc(学号，课程编号，成绩)

    查询选修了5门课程的学生学号和姓名

```
select s.id,s.name
from student s
where (select count(*) from sc where sc.id=s.id)=5
```

 

 ====================================================================

查询公司工资最高的前三位：

```
select * from (select * from emp order by sal desc) where rownum<=3
```