1. 查询的顺序
select top ...
from A
join B
on ...
join C
on ...
where ...... (where 是对原始数据的筛选)
group by ...
having ...... (having 是对分组后的数据筛选)
order by ...
2. 数据库参考代码
select "T".*, "D".dname, "S".GRADE
from dept "D"
join(
select top 2 "E".deptno, AVG(sal) "avg_sal"
from emp "E"
join dept "D"
on "E".deptno = "D".deptno
join SALGRADE "S"
on "E".sal between "S".LOSAL and "S".HISAL
where "E".sal > 1500
group by "E".deptno
having AVG("E".sal) >2000
order by AVG("E".sal) desc
) "T"
on "D".deptno = "T".deptno
join SALGRADE "S"
on "T".avg_sal between "S".LOSAL and "S".HISAL
3. 输出每个员工的姓名 工资 上司的姓名
select "E1".ename, "E2".sal, "E2".ename "上司姓名"
from emp "E1"
join emp "E2"
on "E1".mgr = "E2".empno
union
select ename, sal, '已是最大老板' from emp where mgr is null