標籤:sql server 查詢
相關表:
<span style="white-space:pre">create table DEPT(<span style="white-space:pre"></span>deptno int not null primary key,<span style="white-space:pre"></span>dname varchar(14) null,<span style="white-space:pre"></span>loc varchar(13) null)create table EMP(<span style="white-space:pre"></span>empno int not null primary key,<span style="white-space:pre"></span>ename varchar(10) null,<span style="white-space:pre"></span>job varchar(9) null,<span style="white-space:pre"></span>mgr int null,<span style="white-space:pre"></span>hiredate date null,<span style="white-space:pre"></span>sal float null,<span style="white-space:pre"></span>comm float null,<span style="white-space:pre"></span>deptno int null foreign key references DEPT(deptno),)create table SALGRADE(<span style="white-space:pre"></span>grade int null,<span style="white-space:pre"></span>losal int null,<span style="white-space:pre"></span>hisal int null,)</span>
查詢部分:
--查詢僱員姓名的最後三個字母SELECT SUBSTRING(ename,LEN(ename)-3,3) FROM dbo.EMP--查詢10部門僱員進入公司的星期數SELECT ename,DATEDIFF(wk,hiredate,GETDATE())AS 進入星期數 FROM dbo.EMP WHERE deptno=10--1 查詢部門30中的所有員工SELECT * FROM dbo.EMP WHERE deptno=30--2 列出所有辦事員(CLERK)的姓名,編號和部門編號SELECT ename , empno , deptno FROM emp WHERE job='clerk'--3 找出傭金高於薪金的員工SELECT * FROM dbo.EMP WHERE comm>sal--求出每個僱員的年薪SELECT ename,sal*12+ISNULL(comm,0)*12 AS 年薪 FROM dbo.EMP--4 找出傭金高於薪金的60%的員工SELECT * FROM dbo.EMP WHERE comm>sal*0.6--5 找出部門10中所有經理(MANAGER)和部門20中所有辦事員(CLERK)的詳細資料SELECT * FROM dbo.EMP WHERE deptno=10 AND job='manager' OR deptno=20 and job='CLERK'--6 -- 既不是經理又不是辦事員但薪金大於或等於2000的所有員工的詳細資料SELECT * FROM dbo.EMP WHERE job!='manager' AND job!='clerk' AND sal>=2000--7 找出收取傭金的員工的不同工作SELECT DISTINCT job FROM dbo.EMP WHERE comm IS NOT NULL--8 找出不收取傭金或收取傭金低於100的員工SELECT * FROM dbo.EMP WHERE comm IS NULL OR comm<100--9 找出各月倒數第3天受雇的所有員工SELECT * FROM dbo.EMP WHERE MONTH(DATEADD(DAY,3,hiredate))=MONTH(DATEADD(MONTH,1,hiredate))--10 找出早於12年前受雇的員工SELECT * FROM dbo.EMP WHERE DATEDIFF(YEAR,hiredate,GETDATE())>12--11 以首字母大寫的方式顯示所有員工的姓名select upper(substring(ename,1,1))+lower(substring(ename,2,len(ename)-1)) from emp;--12 顯示正好為5個字元的員工的姓名SELECT ename FROM emp WHERE LEN(ename)=5--13 顯示不帶有"R"的員工的姓名SELECT ename FROM dbo.EMP WHERE ename NOT LIKE '%R%'--14 顯示所有員工的姓名的前三個字元SELECT SUBSTRING(ename,1,3) FROM dbo.EMP--15 顯示所有員工的姓名,用"a"代替所有的"A"SELECT REPLACE(ename,'a','A') FROM dbo.EMP--16 顯示滿10年服務年限的員工的姓名和受雇日期SELECT ename,hiredate FROM dbo.EMP WHERE DATEDIFF(year,hiredate,GETDATE())>10--17 顯示員工詳細資料,按姓名排序SELECT * FROM dbo.EMP ORDER BY ename--18 顯示員工的姓名和受雇日期,根據其服務年限,將最老的員工排在前面SELECT ename,hiredate FROM dbo.EMP ORDER BY DATEDIFF(year,hiredate,GETDATE()) DESC--19 顯示所有員工的姓名,工作和薪金,按工作的降序排序,若工作相同則薪金排序SELECT ename,job ,sal+ISNULL(comm,0)AS 薪金 FROM dbo.EMP order BY job ,sal+ISNULL(comm,0)--20 顯示所有員工的姓名,加入公司的年份和月份,按受雇日期所在月排序,若月份相同則將--最早年份的員工排在前面SELECT ename,YEAR(hiredate),MONTH(hiredate) FROM dbo.EMP ORDER BY MONTH(hiredate),YEAR(hiredate) --21 顯示在一個月為30天的情況,所有員工的日薪金,忽略餘數SELECT ROUND((sal+ISNULL(comm,0))/30,0) FROM dbo.EMP--22 找出在(任何年份的)2月受雇的所有員工SELECT * FROM dbo.EMP WHERE MONTH(hiredate)=2--23 對於每個員工,顯示其加入公司的天數SELECT ename ,DATEDIFF(DAY,hiredate,GETDATE())AS 入職天數 FROM dbo.EMP--24 顯示姓名欄位的任何位置包含"A"的所有員工的姓名SELECT ename FROM dbo.EMP WHERE ename LIKE '%A%'--25 以年月日的方式顯示所有員工的服務年限(大概)SELECT STR(ROUND(DATEDIFF(day,hiredate,GETDATE())/365,0))+'年'+str(ROUND(DATEDIFF(day,hiredate,GETDATE())%365/30+1,0))+'月'+STR(DATEDIFF(day,hiredate,GETDATE())%365%30)+'日' AS 服務年限 FROM dbo.EMP