欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

oracle关于查询的习题

程序员文章站 2022-03-08 23:05:46
/* 1.查询雇佣时间在1997年之后的员工信息。 */ select * from employees where to_char(hire_date, ‘yyyy’) &...

/*

1.查询雇佣时间在1997年之后的员工信息。

*/

select * from employees where to_char(hire_date, ‘yyyy’) > 1997;

/*

2.查询有提成的员工信息(last name, job, salary, and commission),并按工资降序排列

*/

select last_name,job_id,salary,commission_pct

from employees

where commission_pct is not null

order by salary desc;

/*

3.show the employees that have no commission with a 10% raise in their salary (round off the salaries).

*/

select ‘the salary of ’ || last_name || ’ after a 10% raise is ’ ||

salary * 1.1 as new_salary

from employees

where commission_pct is null;

/*

4.show the last names of all employees together with the number of years and the number of completed months that they have been employed.

*/

select last_name,

trunc(months_between(sysdate, hire_date) / 12, 0) as years,

mod((to_char(hire_date, ‘mm’) - to_char(sysdate, ‘mm’) + 12), 12) as months

from employees;

/*

5.show those employees that have a name starting with j, k, l, or m.

*/

select last_name

from employees

where substr(last_name, 1, 1) in (‘j’, ‘k’, ‘l’, ‘m’);

/*

6.show all employees, and indicate with “yes” or “no” whether they receive a commission.

*/

select last_name,salary,nvl2(commission_pct,’yes’,’no’)

from employees;

/*

7.show the department names, locations, names, job titles, and salaries of employees who work in location 1800.

*/

select department_name,location_id,last_name,jobs.job_id,salary

from departments dept,jobs,employees

where dept.department_id=employees.department_id

and employees.job_id = jobs.job_id

and location_id = 1800;

/*

8.how many employees have a name that ends with an n? create two possible solutions.

*/

select count(*) from employees where substr(last_name, -1) = ‘n’

select count(*) from employees where last_name like ‘%n’;

/*

9.show the names and locations for all departments, and the number of employees working in each department. make sure that departments without employees are included as well.

*/

select d.department_id, department_name, location_id, nvl(counts, 0)

from departments d,

(select count(employee_id) as counts, department_id

from employees

group by department_id) e

where e.department_id(+) = d.department_id;

/*

10.which jobs are found in departments 10 and 20?

*/

select distinct job_id from employees where department_id between 10 and 20;

/*

11.which jobs are found in the administration and executive departments, and how many employees do these jobs? show the job with the highest frequency first.

*/

select job_id, count(*) as fruquency

from (select *

from employees

where department_id in

(select department_id

from departments

where department_name in (‘administration’, ‘executive’)))

group by job_id

order by fruquency desc;

/*

12.show all employees who were hired in the first half of the month (before the 16th of the month).

*/

select last_name, hire_date

from employees

where to_char(hire_date, ‘dd’) <= 15;

/*

13.show the names, salaries, and the number of dollars (in thousands) that all employees earn.

*/

select last_name, round(salary, 0), trunc(salary / 1000, 0) thousands

from employees;

/*

14.show all employees who have managers with a salary higher than $15,000. show the following data: employee name, manager name, manager salary, and salary grade of the manager.

*/

select e.last_name,m.last_name as manager,m.salary

from employees e,employees m

where e.manager_id=m.employee_id and m.salary>15000;

/*

15.show the department number, name, number of employees, and average salary of all departments, together with the names, salaries, and jobs of the employees working in each department.

通过lag函数来实现

*/

select (nvl2((lag(t.department_id, 1, null)

over(partition by t.department_id order by e.employee_id)),

null,

t.department_id)) as department_id,

(nvl2((lag(t.department_id, 1, null)

over(partition by t.department_id order by e.employee_id)),

null,

t.department_name)) as department_name,

(nvl2((lag(t.department_id, 1, null)

over(partition by t.department_id order by e.employee_id)),

null,

t.employees)) as employees,

(nvl2((lag(t.department_id, 1, null)

over(partition by t.department_id order by e.employee_id)),

null,

t.avg_salary)) as avg_salary,

last_name,

job_id,

salary

from employees e,

