SQL 语句面试题目:一般查询和高级子查询(上)2018-06-26

******************************************************************************************

关注凡猫学院:加微信+17031115530,拉测试微信群交流

关注凡猫学院:加微信+17031115530,拉测试微信群交流

*******************************************************************************************

SQL 语句面试题目:一般查询和高级子查询(上)

几个表

employees 表:

EMPLOYEE_ID NUMBER(6)

FIRST_NAME VARCHAR2(20)

LAST_NAME VARCHAR2(25)

EMAIL VARCHAR2(25)

PHONE_NUMBER VARCHAR2(20)

HIRE_DATE DATE

JOB_ID VARCHAR2(10)

SALARY NUMBER(8,2)

COMMISSION_PCT NUMBER(2,2)

MANAGER_ID NUMBER(6)

DEPARTMENT_ID NUMBER(4)

jobs 表:

JOB_ID VARCHAR2(10)

JOB_TITLE VARCHAR2(35)

MIN_SALARY NUMBER(6)

MAX_SALARY NUMBER(6)

departments 表:

DEPARTMENT_ID NUMBER(4)

DEPARTMENT_NAME VARCHAR2(30)

MANAGER_ID NUMBER(6)

LOCATION_ID NUMBER(4)

locations 表:

LOCATION_ID NUMBER(4)

微信+17031115530,拉测试微信群交流

job_grades 表:

GRADE_LEVEL VARCHAR2(3)

LOWEST_SAL NUMBER

HIGHEST_SAL NUMBER

1 一般查询

1. 查询每个月倒数第2 天入职的员工的信息.

2. 查询出last_name 为’Chen’ 的manager 的信息.

3. 查询平均工资高于8000 的部门id 和它的平均工资.

4. 查询工资最低的员工信息:last_name, salary

5. 查询平均工资最低的部门信息

6. 查询平均工资最低的部门信息和该部门的平均工资

7. 查询平均工资最高的job 信息

8. 查询平均工资高于公司平均工资的部门有哪些?

9. 查询出公司中所有manager 的详细信息.

10. 各个部门中最高工资中最低的那个部门的最低工资是多少

11. 查询平均工资最高的部门的manager 的详细信息: last_name,department_id, email, salary

12. 查询1999 年来公司的人所有员工的最高工资的那个员工的信息.

13. 返回其它部门中比job_id 为‘IT_PROG’部门所有工资都低的员工的员工号、姓名、job_id

以及salary

************************answers*********************

1.查询每个月倒数第2 天入职的员工的信息.

STREET_ADDRESS VARCHAR2(40)

POSTAL_CODE VARCHAR2(12)

CITY VARCHAR2(30)

STATE_PROVINCE VARCHAR2(25)

COUNTRY_ID CHAR(2)

微信+17031115530,拉测试微信群交流

[plain] view plain copy

select last_name, hire_date

from employees

where hire_date = last_day(hire_date) – 1

2. 查询出last_name 为’Chen’ 的manager 的信息.

1). 通过两条sql 查询:

[sql] view plain copy

select manager_id

from employees

where lower(last_name) = ‘chen’ — 返回的结果为108

select *

from employees

where employee_id = 108

2). 通过一条sql 查询(自连接):

[sql] view plain copy

select m.*

from employees e, employees m

where e.manager_id = m.employee_id and e.last_name= ‘Chen’

3). 通过一条sql 查询(子查询):

[sql] view plain copy

select *

from employees

where employee_id = (

select manager_id

from employees

where last_name = ‘Chen’

)

3.查询平均工资高于8000 的部门id 和它的平均工资.

[sql] view plain copy

SELECT department_id, avg(salary)

FROM employees e

GROUP BY department_id

HAVING avg(salary) > 8000

微信+17031115530,拉测试微信群交流

4. 查询工资最低的员工信息:last_name, salary

[sql] view plain copy

SELECT last_name, salary

FROM employees

WHERE salary = ( SELECT min(salary) FROM employees )

5.查询平均工资最低的部门信息

[sql] view plain copy

SELECT *

FROM departments

WHERE department_id =(

SELECT department_id

FROM employees

GROUP BY department_id

HAVING avg(salary) =(

SELECT min(avg(salary))

FROM employees

GROUP BY department_id )

)

6.查询平均工资最低的部门信息和该部门的平均工资

[sql] view plain copy

select d.*, (select avg(salary)

from employees

where department_id = d.department_id)

from departments d

where d.department_id = (

SELECTdepartment_id FROM employees

GROUP BY department_id HAVING avg(salary) =(

SELECT min(avg(salary)) FROM employees

GROUP BY department_id )

)

7.查询平均工资最高的job 信息

1). 按job_id 分组, 查询最高的平均工资

[sql] view plain copy

SELECT max(avg(salary))

FROM employees

GROUP BY job_id

微信+17031115530,拉测试微信群交流

2). 查询出平均工资等于1) 的job_id

[sql] view plain copy

SELECT job_id

FROM employees

GROUP BYjob_id

HAVING avg(salary) = (

SELECT max(avg(salary))

FROM employees

GROUP BY job_id

)

