查詢所有的部門編號
select deptno from dept
查詢所有有人的部門編號
select deptno from emp
查詢所有崗位的名稱
select DISTINCT job from emp
查詢所有薪水超過兩千的員工信息
select * from emp where sal >2000
查詢所有20部門的員工姓名,編號及薪水
select ename,sal,empno from emp where deptno = 20
查詢所有沒有獎金的員工信息
select * from emp where comm is null
查詢所有有獎金的員工信息
select * from emp where comm is not null
查詢最高領(lǐng)導(dǎo)的員工信息
select * from emp where mgr is null
查詢所有81年之后入職的員工信息
select * from emp where hiredate >= "1981-01-01"
查詢所有薪水在2000-4000范圍內(nèi)的員工信息
select * from emp where sal between 2000 and 4000
查詢所有部門編號是10或30的員工信息
select * from emp where deptno = 10 or deptno = 30
select * from emp where deptno in (10, 30 )
查詢所有20部門并且薪水超過2000的員工信息
select * from emp where deptno = 20 and sal > 2000
查詢所有薪水不在2000-4000范圍內(nèi)的員工信息
select * from emp where sal not between 2000 and 4000
查詢所有部門編號不是10辜昵, 30 的員工信息
select * from emp where deptno not in (10,30)
查詢用戶名為scott的員工信息:注意區(qū)分大小寫
SELECT * from emp where ename = "SCOTT"
查詢姓名里面包含ALL的員工姓名
select ename from emp where ename like "%ALL%"
.查詢所有以”S”開頭的同學(xué)
select ename from emp where ename like "S%"
查詢第二個字母為A的員工姓名
select ename from emp where ename like "_A%"
查詢所有員工的編號客冈、姓名泽论、部門編號、職位奢浑、薪水慎陵,按照薪水降序排列
select empno,ename,deptno,job,sal from emp order by sal desc
查詢所有員工信息,按照部門降序排列马胧,部門內(nèi)按照薪水升序排列
select * from emp order by deptno desc , sal asc
查詢姓名中包含‘A’員工的姓名,編號肛走,薪水漓雅,按照薪水降序排列
select ename,empno,sal from emp where ename like "%A%" ORDER BY sal desc
select * from emp
查詢年收入超過10000的員工的姓名录别,編號朽色,薪水,年收入组题,按照年收入降序排列
select ename,sal,(sal+ comm)12 as newsal from emp where (sal+ comm)12 >10000 order by newsal desc
查詢年薪超過10000的員工的姓名葫男,編號,薪水崔列,年收入梢褐,按照年薪降序排列
SELECT ENAME,EMPNO,SAL,SAL12 AS YEARSAL FROM EMP WHERE SAL12 > 10000 ORDER BY YEARSAL DESC;
查詢雇員表中旺遮,姓名為SMITH的雇員,截止到今天共工作了多少周盈咳,則可以使用如下的SQL語句
SELECT ROUND(DATEDIFF(SYSDATE(),hiredate)/7) FROM EMP WHERE ENAME = "SMITH";
時間差函數(shù):datediff
語法:傳入兩個日期參數(shù)耿眉,比較DAY天數(shù),第一個參數(shù)減去第二個參數(shù)的天數(shù)值
SELECT DATEDIFF('2013-01-13','2012-10-01'); # 104
一鱼响、時間差函數(shù):timestampdiff
語法:timestampdiff(interval, datetime1,datetime2)
結(jié)果:返回(時間2-時間1)的時間差鸣剪,結(jié)果單位由interval參數(shù)給出。
frac_second 毫秒(低版本不支持丈积,用second筐骇,再除于1000)
second 秒
minute 分鐘
hour 小時
day 天
week 周
month 月
quarter 季度
year 年
注意:MySQL 5.6之后才支持毫秒的記錄和計(jì)算,如果是之前的版本江滨,最好是在數(shù)據(jù)庫除datetime類型之外的字段铛纬,再建立用于存儲毫秒的int字段,然后自己進(jìn)行轉(zhuǎn)換計(jì)算唬滑。
復(fù)制代碼
所有格式
SELECT TIMESTAMPDIFF(FRAC_SECOND,'2012-10-01','2013-01-13'); # 暫不支持
SELECT TIMESTAMPDIFF(SECOND,'2012-10-01','2013-01-13'); # 8985600
SELECT TIMESTAMPDIFF(MINUTE,'2012-10-01','2013-01-13'); # 149760
SELECT TIMESTAMPDIFF(HOUR,'2012-10-01','2013-01-13'); # 2496
SELECT TIMESTAMPDIFF(DAY,'2012-10-01','2013-01-13'); # 104
SELECT TIMESTAMPDIFF(WEEK,'2012-10-01','2013-01-13'); # 14
SELECT TIMESTAMPDIFF(MONTH,'2012-10-01','2013-01-13'); # 3
SELECT TIMESTAMPDIFF(QUARTER,'2012-10-01','2013-01-13'); # 1
SELECT TIMESTAMPDIFF(YEAR,'2012-10-01','2013-01-13'); # 0
復(fù)制代碼
二告唆、時間差函數(shù):datediff
語法:傳入兩個日期參數(shù),比較DAY天數(shù)晶密,第一個參數(shù)減去第二個參數(shù)的天數(shù)值悔详。
SELECT DATEDIFF('2013-01-13','2012-10-01'); # 104
三、時間差函數(shù):timediff
語法:timediff(time1,time2)
結(jié)果:返回兩個時間相減得到的差值惹挟,time1-time2
SELECT TIMEDIFF('2018-05-21 14:51:43','2018-05-19 12:54:43');
49:57:00
四茄螃、其他日期函數(shù)
now()函數(shù)返回的是當(dāng)前時間的年月日時分秒
curdate()函數(shù)返回的是年月日信息
curtime()函數(shù)返回的是當(dāng)前時間的時分秒信息
對一個包含年月日時分秒日期格式化成年月日日期,可以使用DATE(time)函數(shù)
復(fù)制代碼
其他日期函數(shù)
SELECT NOW(); # 2018-05-21 14:41:00
SELECT CURDATE(); # 2018-05-21
SELECT CURTIME(); # 14:41:38
SELECT DATE(NOW()); # 2018-05-21
SELECT SYSDATE(); # 2018-05-21 14:47:11
SELECT CURRENT_TIME(); # 14:51:30
SELECT CURRENT_TIMESTAMP; # 2018-05-21 14:51:37
SELECT CURRENT_TIMESTAMP(); # 2018-05-21 14:51:43
復(fù)制代碼
查詢各部門的最高薪水连锯、最低薪水归苍、平均薪水….
select max(sal),MIN(sal),avg(sal) from emp
查詢‘SMITH’的領(lǐng)導(dǎo)姓名
select ename from emp where empno = (select mgr from emp where ename = "SMITH")
查詢部門名稱是‘SALES’的員工信息
select * from emp where deptno = (select deptno from dept where dname="SALES")
select * from dept
查詢公司中薪水最高的員工信息
select * from emp where sal = (select max(sal) from emp )
查詢公司所有員工的個數(shù)
select count(ename) from emp
查詢公司中最高薪水是多少
select max(sal) from emp
31查詢公司中平均獎金是多少
select avg(nvl(comm,0)) from emp
32.查詢公司中最晚入職的時間
SELECT max(HIREDATE) FROM EMP ;
33.查詢公司中有獎金的人數(shù)
SELECT count(ENAME) FROM EMP WHERE (COMM IS NOT NULL) ;
34.查詢20部門的最高薪水是多少
SELECT max(SAL) FROM EMP WHERE DEPTNO = 20 ;
35.查詢各部門的平均薪水及部門編號,部門名稱运怖。
SELECT avg(SAL),DNAME,EMP.DEPTNO FROM EMP,DEPT where EMP.DEPTNO = DEPT.DEPTNO GROUP BY DNAME,EMP.DEPTNO;
36.查詢各部門中最高薪水的員工編號拼弃,姓名…
select empno , ename,DEPTNO from emp WHERE SAL in (select max(SAL) from EMP GROUP BY DEPTNO);
37.查詢所有員工姓名中包含‘A’的最高薪水
SELECT max(SAL) FROM EMP WHERE ENAME LIKE "%A%" ;
38.查詢各崗位的最高薪水,最低薪水摇展。要求只統(tǒng)計(jì)薪水>1000的
SeLECT max(SAL),min(SAL) FROM EMP WHERE SAL > 1000 GROUP BY job;
39.查詢各部門的平均薪水及部門編號鲫售,要求只列出平均薪水>2000
SELECT AVG(SAL),DEPtNO FROM EMP GROUP BY DEPTNO HAVING AVG(SAL) > 2000;
40.查詢各部門的平均薪水及部門編號,要求只有員工姓名中包含
‘A’才參與統(tǒng)計(jì)咱圆,只列出平均薪水>1500的卦羡,按照平均薪水降序排列
SELECT AVG(SAL),DEPTNO FROM EMP where ENAME LIKE "%A%" GROUP BY DEPTNO HAVING AVG(SAL) > 1500 ORDER BY AVG(SAL) DESC;
查詢薪水大于該部門平均薪水的員工信息
select * from emp e1 where sal > ( select avg(sal) from emp e2 where e1.deptno=DEPTNO);
查詢最高薪水的員工信息
SELECT * FROM EMP WHERE SAL = (SELECT MAX(SAL) FROM EMP )
查詢‘SMITH’的領(lǐng)導(dǎo)姓名
select ename from emp where empno= (select mgr from emp where ename="SMITH")
select * from emp
select * from dept
select * from salgrade
查詢部門名稱是‘SALES’的員工信息
select * from emp where deptno = (select deptno from dept where dname = "SALES")
查詢公司中薪水最高的員工信息
SELECT * FROM EMP WHERE SAL = (SELECT MAX(SAL) FROM EMP )
49查詢薪水等級為4的員工信息
select * from emp where sal between (select losal from salgrade where grade = 4) and (select hisal from salgrade where grade = 4)
50.查詢領(lǐng)導(dǎo)者是‘BLAKE’的員工信息
select * from emp where MGR = (select EMPNO from emp where ENAME = "BLAKE");
51.查詢最高領(lǐng)導(dǎo)者的薪水等級
select grade FROM SALGRADE where (SELECT SAL from emp where mgr is NULL) BETWEEN LOSAL AND HISAL;
52.查詢薪水最低的員工信息
select * from emp where SAL = (select MIN(sal) from emp);
53.查詢和SMITH工作相同的員工信息
select * from emp where JOB = (select JOB from emp where ENAME = "SMITH") ;
54.查詢不是領(lǐng)導(dǎo)的員工信息
select * from emp e1 where not EXISTS
(select * from emp e2 where e2.mgr = e1.empno);
55.查詢平均工資比10部門低的部門編號
select deptno from emp group by deptno having avg(sal) <
(select avg(sal) from emp where deptno = 10);
56.查詢在紐約工作的所有員工
select * from EMP where DEPTNO = (select DEPTNO from DEPT where LOC = ‘NEW YORK’);
57.查詢‘SALES’部門平均薪水的等級
select grade from SALGRADE where (select AVG(SAL) from emp where DEPTNO = (select DEPTNO FROM DEPT WHERE dname = "SALES") ) BETWEEN LOSAL AND HISAL;
58.查詢10號部門的員工在整個公司中所占的比例:
select (select COUNT(DEPTNO) from EMP where DEPTNO = 10) / (select COUNT(*) from EMP) FROM dual;
60.查詢各部門工資大于該部門平均工資的員工信息:
select * from emp e1 where sal > ( select avg(sal) from emp e2 where e1.deptno = deptno );
61.查詢各崗位工資小于該崗位平均工資的員工信息;
select * from emp e1 where sal < (select avg(sal) from emp e2 where e1.job= job group by job);
62.查詢所有領(lǐng)導(dǎo)的信息:要求使用exists關(guān)鍵字
select * from emp e1 where EXISTS
(select * from emp e2 where e2.mgr = e1.empno);
63.查詢所有員工的姓名祟滴,薪水振惰,部門名稱
select ename,sal,dname from emp,dept where EMP.DEPTNO = DEPT.DEPTNO;
64.查詢所有員工的姓名,薪水垄懂,部門名稱,薪水等級
select ename,sal,dname,grade from emp,dept,SALGRADE
where EMP.DEPTNO = DEPT.DEPTNO and SAL BETWEEN LOSAL AND HISAL;
65.查詢員工姓名及領(lǐng)導(dǎo)者姓名
select a.ename AS 員工姓名 ,b.ename AS 領(lǐng)導(dǎo)姓名
from emp a LEFT JOIN emp b on a.mgr = b.empno;
66.查詢所有員工的姓名骑晶,部門名稱
select ename, dname from emp ,dept where EMP.deptno = DEPT.deptno;
查詢員工表中工資大于1600的員工的姓名和工資
select ename,sal from emp where sal > 1600
查詢員工表中員工號是17的員工的姓名和部門編號
select ename,deptno from emp where empno = 17
選擇員工表中工資不在4000到5000內(nèi)的員工的姓名和工資
select ename,sal from emp where sal not between 4000 and 5000
選擇員工表中在20和30部門工作的員工的姓名和部門號
select ename,deptno from emp where deptno in (20,30)
選擇員工表中沒有管理者的員工姓名及職位痛垛,按職位排序
select ename,job from emp where mgr is null order by job asc
選擇員工表中有獎金的員工姓名,工資和獎金桶蛔,按工資倒序排列
select ename,sal,comm from emp where comm is not null order by sal desc
選擇員工表中員工姓名的第三個字母是A的員工姓名
select ename from emp where ename like "__A%";
列出部門表中的部門名稱和所在城市
select dname,loc from dept
顯示員工表中的不重復(fù)的崗位job
select DISTINCT job from emp
連接員工表中的員工姓名匙头、職位、薪水仔雷,列之間用逗號連接乾胶,列頭顯示成out_put 用concat#####################
select CONCAT(ename,",",job, "," ,sal) AS ename_job_sal from emp
查詢員工表中員工號,姓名朽寞,工資识窿,以及工資提高百分之20之后的結(jié)果
select empno ,ename,sal ,sal*1.2 as newsal from emp
查詢員工的姓名和工資數(shù),條件限定為工資數(shù)必須大于1200脑融,并且查詢結(jié)果按入職時間進(jìn)行排序喻频。早入職的員工排在前面
select empno ,sal,HIREDATE from emp where sal > 1200 ORDER BY HIREDATE
列出除了ACCOUNTING部門之外還有什么部門
select dname from dept where dname != "ACCOUNTING";
把雇員按部門分組,求最高薪水肘迎,部門號 要求過濾掉名字中第二個字母是’A’的員工甥温, 并且部門的平均薪水 > 3000,按照部門編號倒序排列
select MAX(sal),deptno from emp where ename not like "_A%" GROUP BY deptno having avg(sal) > 3000 ;
求工作職位是’manager’的員工姓名,部門名稱和薪水等級
select ename ,dname,grade from emp,dept,salgrade where job = "MANAGER" and (sal BETWEEN LOSAL and HISAL) and EMP.DEPTNO = DEPT.DEPTNO;
按照部門分組統(tǒng)計(jì)妓布,求最高薪水姻蚓,平均薪水,最低薪水匣沼,只有薪水是1200以上的員工才參與統(tǒng)計(jì)狰挡,
并且分組結(jié)果中只包含平均薪水在1500以上的部門,并且按照平均薪水倒序排列
select max(sal),min(sal),avg(sal) from emp where sal > 1200 group by deptno having avg(sal) > 1500 order by avg(sal) DESC;
求薪水最高的員工姓名
select ename from emp where sal = (select max(sal) from emp);
查詢各部門平均薪水等級释涛,并且按平均薪水等級的降序排列
select grade from salgrade s join (select avg(sal) avg_sal from emp e group by deptno) temp on TEMP.avg_sal between s.LOSAL and s.HISAL;
查詢所有員工姓名以S或s開頭的所有員工信息
select * from emp where ename like "S%" or ename like "s%"
查詢20部門的所有員工的員工姓名加叁,實(shí)際收入
select ename,sal + ifnull(comm, 0) from emp where deptno = 20 ;
查詢10部門工資大于3000的員工信息,要求按員工的入職時間由前到后排序
select * from emp where deptno = 10 and sal > 3000 ORDER BY HIREDATE
查詢10部門或20部門的所有員工的姓名唇撬,并截取前三位它匕,按員工姓名升序排列
select substring(ename, 1, 3) from emp where deptno in (10,20) ORDER BY ename
查詢部門名稱是’ACCOUNTING’的員工姓名及薪水等級
select ename,grade from emp ,salgrade ,dept where dname="ACCOUNTING" and (sal between LOSAL and HISAL) and (EMP.deptno = DEPT.deptno)