(select d.department_id,

department_name,

count(e.department_id) as employees,

round(nvl(avg(salary), 0), 2) as avg_salary

from employees e, departments d

where e.department_id = d.department_id

group by d.department_id, d.department_name) t

where t.department_id = e.department_id

order by t.department_id, employee_id;

/*

16.show the department number and the lowest salary of the department with the highest average salary.

*/

select e.department_id, min(salary)

from employees e,

(select *

from (select department_id, avg(salary) as avg_salary

from employees

group by department_id

order by avg_salary desc)

where rownum = 1) d

where e.department_id = d.department_id

group by e.department_id;

/*

17.show the department numbers, names, and locations of the departments where no sales representatives work

*/

/*

18. show the department number, department name, and the number of employees working in each department that:

a. includes fewer than 3 employees:

b. has the highest number of employees:

c. has the lowest number of employees:

*/

select e.department_id, department_name, count(e.employee_id) as employees

from departments d, employees e

where e.department_id = d.department_id

group by e.department_id, department_name

having count(e.employee_id) < 3;

select *

from (select e.department_id,

department_name,

count(e.employee_id) as employees

from departments d, employees e

where e.department_id = d.department_id

group by e.department_id, department_name

order by employees desc)

where rownum = 1;

select *

from (select e.department_id,

department_name,

count(e.employee_id) as employees

from departments d, employees e

where e.department_id = d.department_id

group by e.department_id, department_name

order by employees)

where rownum = 1;

/*

19.show the employee number, last name, salary, department number, and the average salary in their department for all employees.

*/

select employee_id,

last_name,

department_id,

round(avg(salary) over(partition by department_id),4) as avg_salary

from employees

order by employee_id;

/*

20.show all employees who were hired on the day of the week on which the highest number of employees were hired.

*/

select last_name, dd as day

from employees,

(select dd

from (select to_char(hire_date, ‘day’) as dd, count(*) as cc

from employees

group by to_char(hire_date, ‘day’)

order by cc desc)

where rownum = 1)

where to_char(hire_date, ‘day’) = dd;

/*

21.create an anniversary overview based on the hire date of the employees. sort the anniversaries in ascending order.

*/

create view emp_hiredate as select last_name, to_char(hire_date, ‘month-dd’) as birthday

from employees

order by to_char(hire_date,’mm’),to_char(hire_date,’dd’) ;

/*

22. find the job that was filled in the first half of 1990 and the same job that was filled during the same period in 1991.

*/

select job_id

from employees

where extract(year from hire_date) = 1990

and extract(month from hire_date) <= 6

intersect

select job_id

from employees

where extract(year from hire_date) = 1991

and extract(month from hire_date) <= 6

;

/*

23.write a compound query to produce a list of employees showing raise percentages, employee ids, and old salary and new salary increase. employees in departments 10, 50, and 110 are given a 5% raise, employees in department 60 are given a 10% raise, employees in departments 20 and 80 are given a 15% raise, and employees in department 90 are not given a raise.

*/

select nvl2(aa,lpad(aa,2,’0’),’no’)||’%raise’ as raise,employee_id, salary, nvl(aa/100,1) * salary as new_salary

from (select employee_id as em_id,

(case

when department_id in (10,50,110) then 5

when department_id = 60 then 10

when department_id in (20,80) then 15

else null

end

) aa

from employees),

employees

where employees.employee_id = em_id

order by aa;

/*

24.alter the session to set the nls_date_format to dd-mon-yyyy hh24:mi:ss.

*/

alter session set nls_date_format = ‘dd-mon-yyyy hh24:mi:ss’

/*

25.a. write queries to display the time zone offsets (tz_offset) for the following time zones.

australia/sydney

*/

select tz_offset (‘australia/sydney’) from dual;

–chile/easter island

select tz_offset (‘chile/easterisland’) from dual;

/*

b. alter the session to set the time_zone parameter value to the time zone offset of australia/sydney.

*/

alter session set time_zone = ‘+10:00’;

/*

c. display the sysdate, current_date, current_timestamp, and localtimestamp for this session.

*/

select sysdate,current_date, current_timestamp, localtimestamp from dual;

/*

d. alter the session to set the time_zone parameter value to the time zone offset of chile/easter island.

*/

