SELECT a.name as '姓名',
(SELECT b.chengji from aa b where b.kemu='英语' and b.name=a.name) as '英语',
(SELECT b.chengji from aa b where b.kemu='数学' and b.name=a.name) as '数学',
(SELECT b.chengji from aa b where b.kemu='语文' and b.name=a.name) as '语文'
from aa a GROUP BY name
-- 1、在emp表中查询出所有记录的姓名、部门编号、薪水,并且列名要显示为中文。
SELECT ename AS '姓名',deptno AS '部门编号',IFNULL(sal,0)+IFNULL(comm,0) AS '薪水'
FROM emp;
-- 2、在emp表中查询出薪水大于1500的记录,并且按照薪水的降序排列。
SELECT * FROM emp WHERE IFNULL(sal,0)+IFNULL(comm,0) >1500
ORDER BY IFNULL(sal,0)+IFNULL(comm,0) DESC;
-- 3、在emp表中查询出comm字段为空值的记录。
SELECT * FROM emp WHERE comm is NULL;
-- 4、查询出emp表中含有几个部门的记录。(用DISTINCT去除重复记录)
SELECT COUNT(DISTINCT deptno) FROM emp;
-- 5、在emp表中查询出部门编号为1或2的记录(要求使用IN关键字)
SELECT * FROM emp WHERE deptno IN(1,2);
-- 6、在emp表中查询出姓名的第二字为独的记录。
SELECT * FROM emp WHERE ename LIKE '_独%';
-- 7、查询出emp表中总共有多少条记录。
SELECT COUNT(*) FROM emp;
-- 8、查询emp表中出每个部门的部门代码、薪水之和、平均薪水。
SELECT AVG(sal+IFNULL(comm,0))AS '平均薪水',
SUM(sal+IFNULL(comm,0))AS '薪水之和',
deptno AS '部门编号' FROM emp GROUP BY deptno ;
-- ----------------------------------------------------------------------------
练习二
-- 1.列出至少有一个员工的所有部门。
-- select dname from dept where deptno in(select deptno from emp);
select dname from dept where deptno
in(select deptno from emp group by deptno having count(deptno) >=1);
❌-- 2.列出薪金比“陈独秀”多的所有员工。
select * from emp where sal > (select sal from emp where ename = '陈独秀');
-- 3.列出所有员工的姓名及其直接上级的姓名。
select a.ename,(select ename from emp b where b.empno=a.mgr) as boss_name from emp a;
-- SELECT ename,mgr FROM emp;
-- 4.列出受雇日期早于其直接上级的所有员工。
select a.ename from emp a
where a.hiredate<(select hiredate from emp b where b.empno=a.mgr);
-- 5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
select a.dname,b.empno,b.ename,b.job,b.mgr,b.hiredate,b.sal,b.deptno
from dept a left join emp b on a.deptno=b.deptno;
-- 6.列出所有“CLERK”(办事员)的姓名及其部门名称。
select a.ename,b.dname from emp a join dept b on a.deptno=b.deptno and a.job='职员';
-- 7.列出最低薪金大于1500的各种工作。
select distinct job as '工作' from emp group by job having min(sal)>1500;
-- 8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
select ename from emp where deptno=(select deptno from dept where dname='营销部');
-- 9.列出薪金高于公司平均薪金的所有员工。
select ename from emp where sal>(select avg(sal) from emp);
-- 10.列出与“万古”从事相同工作的所有员工。
select ename from emp where job=(select job from emp where ename='万古');
-- 11.列出薪金高于在部门3工作的所有员工的薪金的员工姓名和薪金。
select ename,sal from emp where sal>(select max(sal) from emp where deptno=3);
-- 12.列出在每个部门工作的员工数量、平均工资
select (select b.dname from dept b where a.deptno=b.deptno)
as deptname ,count(deptno) as deptcount,avg(sal) as deptavgsal
from emp a group by deptno;
-- 14.列出所有员工的姓名、部门名称和工资。
select a.ename,(select b.dname from dept b where b.deptno=a.deptno)
as deptname,sal from emp a;
-- 15.列出所有部门的详细信息和部门人数。
select a.deptno,a.dname,a.loc,(select count(deptno) from emp b
where b.deptno=a.deptno group by b.deptno) as deptcount from dept a;
-- 16.列出各种工作的最低工资。
select job,avg(sal) from emp group by job;
-- 17.列出各个部门的MANAGER(经理)的最低薪金
select deptno,min(sal) from emp where job='经理' group by deptno;
-- 18.列出所有员工的年工资,按年薪从低到高排序
select ename,(sal+NULLIF(comm,0))*12 as salpersal from emp order by salpersal;