1. dual 确实是一张表.是一张只有一个字段,一行记录的表. 2.习惯上,我们称之为'伪表'.因为他不存储主题数据. 3. 他的存在,是为了操作上的方便.因为select 都是要有特定对象的.
- 与 查询与 141 号或 174 号员工的 manager_id 和 和 department_id 相同的
的 其他员工的 employee_id, manager_id, department_id
[ 方式一]
select employee_id,manager_id,department_id
from employees
where manager_id in
(select manager_id
from employees
where employee_id in (174,141))
and
department_id in (
select department_id
from employees
where employee_id in (174,141))
and
employee_id not in(174,141);
[ 方式二]
select employee_id,manager_id,department_id
from employees
where(manager_id,department_id)
in
(
select manager_id,department_id
from employees
where employee_id in (141,174))
and
employee_id not in (174,141);
的 返回比本部门平均工资高的员工的 last_name, department_id,
salary
SELECT a.last_name, a.salary,
a.department_id, b.salavg
FROM employees a,
(SELECT department_id,
AVG(salary) salavg
FROM employees
GROUP BY department_id) b
WHERE a.department_id = b.department_id
AND a.salary > b.salavg;
- 的 显式员工的 employee_id,last_name 和 和 location 。其中,若员工
department_id 与 与 location_id 为 为 1800 的 的 department_id 相同,则
location 为’Canada’, 其余则为’USA’ 。
SELECT employee_id, last_name,
(CASE department_id
WHEN (SELECT department_id FROM departments
WHERE location_id = 1800)
THEN 'Canada' ELSE 'USA' END) location
FROM employees;
employees 表中 employee_id 与 job_history 表中 employee_id
于 相同的数目不小于 2 ,输出这些相同 id 的员工的 employee_id,last_name
其 和其 job_id
select e.employee_id ,last_name,e.job_id
from employees e
where 2<=(select count(*)
from job_history
where employee_id = e.employee_id
);
insert into demo_score select * from (
select to_date('2008-8-8','yyyy-MM-dd'),'拜仁','胜' from dual
union
select to_date('2008-8-9','yyyy-MM-dd'),'奇才','胜' from dual
union
select to_date('2008-8-9','yyyy-MM-dd'),'湖人','胜'from dual
union
select to_date('2008-8-10','yyyy-MM-dd'),'拜仁','负' from dual
union
select to_date('2008-8-8','yyyy-MM-dd'),'拜仁','胜' from dual
union
select to_date('2008-8-12','yyyy-MM-dd'),'奇才','胜' from dual
)
select Name, SUM(胜)as "胜",SUM(负) as "负" from
(
select Name ,(case score when '胜' then 1 else 0 end)as "胜",(case score when '负' then 1 else 0 end)as "负"
from demo_score )
group by Name