表结构:
 create database MyCompany
 go
 use MyCompany
 go
 create table Departments
 (
     Department_ID ,) primary key,
     Department_Name ),
 )
 go
 create table Employees
 (
     Employee_Id ,) primary key,
     Employee_Name ),
     Employee_Job ),
     Salary money,
     Department_Id int foreign key references Departments(Department_ID)
 )
 Go

 --------------------------------------------插入数据----------------------------------------------------------------------------------
 ----------------------------------部门表-------------------------------------------------------------------
 SET IDENTITY_INSERT departments ON
              ,    N'财务部'                                                                                                                                                                                                                                                           )
              ,    N'行政部'                                                                                                                                                                                                                                                           )
              ,    N'开发部'                                                                                                                                                                                                                                                           )
              ,    N'市场部'                                                                                                                                                                                                                                                           )
 SET IDENTITY_INSERT departments OFF
 ------------------------=============================员工表================================================================================================
            ,    N             )
            ,    N             )
            ,    N             )
            ,    N             )
            ,    N             )
            ,    N             )
            ,    N             )
            ,    N             )
            ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )
           ,    N             )

 SET IDENTITY_INSERT employees OFF

上面是创建表和数据库的代码

use MyCompanyTest
go
select * from dbo.Departments
select * from Employees

--1.列出EMPLOYEES表中各部门的:部门编号,最高工资,
select e.department_Id 部门编号, MAX(e.Salary) 最高工资,MIN(e.salary)最低工资
from Employees e
group by e.department_Id 

--2.列出EMPLOYEES表中各部门的:部门编号、部门名称、最高工资、最低工资

  select 部门编号, 最高工资,最低工资 ,d.Department_Name from
      (select e.Department_ID 部门编号, max(e.Salary)最高工资,min(e.Salary)最低工资
     from Employees e
    group by e.Department_Id )as t join Departments d
on t.部门编号=d.Department_ID

--3.列出EMPLOYEES表中各部门中'职员'(Employee_job为'职员')的:最低工资,最高工资和部门Id

select min(Salary)最低工资,MAX(Salary) 最高工资,Department_Id 部门Id from Employees
where Employee_Job='职员'
group by Department_Id

--4.对于EMPLOYEES中最低工资小于1000的部门,列出EMPLOYEE_JOB为'职员'的:部门编号,最低工资,最高工资

select * from(
    select Department_Id,MIN(e.Salary)最低工资,MAX(e.Salary)最高工资  from Employees e
    where e.Employee_Job='职员'
    group by e.Department_Id) as t
    join Departments d on d.Department_ID =t.Department_Id

--5.根据部门编号由高到低,工资由低到高,列出每个员工的姓名,部门号,工资
        select * from Employees
        order by Department_Id desc , Salary asc
--6.列出'吴用'所在部门中每个员工的姓名与部门号
    select * from Employees where Department_Id in (
    select   Department_Id  from Employees where Employee_Name='吴用'
    )
--7.列出每个员工的姓名,头衔,部门号,部门名
    select e.Employee_Name,e.Employee_Job,d.Department_ID,d.Department_Name
     from Employees e join Departments d
    on e.Department_Id=d.Department_ID
--8.列出EMPLOYEES中头衔为'职员'的员工的姓名,工作,部门号,部门名

        select * from Employees e
        join Departments d on e.Department_Id=d.Department_ID
        where e.Employee_Job='职员'
--9.对于DEPARTMENTS表中,列出所有(说明是左联):部门名称,部门编号,以及该部门的:员工姓名与头衔
    select * from Departments d left join Employees e
    on d.Department_ID=e.Department_Id
--10.列出工资高于本部门工资平均水平的员工的部门编号,姓名,工资,并且按部门编号排序。
select * from Employees e join(
            select Department_Id, AVG(Salary)as 平均工资 from Employees  e
            group by e.Department_Id)as t --求出各个部门的平均工资
    on e.Department_Id=t.Department_Id
    where e.Salary > t.平均工资
    order by e.Department_Id

    ---相关子查询
    select * from employees as emp
    where
    exists(
     select department_id,avg(salary) as avg_salary
       from employees as emp_sub
        group by department_id
    having emp_sub.department_id=emp.department_id and emp.salary>avg(salary)
    )

----
select *
from EMPLOYEES as e
where
e.SALARY >(select avg(SALARY) from EMPLOYEES as b where e.DEPARTMENT_ID = b.DEPARTMENT_ID)
order by e.DEPARTMENT_ID
--11.对于EMPLOYEES,列出各个部门中工资高于本部门平均水平的员工 数和部门号,按部门号排序
select
        emp.department_id as 部门编号,
        count(*) as 员工数
from Employees as emp
where emp.salary >
        (select avg(salary) from employees  emp_sub
      where emp_sub.department_id=emp.department_id)
group by emp.department_id
order by emp.department_id