3). 查询出2) 对应的job 信息

[sql] view plain copy

SELECT *

FROM jobs

WHERE job_id = (

SELECT job_id

FROM employees

GROUP BY job_id

HAVING avg(salary) = (

SELECT max(avg(salary))

FROM employees

GROUP BY job_id )

)

8.查询平均工资高于公司平均工资的部门有哪些?

1). 查询出公司的平均工资

[sql] view plain copy

SELECT avg(salary)

FROM employees

2). 查询平均工资高于1) 的部门ID

[sql] view plain copy

SELECT department_id

FROM employees

GROUP BY department_id

HAVING avg(salary) > ( SELECT avg(salary) FROMemployees)

微信+17031115530,拉测试微信群交流

9.查询出公司中所有manager 的详细信息.(IN 关键字)

1). 查询出所有的manager_id

[sql] view plain copy

SELECT distinct manager_id FROM employeess

2). 查询出employee_id 为1) 查询结果的那些员工的信息

[sql] view plain copy

SELECT employee_id, last_name

FROM employees

WHERE employee_id in (

SELECT distinct manager_id

FROM employees

)

10.各个部门中最高工资中最低的那个部门的最低工资是多少

1). 查询出各个部门的最高工资

[sql] view plain copy

SELECT max(salary)

FROM employees

GROUP BY department_id

2). 查询出1) 对应的查询结果的最低值: 各个部门中最低的最高工资(无法查询对应的

department_id)

[sql] view plain copy

SELECT min(max(salary))

FROM employees

GROUP BY department_id

3). 查询出2) 所对应的部门id 是多少: 各个部门中最高工资等于的那个部门的id

[sql] view plain copy

SELECT department_id

FROM employees

GROUP BY department_id

HAVING max(salary) =(

SELECT min(max(salary))

FROM employees

GROUP BY department_id

)

微信+17031115530,拉测试微信群交流

4). 查询出3) 所在部门的最低工资

[sql] view plain copy

SELECT min(salary)

FROM employees

WHERE department_id = (

SELECT department_id

FROM employees

GROUP BY department_id HAVING max(salary) =(

SELECT min(max(salary))

FROM employees

GROUP BY department_id )

)

11.查询平均工资最高的部门的manager 的详细信息: last_name,department_id,

email, salary

1). 各个部门中, 查询平均工资最高的平均工资是多少

[sql] view plain copy

SELECT max(avg(salary))

FROM employees

GROUP BY department_id

2). 各个部门中, 平均工资等于1) 的那个部门的部门号是多少

[sql] view plain copy

SELECT department_id

FROM employees

GROUP BY department_id

HAVING avg(salary) = (

SELECT max(avg(salary))

FROM employees

GROUP BY department_id

)

3). 查询出2) 对应的部门的manager_id

[sql] view plain copy

SELECT manager_id

FROM departments

WHERE department_id = (

SELECT department_id

FROM employees

GROUP BY department_id

微信+17031115530,拉测试微信群交流

HAVING avg(salary) = (

SELECT max(avg(salary))

FROM employees

GROUP BY department_id )

)

4). 查询出employee_id 为3) 查询的manager_id 的员工的last_name, department_id, email,

salary

[sql] view plain copy

SELECT last_name, department_id, email,salary

FROM employees

WHERE employee_id = (

SELECT manager_id

FROM departments

WHERE department_id = (

SELECT department_id

FROM employees

GROUP BY department_id

HAVING avg(salary) = (

SELECT max(avg(salary))

FROM employees

GROUP BY department_id )

)

)

12.查询1999 年来公司的人中所有员工的最高工资的那个员工的信息.

1). 查询出1999 年来公司的所有的员工的salary

[sql] view plain copy

SELECT salary

FROM employees

WHERE to_char(hire_date,’yyyy’) = ‘1999’

2). 查询出1) 对应的结果的最大值

[sql] view plain copy

SELECT max(salary)

FROM employees

WHERE to_char(hire_date, ‘yyyy’) = ‘1999’

微信+17031115530,拉测试微信群交流

3). 查询工资等于2) 对应的结果且1999 年入职的员工信息

[sql] view plain copy

SELECT *

FROM employees

WHERE to_char(hire_date, ‘yyyy’) = ‘1999’ AND salary = (

SELECT max(salary)

FROMemployees

WHERE to_char(hire_date, ‘yyyy’) = ‘1999’ )

13.返回其它部门中比job_id 为‘IT_PROG’部门所有工资都低的员工的员工

号、姓名、job_id 以及salary

[sql] view plain copy

SELECT employee_id, last_name, job_id,salary

FROM employees

WHERE salary< ALL (SELECT salary

FROM employees

WHERE job_id = ‘IT_PROG’)

AND job_id<> ‘IT_PROG’;

******************************************************************************************

关注凡猫学院:加微信+17031115530,拉测试微信群交流

关注凡猫学院:加微信+17031115530,拉测试微信群交流

*******************************************************************************************

    原文作者:凡猫学院
    原文地址: https://www.jianshu.com/p/592837bf3d3c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