首页 > 试题广场 >

数据库 Small Bank corporation有

[问答题]
数据库
Small Bank corporation有如下的员工数据库,为下面每个查询语句写出SQL表达式:


Employee 员工表

员工姓名

employee_name

所住街道

street

所在城市

city


Company 公司信息表

公司名称

company_name

所在城市

city


Works 工作信息表

员工姓名

employee_name

公司名称

company_name

薪水

salary


1)  找出所有为Small Bank corporation工作且薪金超过10000的员工的名字、居住的街道和城市

2)  查找数据库中所有居住城市和公司所在城市相同的员工

3)  找出数据库中所有工资高于Small Bank corporation的每一个员工工资的员工姓名

4)  假设一家公司可以在好几个城市有分部,找出所有这样的公司,其所在城市包含了Small Bank corporation所在的每一个城市

5)  找出平均工资高于Small Bank corporation平均工资的所有公司名字及平均工资

SELECT
    a.company_name,
    avg( a.salary ) 
FROM
    works a 
WHERE
    a.company_name <> 'Small Bank corporation' 
HAVING
    avg( a.salary ) > ( SELECT avg( b.salary ) AS salary FROM works b WHERE b.company_name = 'Small Bank corporation' ) 
GROUP BY
    a.company_name
> 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY
    a.company_name' at line 10
> 时间: 0s
我复制了答案输入到mysql中执行时 出现错误 错误信息如上
在mysql中 group by需要在having之前
编辑于 2019-09-02 11:59:44 回复(2)
参考答案里第五个有点问题,group by 和having的顺序有问题,判断不是‘Small Bank corporation’ 也不需要 
参考:
select wo.company_name ,avg(wo.salary)
from Works wo 
group by wo.company_name 
having avg(wo.salary) > (
	select avg(a.salary) 
	from Works a
	where a.company_name = ‘Small Bank corporation’
);



发表于 2021-03-06 20:41:54 回复(0)
4、select a.company_name from company a  where not exists((select b.city from company c where company_nam='Small Bank corporation') except (select c.city from company c where a.company_name =b.company_name))
except用来获取差
发表于 2019-10-02 16:58:02 回复(1)
这个题目表达不够清楚诶,Small Bank corporation到底是一个公司的名字呢,还是整个数据库的名,有点争议。为了满足问题要求,我把它当作公司名字来写的。

1) select stree,city,employee_name from Employee,Works Where Employee.employee_name=Works.employee_name and Works.company_name='Small Bank corporation' and salary>10000;

2) select Employ_name from Employee,(select city,employee_name from Company,Works where Company.company_name= Works.company_name) A where Employ.employee_name = A.employee_name and Employee.city=A.city;

3) select emloyee_name from Works where salary > all (select salary from Works where Company_name='Small Bank corporation');

4) selec a.company_name from Company a where not exists((select b.city from Company b where b.company_name= 'Small Bank corporation') except (select c.city from Company c where a.company_name = c.company_name));-------参考楼上的

5)select a.company_name avg(salary) from Works a where group by a.company_name having  avg(a.salary) > (select avg(b.salary) from Works b where b.company_name =Small Bank corporation");

不知道对了没,欢迎小伙伴们指出问题 
发表于 2020-03-18 15:29:50 回复(0)
-- 参考答案好多错的,下面是在mysql上实际验证过得代码

-- 1.找出所有为Small Bank corporation工作且薪金超过10000的员工的名字、居住的街道和城市
select e.* from Employee e , Works w where e.employee_name = w.employee_name and w.salary >10000;

-- 2. 查找数据库中所有居住城市和公司所在城市相同的员工
select e.*,A.company_name,A.city from (select Works.employee_name,Works.company_name ,Company.city from Company , Works where Works.company_name = Company.company_name)A , Employee e where e.city = A.city and e.employee_name = A.employee_name; 

-- 3. 找出数据库中所有工资高于Small Bank corporation的每一个员工工资的员工姓名
select w.* from works w where w.salary > (SELECT  max( salary ) FROM works where company_name = 'small bank corporation') ;

-- 4. 假设一家公司可以在好几个城市有分部,找出所有这样的公司,其所在城市包含了Small Bank corporation所在的每一个城市
select A.* from (select a.* from company a ,company b where a.company_name <> 'small bank corporation' and b.company_name = 'small bank corporation' and a.city = b.city)A group by A.company_name having count(*)>=(select count(*)from company where company.company_name = 'small bank corporation') ;

-- 5. 找出平均工资高于Small Bank corporation平均工资的所有公司名字及平均工资
select works.company_name,avg(salary) from works group by works.company_name having avg(salary) > (select avg(salary) from works where works.company_name = 'small bank corporation');

发表于 2021-09-07 22:56:33 回复(0)
答案就是垃圾,我不想说什么,
发表于 2022-02-22 21:25:57 回复(0)
1.select employee_name, street, city from Employee e, Works w where e.employee_name=w.employee_name and salary>10000 and company_name='Small Bank corporation';
2.select employee_name from Employee e ,Company c ,Works w where e.city=c.city and e.employee_name=w.employee_name and c.company_name=w.company;
3.select enmployee_name from Works where salary >(select max(salary) from Works where company_name=‘Small Bank corporation’);
4.select company_name from(select company_name from Company where city in(select city from Company where company_name='Small Bank corporation')) as sheet where sheet.company_name!='Small Bank corporation';
5.select company_name, avg(salary) from Works group by company_name where avg(salary)>(select avg(salary) from Works where company_name='Small Bank corporation'); 
发表于 2020-10-06 12:31:18 回复(0)

(1) SELECT employee name, city, street FROM Employee, Works WHERE Employee. employee_name=Works. employee_name AND Works. company name=Fist Bank Cooporation AND salary>10000

(2) Select employ_name from Employee e,Company c  where e.city=c.city;

 

(3)SELECT employee_name FROM Works WHERE salary>(SELECT MAX(salary)

FROM Works WHERE Works. company_name=Small Bank Cooporation)

(4)SELECT company_name FROM Company WHERE city=(SELECT city FROM Company WHERE Works. company_name=Small Bank Cooporation)

(5) SELECT company_name FROM Works WHERE AUG(salary)>(SELECT AUG(salary)

FROM Works WHERE Works. company_name=Fist Bank Cooporation)

GROUP BY company_name

 

发表于 2020-03-15 12:24:47 回复(0)
select a.company_name from 
(select a.company_name,count(*) as count from Company a group by company_name) as C
where C.count = (select count(distinct city) from Company b)
发表于 2020-03-07 12:00:35 回复(0)