--12.请找出部门中具有2人以上,员工工资大于所在部门平均工资的:部门的id与这些人的人数。
--分解:
--1>.部门中有人的工资比部门的平均工资还高
--2>并且这些人在2人以上
--3>查询出这些部门Id,与工资高于部门平均工资的人的人数。
select
        emp.department_id as 部门编号,
        count(*) as 员工数
from Employees as emp
where emp.salary > (select avg(salary) from employees emp_sub where emp_sub.department_id=emp.department_id)
group by emp.department_id

order by emp.department_id

--13.对于EMPLOYEES中低于自己工资至少5人的员工,列出其部门号,姓名,工资,
--以及工资少于自己的人数
select

        employee_name 姓名,
        salary 工资,
        小于自己工资的人数=(select count(*) from employees as emp_sub where emp_sub.salary<emp.salary)
from employees as emp

sql习题练习的更多相关文章

  1. sql习题及答案

    sql习题:http://www.cnblogs.com/wupeiqi/articles/5729934.html 习题答案参考:https://www.cnblogs.com/wupeiqi/ar ...

  2. sql习题--转换(LEFT/RIGTH)

    /* 转换为100-5 0100-000051-998 0001-0099812-1589 0012-01589*/IF EXISTS(SELECT * FROM sys.objects WHERE ...

  3. SQL简单语句总结习题

    创建一个表记员工个人信息: --创建一个表 create table plspl_company_info( empno ) not null, ename ) not null, job ), ma ...

  4. 我认为测试应该掌握的SQL语句

    最近在学习Oracle,对测试人员而言必须掌握两种语言:第一种是DML,数据操纵语言 (Data Manipulation Language) 是SQL语言中,负责对数据库对象运行数据访问工作的指令集 ...

  5. 资料整理,SQL Server ,面试前复习笔记

    T-SQL 要掌握的知识点分类 SQL 面向数据库执行查询 SQL 从数据库取回数据 SQL 在数据库中插入新的记录 SQL 更新数据库中的数据 SQL 从数据库删除记录 SQL 创建新数据库 SQL ...

  6. 推荐《SQL基础教程(第2版)》中文PDF+源代码+习题答案

    我认为<SQL基础教程(第2版)>非常适合数据库学习的初学者.论述的角度是读者的角度,会换位思考到读者在看到这一段时候会发出怎样的疑问,非常难得:原始数据的例题只有一道,但是可以反复从不同 ...

  7. SQL Server(四)——查询练习(45道习题)

    题目:设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1 ...

  8. Server SQL 2008 习题

    [序言:学期末了,整理了自己这个学期学习数据库做的练习题,也是让自己复习了一遍.有错误的话希望大佬能够批评指正,不胜感激] 一.修改数据库 (1)给db_temp数据库添加一个数据文件文件db_tem ...

  9. SQL Server(四)——查询练习(45道习题)转

    题目:设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1 ...

随机推荐

  1. hdu 6119 …&&百度之星 T6

    小小粉丝度度熊 Problem Description 度度熊喜欢着喵哈哈村的大明星——星星小姐. 为什么度度熊会喜欢星星小姐呢? 首先星星小姐笑起来非常动人,其次星星小姐唱歌也非常好听. 但这都不是 ...

  2. mysql 连接失败问题汇集

    FHost '192.168.5.128' is not allowed to connect to this MySQL serverConnection closed by foreign hos ...

  3. 编写COOL编译器

    Coursera上面有Stanford的课程“Compilers”,该课程使用“龙书”作为参考书,并有一个编程项目,完成一个完整的编译器.这个编译器支持的语言称为COOL,是一个面向对象的用于教学的语 ...

  4. JQuery实现多个菜单的显示隐藏

    (如有错敬请指点,以下是我工作中遇到并且解决的问题) 效果图: 点击各个菜单显示/隐藏,以及点击灰色部分隐藏. 比如点击了第一个菜单,然后点击第二个菜单,第一个菜单会隐藏,再显示第二个菜单,不会叠加. ...

  5. Laravel向视图传递变量的两种方法

    //方法一 return view('home.user')->with('datas', $datas); //方法二 return view('home.user.my-indent',co ...

  6. 2017 CCPC 湘潭邀请赛

    都tm快一年了我还没补这套题……再不补怕是要留给退役后乐 Problem A 把$n * (n + 1)$的矩阵补成$(n + 1) * (n + 1)$的,然后高斯消元. Problem B 一看题 ...

  7. Python Unittest与数据驱动

    python中有一个装饰器类DDT,通过它我们可以复用代码,达到数据驱动测试的目的,该类的官方介绍可以参考 http://ddt.readthedocs.io/en/latest/index.html ...

  8. [Math Review] Statistics Basic: Estimation

    Two Types of Estimation One of the major applications of statistics is estimating population paramet ...

  9. C++ 二位数组做参数传递

    指针的强大功能,,,,简直牛逼!!! #include<iostream> #include<cstdio> #include<map> using namespa ...

  10. PDF审计工具peepdf

    PDF审计工具peepdf   PDF是Portable Document Format(便携式文档格式)的缩写.它是Adobe公司推出的文件格式规范.现在,PDF是网络电子书籍的主流格式.由于PDF ...