我这里有一个跨国公司给我的SQL培训题,给大家分享一下(1)--答案

 

 

CREATE TABLE `department` (            

              `id` int(5) NOT NULL auto_increment, 

              `name` varchar(20) default '(NULL)', 

              PRIMARY KEY  (`id`)                  

            ) ENGINE=InnoDB DEFAULT CHARSET=gbk 

 

CREATE TABLE `employee` (                                                                   

            `id` int(5) NOT NULL auto_increment,                                                     

            `name` varchar(20) NOT NULL,                                                              

            `departmentid` int(5) NOT NULL,                                                          

            PRIMARY KEY  (`id`),                                                                     

            KEY `departmentid` (`departmentid`),                                                     

            CONSTRAINT `employee_ibfk_2` FOREIGN KEY (`departmentid`) REFERENCES `department` (`id`) 

          ) ENGINE=InnoDB DEFAULT CHARSET=gbk 

 

 

2. select department.name as '部门名称', count(employee.name) as '员工数' from department join employee on(department.id=employee.departmentid) where department.name is not null group by department.name

 

3.      select employee.name as '姓名' from department  join employee  on(department.id=employee.departmentid) where department.name  is null

 

4. select employee.name as '姓名', count(employee.name) as '部门数' from department   join employee  on(department.id=employee.departmentid) group by employee.name having count(employee.name)>1

 

5. select department1.name as '部门名称',department2.name as '关联部门' from employee employee1 ,department department1, employee employee2 ,department department2 where employee1.departmentid=department1.id and employee2.departmentid=department2.id and not employee1.departmentid  = 1 and not employee2.departmentid  = 1 and employee1.name=employee2.name and not department1.name=department2.name group by department1.name,department2.name

 

 

这个是公司认为比较可以的代码,大家有更好,更高效的代码希望大家分享哦

接下来,我会为大家带来下一阶段的培训

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值