alter session set time_zone = ‘-6:00’;

/*

e. display the sysdate, current_date, current_timestamp, and localtimestamp for this session.

*/

select sysdate,current_date, current_timestamp, localtimestamp from dual;

/*

f. alter the session to set the nls_date_format to dd-mon-yyyy.

*/

alter session set nls_date_format= ‘dd-mon-yyyy’;

/*

26.write a query to display the last names, month of the date of join, and hire date of those employees who have joined in the month of january, irrespective of the year of join.

*/

select last_name, extract(month from hire_date) as month_join, hire_date

from employees

where extract(month from hire_date) = 1;

/*

27.write a query to display the following for those departments whose department id is greater than 80:

the total salary for every job within a department

the total salary

the total salary for those cities in which the departments are located

the total salary for every job, irrespective of the department

the total salary for every department irrespective of the city

the total salary of the cities in which the departments are located

total salary for the departments, irrespective of job titles and cities

*/

select city, department_name as dname, e.job_id as job, sum(salary)

from employees e, departments d, locations l

where e.department_id = d.department_id

and d.location_id = l.location_id

and e.department_id > 80

group by cube(city, department_name, e.job_id);

/*

28.. write a query to display the following groupings:

department id, job id

job id, manager id

the query should calculate the maximum and minimum salaries for each of these groups.

*/

select department_id, job_id, manager_id, max(salary), min(salary)

from employees

group by grouping sets((department_id, job_id),(job_id, manager_id));

/*

29.write a query to display the top three earners in the employees table. display their last names and salaries.

*/

select *

from (select last_name, salary from employees order by salary desc)

where rownum <= 3;

/*

30.write a query to display the employee id and last names of the employees who work in the state of california.

*/

select last_name,employee_id from employees e,departments d,locations l

where e.department_id = d.department_id

and d.location_id = l.location_id

and l.state_province=’california’;

/*

31.write a query to delete the oldest job_history row of an employee by looking up the

job_history table for the min(start_date) for the employee. delete the records of

only those employees who have changed at least two jobs. if your query executes correctly,

you will get the feedback:

*/

delete from job_history jh

where employee_id = (select employee_id

from employees e

where jh.employee_id = e.employee_id

and start_date =

(select min(start_date)

from job_history jh

where jh.employee_id = e.employee_id)

and 3 > (select count(*)

from job_history jh

where jh.employee_id = e.employee_id

group by employee_id

having count(*) >= 2));

/*

33.write a query to display the job ids of those jobs

whose maximum salary is above half the maximum salary in the whole company.

use the with clause to write this query. name the query max_sal_calc.

*/

with

max_sal_clac as (select employees.job_id,job_title,max(salary) as job_salary from employees,jobs

where jobs.job_id = employees.job_id

group by employees.job_id,job_title),

max_sal_cmp as (select max(salary) as max_salary from employees)

select job_title,job_salary from max_sal_clac

where job_salary > (select * from max_sal_cmp)/2

/*

34. write a sql statement to display employee number, last name, start date, and salary, showing:

a. de haan’s direct reports

*/

select employee_id, last_name, hire_date, salary

from employees

where manager_id =

(select employee_id from employees where last_name = ‘de haan’);

/*

b. the organization tree under de haan (employee number 102)

*/

select employee_id, last_name, hire_date, salary

from employees

start with manager_id = 102

connect by manager_id = prior employee_id;

/*

35. write a hierarchical query to display the employee number, manager number, and employee

last name for all employees who are two levels below employee de haan (employee

number 102). also display the level of the employee.

*/

select employee_id,manager_id,last_name,level

from employees

where level>2

start with employee_id=102

connect by manager_id = prior employee_id

/*

36. produce a hierarchical report to display the employee number, manager number, the level

pseudocolumn, and employee last name. for every row in the employees table, you

should print a tree structure showing the employee, the employee’s manager, then the

manager’s manager, and so on. use indentations for the name column.

*/

select employee_id,

manager_id,

level,

lpad(last_name, length(last_name) + level - 1, ‘_’)

from employees e

start with employee_id in

(select employee_id

from employees

start with manager_id is null

connect by manager_id = prior employee_id)

connect by e.employee_id = prior e.manager_id;