Oracle上機練習題(一)
試卷總分:100
答題時間:120分鐘
一、Oracle上機練習題
1.
查詢100號部門的所有員工信息[2分]
SELECT * FROM employees WHERE department_id = 100
2.
查詢所有崗位編號爲“SA_MAN"的員工的員工號、員工名和部門號。[2分]
SELECT employee_id,first_name,last_name,department_id FROM employees WHERE job_id = 'SA_MAN'
3.
查詢每個員工的員工號、工資、獎金以及工資與獎金的和。[2分]
SELECT employee_id,salary,commission_pct,salary*(1+nvl(commission_pct,0) FROM employees
4.
查詢40號部門中職位編號爲“ad_asst”和20號部門中職位編號爲“sa_rep”的員工信息。[2分]
SELECT * FROM employees WHERE department_id=40 AND job_id='AD_ASST' OR department_id=20 AND job_id='SA_REP'
5.
查詢職位名稱不爲“stockmanager”和“purchasing manager”且工資大於等於2000元的員工詳細信息[2分]
SELECT * FROM employees WHERE job_id not in('stockmanager','purchasing') AND salary >= 2000
6.
查詢有獎金的員工不同的職位編號和名稱。[2分]
SELECT distinct job_id,job_title FROM jobs WHERE job_id in(SELECT job_id FROM employees WHERE job_id is not null
7.
查詢沒有獎金或獎金低於100元的員工信息。[2分]
SELECT * from employees WHERE salary*commission_pct<100 OR commission is NULL
8.
查詢員工名(first_name)中不包含“s”的員工。[2分]
SELECT first_name FROM employees WHERE first_name LIKE '%S%'
9.
查詢員工的姓名和入職日期,並按入職日期從先到後進行排序。[2分]
SELECT first_name,last_name,hire_date FROM employees ORDER BY hire_date
10.
顯示所有員工的姓名、職位、工資和獎金,按職位降序排序,若職位相同則按工資升序排序。[2分]
SELECT first_name,last_name,job_id,salary,salary*commission_pet FROM employees ORDER BY job_id DESC,salary ASC
11.
查詢所有員工的姓名及其直接上級的姓名。[2分]
SELECT a.first_name,b.first_name FROM employees a join employees b on b.employee_id = a.manage_id
12.
查詢入職日期早於其直接上級領導的所有員工信息[2分]
SELECT * FROM employees a WHERE hire_date<(SELECT hire_date FROM employees b b.employees_id=a.manage_id)
13.
查詢各個部門部門號,部門名稱,部門所在地及部門的領導姓名。[2分]
SELECT d.department_id,d.department_name,d.location,e.first_name FROM departments d JOIN employees e on d.manager_id=e.employee_id
14.
查詢所有部門及其員工信息,包括哪些沒有員工的部門[2分]
SELECT department_name,first_name FROM departments d LEFT JOIN employees e on d.department_id=e.department_id
15.
查詢所有員工及其部門信息,包括那些還不屬於任何部門的員工。[2分]
SELECT e.first_name,d.department_name FROM employees LEFT JOIN departments on e.department_id=d.department_id
16.
查詢所有員工的員工號,員工名,部門名稱,職位名稱,工資和獎金[2分]
SELECT e.employee_id,e.first_name,d.department_name,j.job_title,e.salary,e.salary*e.commission_pct FROM departments d JOIN employees e ON d.department_id=e.department_id JOIN jobs j on j.job_id=e.job_id
17.
查詢至少有一個員工的部門信息[2分]
SELECT DISTINCT departments.* FROM departments d join employees e on e.employee_id is not NULL
18.
查詢工資比100號員工工資高的所有員工信息[2分]
SELECT * FROM employees WHERE salary>(SELECT salary FROM employees where employee_id=100)
19.
查詢工資高於公司平均工資的所有員工信息[2分]
SELECT * FROM employees WHERE salary>(SELECT avg(salary) FROM employees)
20.
查詢各個部門中不同職位的最高工資[2分]
SELECT job_id,max(salary) FROM employees GROUP BY job_id
21.
查詢各個部門的人數及平均工資[2分]
SELECT department_id,count(*),avg(salary) FROM employees GROUP BY department_id
22.
統計各個職位的員工數與平均工資[2分]
SELECT job_id,count(employee_id),avg(salary) FROM employees GROUP BY job_id
23.
統計每個部門中各職位的人數與平均工資[2分]
SELECT department_id,job_id,count(*),avg(salary) FROM employees GROUP BY department_id,job_id
24.
查詢最低工資大於5000元的各種工種[2分]
SELECT job_,job_title FROM jobs WHERE job_id in(SELECT job_id FROM employees GROUP BY job_id having min(salary)>5000)
25.
查詢平均工資低於6000元的部門及其員工信息[2分]
SELECT e.*,d.* FROM employees e JOIN departments d ON e.department_id=d.department_id AND department_id IN(SELECT department_id FROM employees GROUP BY employee_id having avg(salary)<6000)
26.
查詢在“Sales”部門工作的員工姓名和部門號[2分]
select * from employee where department_id in(salary department_id from departments where department_name='Sales'
27.
查詢與140號員工從事相同工作的所有員工信息[2分]
select * from employees where job_id in(select job_id from employees where employee_id=140)
28.
查詢員工工資高於30號部門中所有員工的工資的員工姓名和工資以及部門號[2分]
select first_name,last_name,salary from employees where salary>(select max(salary) from employees group by department_id having department_id=30)
29.
查詢每個部門中部門號,員工數量,平均工資和平均工作年限。[2分]
select count(*),avg(salary),avg(round((sysdate-hire_date)/365)) from employees group by department_id
30.
查詢工資爲某個部門平均工資的員工的信息[2分]
select * from employees where salary in(select avg(salary) from employees group by department_id)
31.
查詢工資高於本部門平均工資的員工信息 [2分]
select * from employees e1 where salary>(salary avg(salary) from employees e2 where e2.department_id=e1.department_id)
32.
查詢工資高於本部門平均工資的員工信息及其部門的平均工資。[2分]
select e.*,avgsal from employees e join (select department_id,avg(salary) avgsal from employees group by department_id) d on e.department_id=d.department_id and e.salary>d.avgsal
33.
查詢工資高於50號部門某個員工工資的員工信息[2分]
select * from employees where salary>any(select salary from employees where department_id=50)
34.
查詢工資,獎金與10號部門某員工工資,獎金都相同的員工信息[2分]
select * from employees where (salary,nvl(commission_pct)) in(select salary,nvl(commission_pct) from employees where department_id=10
35.
查詢部門人數大於10的部門員工信息[2分]
select * from employees where department_id in(select department_id from employees group by department_id having count(*)>10)
36.
查詢所有員工工資都大於10000的部門信息[2分]
select * from department where department_id in(select department_id from employees group by department_id having min(salary)>10000)
37.
查詢所有員工工資都大於5000元的部門的信息及其員工信息[2分]
select * from departments d,employees e where d.department_id=e.department_id group by department_id having min(salary)>5000
38.
查詢所有員工工資都在4000-8000元之間的部門的信息[2分]
select * from departments where department_id in(select department_id from employees group by department_id having min(slary)>=4000 and max(salary)<=8000)
39.
查詢人數最多的部門信息。[2分]
select * from department_id where department_id in(select department_id from employees group by department_id having count(*)>=all(select count(*) from employees group by department_id))
40.
查詢30號部門中工資排序前3名的員工信息。[2分]
select * from employee where department_id=30 and salary is not null and rownum<=3 order by salary desc
41.
查詢所有員工中工資排序在5-10名之間的員工信息。[2分]
select * from (select rownum rn,employee_id,salary from(select employee_id,salary from employees where salary is not null order by salary desc) e1)e2 where rn between 5 and 10
42.
向employees表中插入一條記錄,員工號爲1000,入職日期爲2002年5月10日,emial爲[email protected],其他信息與員工號爲150的員工相同。[2分]
insert into employees select 1000,first_name,last_name,'[email protected]',phone_number,to_date('10-05-2002','dd-mm-yyyy'),job_id,salary,commission_pct,manager_id,department_id from employees where employee_id=150
43.
將各部門員工的工資修改爲該員工所在部門平均工資加1000元[2分]
update employee b
set sal=(select sal from (select deptno,avg(sal)+1000 sal from employee group by deptno) a where a.deptno=b.deptno)
44.
查詢各月倒數第二天入職的員工信息[2分]
select * from employees where hire_date=last_day(hire_date)-1
45.
查詢工齡大於或等於10年的員工信息。[2分]
select * from employees where (sysdate-hire_date)/365>=10
46.
查詢員工信息,要求以首字母大寫的方式顯示所有員工姓和員工名。[2分]
select employee_id,initcap(first_name),initcap(last_name) from employees
47.
查詢員工名(first_name)正好爲6個字符的員工的信息[2分]
select * from employees where length(first_name)=6
48.
查詢員工名(first_name)的第二個字母爲“M”的員工信息[2分]
select * from employees where first_name like '_M%'
49.
查詢所有員工名(first_name),如果包含字母”s”,則用“S”替換[2分]
select replace(first_name,'s','S') from employees
50.
查詢在2月份入職的所有員工信息[2分]
select * from employees where extract(month from hire_date)=2