use day0202;-- 1.列出至少有一個員工的所有部門。#查詢所有的部門總數大於1的部門;select deptno from emp groupby deptno havingcount(deptno)>1;#查詢對應的部門名稱select dname from dept emp where deptno in(select deptno from emp groupby deptno havingcount(deptno)>1);-- 2.列出薪金比"劉一"多的所有員工。#查詢劉一的薪資select sal from emp where ename="劉一";select*from emp where sal>(select sal from emp where ename="劉一");-- 3.***** 列出所有員工的姓名及其直接上級的姓名。#思路:先查看對應的員工id與上級idselect empno,mgr from emp ;#查詢員工姓名查詢上級的idselect emp.ename as 上司 , e.ename as 員工 from emp,(select empno,mgr,ename from emp )as e where emp.empno =e.mgr;#思路反思:#1.根據員工表查詢員工的id上司的id 姓名#2.建立條件讓找到上司id對應的員工#精簡答案select e1.ename,e2.ename from emp as e1,emp as e2
where e1.mgr=e2.empno;-- 4.列出受僱日期早於其直接上級的所有員工。select e1.ename,e2.ename from emp as e1 innerjoin emp as e2 on e1.empno=e2.mgr;select e2.*from emp as e1 innerjoin emp as e2 on e1.empno=e2.mgr where e1.hiredate<e2.hiredate;-- 5.列出部門名稱和這些部門的員工信息,同時列出那些沒有員工的部門。select*from dept as d leftjoin emp as e on d.deptno=e.deptno
-- 6.列出所有job爲“職員”的姓名及其部門名稱。select e.ename ,d.dname from emp as e innerjoin dept as d on d.deptno=e.deptno where job="職員";-- 7.列出最低薪金大於1500的各種工作。select job from emp groupby job havingmin(sal)>1500;-- 8.列出在部門 "銷售部" 工作的員工的姓名,假定不知道銷售部的部門編號。select deptno from dept where dname ="銷售部";select ename as 姓名 ,empno as 編號 from emp where deptno in(select deptno from dept where dname ="銷售部");-- 9.列出薪金高於公司平均薪金的所有員工。selectavg(sal)from emp ;select*from emp where sal>(selectavg(sal)from emp);-- 10.列出與"周八"從事相同工作的所有員工。#創建driver Manger的註冊驅動#使用connection創建連接urlusernamepassword#向數據庫發送sql語句#select job from emp where ename="周八";select*from emp where job in(select job from emp where ename="周八");-- 11.列出薪金等於部門30中員工的薪金的所有員工的姓名和薪金。select ename , sal from emp where deptno=30;-- 12.列出薪金高於在部門30工作的所有員工的薪金的員工姓名和薪金。selectmax(sal)from emp where deptno =30;select ename ,sal from emp where sal>(selectmax(sal)from emp where deptno =30);-- 13.列出在每個部門工作的員工數量、平均工資。selectcount(deptno),avg(sal)from emp groupby deptno havingavg(sal);-- 14.列出所有員工的姓名、部門名稱和工資。select e.ename,d.dname,e.sal from emp as e innerjoin dept as d on e.deptno=d.deptno ;-- 15.列出所有部門的詳細信息和部門人數。select deptno,count(deptno)from emp groupby deptno havingcount(deptno);select d.*,e.c from dept as d leftjoin(select deptno as dd,count(deptno)as c from emp groupby deptno havingcount(deptno))as e on d.deptno=e.dd;-- 16.列出各種工作的最低工資。select job,min(sal)from emp groupby job havingmin(sal);-- 17.列出各個部門的 經理 的最低薪金。select deptno ,min(sal)from emp where job ="經理"groupby deptno;select*from dept innerjoin(select deptno ,min(sal)from emp where job ="經理"groupby deptno)as e on e.deptno =dept.deptno;-- 18.列出所有員工的年工資,按年薪從低到高排序。 select*,sal*12+IFNULL(comm,0)*12as 年薪 from emp ;-- 19.查出emp表中薪水在3000以上(包括3000)的所有員工的員工號、姓名、薪水。select empno , ename ,sal from emp where sal >=3000;-- 20.查詢出所有薪水在'陳二'之上的所有人員信息。select sal from emp where ename ="陳二";select*from emp where sal>(select sal from emp where ename ="陳二");-- 21.查詢出emp表中部門編號爲20,薪水在2000以上(不包括2000)的所有員工,顯示他們的員工號,姓名以及薪水,以如下列名顯示:員工編號 員工名字 薪水select*from emp where deptno=20and sal>2000;-- 22.查詢出emp表中所有的工作種類(無重複)selectDISTINCT job from emp;-- 23.查詢出所有獎金(comm)字段不爲空的人員的所有信息。select*from emp where comm isnotnull;-- 24.查詢出薪水在800到2500之間(閉區間)所有員工的信息。(注:使用兩種方式實現and以及between and)select*from emp where sal between800and2500;select*from emp where sal>=800and sal<2500;-- 25.查詢出員工號爲7521,7900,7782的所有員工的信息。(注:使用兩種方式實現,or以及in)select*from emp where empno in(7521,7900,7782);select*from emp where empno =7521or empno=7900or empno=7782;-- 26.查詢出名字中有“張”字符,並且薪水在1000以上(不包括1000)的所有員工信息。select*from emp where ename like"%張%"and sal>1000;-- 27.查詢出名字第三個漢字是“忌”的所有員工信息。select*from emp where ename like"__忌";-- 28.將所有員工按薪水升序排序,薪水相同的按照入職時間降序排序。select*from emp ORDERBY sal asc,hiredate desc;-- 29.將所有員工按照名字首字母升序排序,首字母相同的按照薪水降序排序。 order by convert(name using gbk) asc; select*from emp orderbyconvert(ename USING gbk)asc,sal desc;-- 30.查詢出最早工作的那個人的名字、入職時間和薪水。selectmin(hiredate)from emp;select ename ,hiredate ,sal from emp where hiredate in(selectmin(hiredate)from emp);-- 31.顯示所有員工的名字、薪水、獎金,如果沒有獎金,暫時顯示100.#不會寫,判斷爲空加100select ename ,sal ,comm+100from emp where comm ifnull;select ename,sal,ifnull(comm,100)from emp;-- 32.顯示出薪水最高人的職位。selectmax(sal)from emp;select job ,ename from emp where sal in(selectmax(sal)from emp);-- 33.查出emp表中所有部門的最高薪水和最低薪水,部門編號爲10的部門不顯示。select deptno,min(sal),max(sal)from emp groupby deptno HAVING deptno<>10;-- 34.刪除10號部門薪水最高的員工。selectmax(sal)from emp where deptno=10;select empno from emp where sal=(selectmax(sal)from emp where deptno=10);deletefrom emp where empno=(select empno from emp where sal=(selectmax(sal)from emp where deptno=10));#標準答案select empno from emp
where deptno=10and sal>=all(select sal from emp where deptno=10);deletefrom emp
where empno =(select empno from(select empno from emp
where deptno=10and sal>=all(select sal from emp where deptno=10))astemp);-- 35.將薪水最高的員工的薪水降30%。selectmax(sal)*(1-0.3)from emp ;select empno from emp
where sal >=all(select sal from emp);update emp
set sal=sal*(1-0.3)where empno in(selecttemp.empno from(select empno from emp
where sal >=all(select sal from emp))temp);select*from emp;-- 36.查詢員工姓名,工資和 工資級別(工資>=3000 爲3級,工資>2000 爲2級,工資<=2000 爲1級)-- 語法:case when ... then ... when ... then ... else ... endselect ename ,sal,casewhen sal>3000then"3級"when sal>2000then"2級"else"1級"endfrom emp;select ename,casewhen sal>3000then'3級'when sal>2000then'2級'else'1級'endfrom emp;