sqlserver 常用的練習50例子


CREATE TABLE EMP
(
EMPNO numeric(5,0) NOT NULL primary key,--雇員的編號
ENAME nvarchar(10) not null,--雇員的名字
JOB nvarchar(9)not null,--雇員的的職位
MGR numeric(5,0),--上級主管編號
HIREDATE datetime,--入職(受雇)日期
SAL numeric(7, 2),--薪金;
COMM numeric(7, 2),--佣金;
DEPTNO numeric(2,0)--部門編號
)

CREATE TABLE DEPT
(
DEPTNO numeric(2) primary key,--部門編號
DNAME nvarchar(14) not null,--部門名稱
LOC部門所在地 nvarchar(13) --部門所在地
);

INSERT INTO EMP VALUES (7369, 'SMITH', 'CLERK', 7902,'2000-12-17', 800, NULL, 20);
INSERT INTO EMP VALUES (7499, 'allen', 'SALESMAN', 7698,'2001-2-20', 1600, 300, 30);
INSERT INTO EMP VALUES (7521, 'WARD', 'SALESMAN', 7698,'2001-2-22', 1250, 500, 30);
INSERT INTO EMP VALUES (7566, 'JONES', 'MANAGER', 7839,'2001-4-2', 2975, NULL, 20);
INSERT INTO EMP VALUES (7654, 'MARTIN', 'SALESMAN', 7698,'2001-9-28',1250, 1400, 30);
INSERT INTO EMP VALUES (7698, 'BLAKE', 'MANAGER', 7839,'2001-5-1', 2850, NULL, 30);
INSERT INTO EMP VALUES (7782, 'CLARK', 'MANAGER', 7839,'2001-6-9', 2450, NULL, 10);
INSERT INTO EMP VALUES (7788, 'scott', 'ANALYST', 7566,'2002-12-9',3000, NULL, 20);
INSERT INTO EMP VALUES (7839, 'king', 'PRESIDENT', NULL,'2001-11-17',5000, NULL, 10);
INSERT INTO EMP VALUES (7844, 'TURNER', 'SALESMAN', 7698,'2001-9-8', 1500, 0, 30);
INSERT INTO EMP VALUES (7876, 'ADAMS', 'CLERK', 7788,'2003-1-12',1100, NULL, 20);
INSERT INTO EMP VALUES (7900, 'JAMES', 'CLERK', 7698,'2001-3-12',950, NULL, 30);
INSERT INTO EMP VALUES (7902, 'FORD', 'ANALYST', 7566,'2001-3-12',3000, NULL, 20);
INSERT INTO EMP VALUES (7934, 'MILLER', 'CLERK', 7782,'2002-01-23',1300, NULL, 10);
INSERT INTO DEPT VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO DEPT VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO DEPT VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO DEPT VALUES (40, 'OPERATIONS', 'BOSTON');

SELECT *FROM EMP
--1、查詢所有的雇員名字
SELECT ENAME FROM EMP

--2、查詢所有的職位
SELECT DISTINCT JOB FROM EMP --DISTINCT 隱藏重復的行

--3、查詢沒有佣金(COMM)的所有雇員信息
select * from EMP where COMM is null

--4、查詢薪金(SAL)和佣金(COMM)總數大於2000的所有雇員信息
select * from EMP where (SAL+COMM)>2000  --cuo

SELECT * FROM EMP
WHERE SAL+ISNULL(COMM,0)>2000
--提示:isnull(列名,0) :如果該列中有空值,就把空值當做0做計算

--5、選擇部門編號=30中的雇員
select * from EMP  where EMPNO=30

--6、列出所有Job辦事員("CLERK")的姓名、編號和部門名稱
select EMP.ENAME,EMP.EMPNO,DEPT.DNAME from EMP left join Dept  on EMP.DEPTNO=DEPT.DEPTNO where EMP.Job='CLERK'

--6、列出所有Job辦事員("CLERK")的姓名、編號和部門名稱
SELECT ENAME,EMPNO,DNAME FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND JOB='CLERK'
--7、找出佣金高於薪金的雇員
select * from EMP where ISNULL(COMM,0)>SAL
select * from EMP where COMM>SAL
--8、找出佣金高於薪金的60%的雇員
select * from EMP where ISNULL(COMM,0)/(ISNULL(COMM,0)-SAL)>0.6
SELECT ENAME FROM EMP WHERE COMM>0.6 * SAL
--9、找出部門10中所有經理和部門20中的所有辦事員的詳細資料
select * from EMP where (EMP.DEPTNO=10 and EMP.JOB='MANAGER') or (EMP.DEPTNO=20 and JOB='CLERK')

SELECT *FROM EMP WHERE JOB='MANAGER' AND DEPTNO=10 OR JOB='CLERK' AND DEPTNO =20

--10、既不是經理又不是辦事員但其薪金>=2000的所有雇員的詳細資料
select * from EMP where job !='MANAGER' and job!='CLERK' and SAL>2000
--11、找出收取佣金的雇員的不同工作
--select a.JOB from EMP as a left join EMP as b on a.ENAME=b.ename where 
select DISTINCT  job from  emp  where COMM is not null

select * from  emp  where COMM is not null

--如何查詢某一張表某一字段重復次數,以及重復的字段
select count(*) as count,job from EMP Group by JOB HAVING count(*)>1

--12、找出不收取佣金或收取的佣金低於100的雇員
select * from emp where COMM is null or comm<100
--13、找出早於8年之前受雇的雇員
select * from emp where HIREDATE < DateAdd(yyyy,-8,getdate())
SELECT * FROM EMP WHERE GETDATE()-HIREDATE>8----例子錯誤
--14、顯示首字母大寫的所有雇員的姓名
--區分大小寫:collate:指定排序規則的
--修改表,設置大小寫是否敏感, chinese_prc_ci_as 不區分大小寫
--區分大小寫 chinese_prc_cs_as
SELECT * FROM emp WHERE (ASCII(SUBSTRING(ENAME, 1, 1)) > 64) AND (ASCII(SUBSTRING(ENAME, 1, 1)) < 91)
select * from emp where ENAME collate chinese_prc_cs_as_ws like '[A]%'

--SELECT  UPPER(SUBSTRING('aYAME',1,1))+LOWER(SUBSTRING('aYAME',2,( SELECT LEN('aYAME'))))

--15、顯示正好為5個字符的雇員姓名
select * from emp where LEN(ENAME)=5
--16、顯示帶有'R'的雇員姓名 不區分大小寫
select * from emp where ENAME  like '%R%'
--17、顯示不帶有'R'的雇員姓名
select * from emp where ENAME not like '%R%'
--18、顯示包含"A"的所有雇員的姓名及"A"在姓名字段中的位置
--select emp.ENAME from emp where ENAME
select ename,CHARINDEX('A',ENAME) from emp where ename like '%A%';

--19、顯示所有雇員的姓名,用a替換所有'A'
select REPLACE(ename,'A','a') as ename from emp 
--20、顯示所有雇員的姓名的前三個字符
--1.left(name,4)截取左邊的4個字符
--2.right(name,2)截取右邊的2個字符
--3.SUBSTRING(name,5,3) 截取name這個字段 從第五個字符開始 只截取...
--4.SUBSTRING(name,3) 截取name這個字段 從第三個字符開始,之后的所有個...
--5.SUBSTRING(name, -4) 截取name這個字段的第 4 個字符位置(倒數)..
select left(ename,3) as Ename  from emp
--21、顯示雇員的詳細資料,按姓名排序
select * from emp order by ENAME 
--22、顯示雇員姓名,根據其服務年限,將最老的雇員排在最前面
select ename from emp  order by HIREDATE Asc
--23、顯示所有雇員的姓名、工作和薪金,按工作降序順序排序,
--而工作相同的按薪金升序排序.
select emp.ENAME,EMP.JOB,emp.SAL from emp order by job desc ,SAL ASC   --如果第一個排序條件重復,則繼續用第二個排序
--24、顯示在一個月為30天的情況下所有雇員的日薪金,忽略小數
SELECT ENAME,SAL/30, CAST(SAL/30 AS INT) FROM EMP
--25、找出在(任何年份的)2月受聘的所有雇員
SELECT ENAME,HIREDATE FROM EMP WHERE MONTH(HIREDATE)=2
--26、對於每個雇員,顯示其加入公司的天數
--提示:datediff(day,hiredate,getdate())
--獲取兩個時間的差值.(單位可選)
select ename, DATEDIFF(DAY,emp.HIREDATE,GETDATE()) as tianshu from EMP
--27、列出至少有一個雇員的所有部門
select count(EMP.EMPNO),DEPT.DNAME from emp left join DEPT on EMP.DEPTNO=DEPT.DEPTNO GROUP BY DEPT.DNAME 
 Having  COUNT(EMP.EMPNO)IS NOT NULL

 SELECT DEPTNO,COUNT(EMPNO) 人數 FROM EMP a
GROUP BY DEPTNO
HAVING COUNT(EMPNO) IS NOT NULL
--28、列出各種類別工作的最低工資
select JOB, MIN(sal) AS 最低工資 from emp GROUP BY JOB ORDER BY 最低工資 DESC

SELECT JOB,MIN(SAL+ISNULL(COMM,0)) 最低工資 FROM EMP GROUP BY JOB
--29、列出各個部門的MANAGER(經理)的最低薪金
select min(Sal) from emp where job='MANAGER' --所有的取最低值
SELECT DEPTNO 部門名稱,MIN(SAL) 最低薪金 FROM EMP WHERE JOB='MANAGER' GROUP BY DEPTNO --分組之后 ,取組內最低值
--30、列出薪金高於公司平均水平的所有雇員
select *  from emp Having SAL> AVG(SAL) --cuo
SELECT * FROM EMP WHERE SAL>(SELECT AVG(SAL)FROM EMP)
--31、列出各種工作類別的最低薪金,並顯示最低薪金大於1500
--select job, min(SAL)>1500 from emp  group by job 錯誤
SELECT JOB,MIN(SAL) FROM EMP GROUP BY JOB HAVING MIN(SAL)>1500

--32、顯示所有雇員的姓名和加入公司的年份和月份,按雇員受雇 日所在月排序,將最早年份的項目排在最前面
select ename,emp.HIREDATE from emp 
SELECT ENAME,MONTH(HIREDATE) as 月,YEAR(HIREDATE) as 年 FROM EMP ORDER BY MONTH(HIREDATE) ,YEAR(HIREDATE)ASC
--33、顯示所有雇員的姓名以及滿8年服務年限的日期
select emp.ENAME ,emp.HIREDATE  from emp  where DATEDIFF(YEAR,emp.HIREDATE,GETDATE())>8
--34、顯示所有雇員的服務年限:總的年數或總的月數或總的天數
select *, DATEDIFF(YEAR,emp.HIREDATE,GETDATE()) As 年,DATEDIFF(MONTH,emp.HIREDATE,GETDATE()) As 月,
DATEDIFF(DAY,emp.HIREDATE,GETDATE()) As 天 from emp
--35、列出按計算的字段排序的所有雇員的年薪.即:按照年薪對雇 員進行排序,年薪指雇員每月的總收入總共12個月的累加
--select  emp.ename, sum(sal) as 年薪 from emp where YEAR(EMP.HIREDATE)=2020 group by EMP.ENAME  ling
SELECT ENAME ,SAL*12 FROM EMP ORDER BY SAL*12 ASC
--36、列出年薪前5名的雇員
SELECT TOP 5 ENAME, SAL*12 AS 年薪 FROM EMP ORDER BY SAL*12 DESC
--37、列出年薪低於10000的雇員
select * ,(sal*12) as nianxin from emp where sal*12<10000
--38、列出雇員的平均月薪和平均年薪
select avg(sal)as 平均月薪, AVG(sal*12)as 平均年薪 from emp
SELECT CAST( AVG(SAL)AS INT)AS 平均月薪,CAST( AVG(SAL*12)AS INT)AS 平均年薪 FROM EMP
--39、列出部門名稱和這些部門的雇員,同時列出那些沒有雇員的部門
select d.DNAME,e.ENAME  from dept d left join emp e on d.DEPTNO=e.DEPTNO
SELECT DNAME,ENAME FROM EMP RIGHT JOIN DEPT ON EMP.DEPTNO=DEPT.DEPTNO
--40、列出每個部門的信息以及該部門中雇員的數量
--select count(DEPTNO) from (select d.*  from DEPT d left join emp e on e.DEPTNO=d.DEPTNO) group by d.DEPTNO cuowu

select d.DEPTNO,DNAME,LOC部門所在地,count(d.DEPTNO)As 人數  from DEPT d  left join emp e on e.DEPTNO=d.DEPTNO  group by DNAME,LOC部門所在地,d.DEPTNO
SELECT DEPT.DEPTNO,DNAME,LOC部門所在地,COUNT(EMPNO) 雇員數量 FROM EMP RIGHT JOIN DEPT
ON EMP.DEPTNO=DEPT.DEPTNO
GROUP BY DNAME,LOC部門所在地,DEPT.DEPTNO

--41、列出薪金比"SMITH"多的所有雇員
select * from emp where SAL>(select sal from emp where ename='JONES')
SELECT A.ENAME,A.SAL FROM EMP AS A,EMP AS B WHERE A.SAL>B.SAL AND B.ENAME='JONES'--自連接 當同一列的值相互之間進行比較時
ORDER BY A.SAL ASC

--42、列出所有雇員的姓名及其直接上級的姓名(自連接)
--select a.ename from emp a ,emp b where  a.DEPTNO=b.DEPTNO and 
--select ename,DEPTNO from EMP where job='MANAGER'
select a.ename ,b.ENAME from emp a ,emp b where a.MGR=b.EMPNO
select a.ename ,b.ename from emp a left join emp b on a.MGR=b.EMPNO
--43、列出入職日期早於其直接上級的所有雇員
select a.ename ,b.ename from emp a inner join emp b on a.MGR=b.EMPNO and a.HIREDATE<b.HIREDATE

SELECT A.ENAME FROM EMP AS A,EMP AS B WHERE A.MGR=B.EMPNO AND A.HIREDATE<B.HIREDATE
--44、列出所有辦事員("CLERK")的姓名及其部門名稱
select ename,dept.DNAME from emp inner join DEPT on  EMP.JOB='CLERK' AND emp.DEPTNO=DEPT.DEPTNO
SELECT ENAME,DNAME FROM EMP,DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO AND EMP.JOB='CLERK'
--45、列出從事"SALES"(銷售)工作的雇員的姓名,假定不知道 銷售部的部門編號
select ename from DEPT  join emp on DEPT.DNAME='SALES'  and DEPT.DEPTNO=EMP.DEPTNO
SELECT ENAME FROM EMP,DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO AND DEPT.DNAME='SALES'
--46、列出與"SCOTT"從事相同工作的所有雇員
select  b.ENAME from emp a inner join emp b on a.ename='SCOTT' and  a.JOB=b.job
--47、列出某些雇員的姓名和薪金,條件是他們的薪金等於部門30 中任何一個雇員的薪金

select ename,sal,deptno from emp where sal=any(select sal from emp where DEPTNO=30)
--48、列出某些雇員的姓名和薪金,條件是他們的薪金高於部門30 中所有雇員的薪金
select ename,sal,deptno from emp where sal>all(select sal from emp where DEPTNO=30)
--49、列出從事同一種工作但屬於不同部門的雇員的不同組合
select DISTINCT a.ENAME, b.ENAME from  emp a join emp b on  a.JOB=b.JOB and a.DEPTNO != b.DEPTNO
SELECT DISTINCT A.ENAME, B.ENAME FROM EMP AS A,EMP AS B WHERE A.JOB=B.JOB AND A.DEPTNO!=B.DEPTNO

外增 多列


 

CREATE TABLE [dbo].[course](
    [cno] [int] NOT NULL,
    [cname] [nchar](10) NOT NULL,
    [tno] [nchar](10) NOT NULL,
)
CREATE TABLE [dbo].[sc](
    [sno] [int] NULL,
    [cno] [int] NULL,
    [socre] [float] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[student](
    [sno] [int] NOT NULL,
    [sname] [nchar](10) NOT NULL,
    [sage] [int] NOT NULL,
    [ssex] [nchar](10) NOT NULL,
CREATE TABLE [dbo].[teacher](
    [tno] [nchar](10) NOT NULL,
    [tname] [nchar](10) NULL,
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1001,'english','TS01')
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1002,'math','TS02')
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1003,'art','TS05')
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1004,'dance','TS04')
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1005,'physic','TS06')
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1006,'cheministy','TS08')
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1007,'paint','TS07')
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1008,'panio','TS09')
INSERT INTO [dbo].[course]([cno],[cname],[tno])VALUES(1009,'computer','TS03')
insert into  dbo.sc(sno,cno,socre)values(1,1001,89)
insert into  dbo.sc(sno,cno,socre)values(1,1002,89)
insert into  dbo.sc(sno,cno,socre)values(1,1003,95)
insert into  dbo.sc(sno,cno,socre)values(1,1004,95)
insert into  dbo.sc(sno,cno,socre)values(1,1005,83)
insert into  dbo.sc(sno,cno,socre)values(1,1006,95)
insert into  dbo.sc(sno,cno,socre)values(1,1007,82)
insert into  dbo.sc(sno,cno,socre)values(1,1008,96)
insert into  dbo.sc(sno,cno,socre)values(1,1009,97)
insert into  dbo.sc(sno,cno,socre)values(2,1001,85)
insert into  dbo.sc(sno,cno,socre)values(2,1002,89)
insert into  dbo.sc(sno,cno,socre)values(2,1003,75)
insert into  dbo.sc(sno,cno,socre)values(2,1004,95)
insert into  dbo.sc(sno,cno,socre)values(2,1005,83)
insert into  dbo.sc(sno,cno,socre)values(2,1006,85)
insert into  dbo.sc(sno,cno,socre)values(2,1007,92)
insert into  dbo.sc(sno,cno,socre)values(2,1008,96)
insert into  dbo.sc(sno,cno,socre)values(2,1009,97)
insert into  dbo.sc(sno,cno,socre)values(3,1001,95)
insert into  dbo.sc(sno,cno,socre)values(3,1002,89)
insert into  dbo.sc(sno,cno,socre)values(3,1003,95)
insert into  dbo.sc(sno,cno,socre)values(3,1004,95)
insert into  dbo.sc(sno,cno,socre)values(3,1005,88)
insert into  dbo.sc(sno,cno,socre)values(3,1006,95)
insert into  dbo.sc(sno,cno,socre)values(3,1007,92)
insert into  dbo.sc(sno,cno,socre)values(3,1008,96)
insert into  dbo.sc(sno,cno,socre)values(3,1009,90)
insert into  dbo.sc(sno,cno,socre)values(4,1001,96)
insert into  dbo.sc(sno,cno,socre)values(4,1002,89)
insert into  dbo.sc(sno,cno,socre)values(4,1003,95)
insert into  dbo.sc(sno,cno,socre)values(4,1004,95)
insert into  dbo.sc(sno,cno,socre)values(4,1005,89)
insert into  dbo.sc(sno,cno,socre)values(4,1006,95)
insert into  dbo.sc(sno,cno,socre)values(4,1007,82)
insert into  dbo.sc(sno,cno,socre)values(4,1008,96)
insert into  dbo.sc(sno,cno,socre)values(4,1009,97)
insert into  dbo.sc(sno,cno,socre)values(5,1001,93)
insert into  dbo.sc(sno,cno,socre)values(5,1002,89)
insert into  dbo.sc(sno,cno,socre)values(5,1003,98)
insert into  dbo.sc(sno,cno,socre)values(5,1004,93)
insert into  dbo.sc(sno,cno,socre)values(5,1005,83)
insert into  dbo.sc(sno,cno,socre)values(5,1006,95)
insert into  dbo.sc(sno,cno,socre)values(5,1007,92)
insert into  dbo.sc(sno,cno,socre)values(5,1008,93)
insert into  dbo.sc(sno,cno,socre)values(5,1009,97)
insert into  dbo.sc(sno,cno,socre)values(6,1001,92)
insert into  dbo.sc(sno,cno,socre)values(6,1002,89)
insert into  dbo.sc(sno,cno,socre)values(6,1003,95)
insert into  dbo.sc(sno,cno,socre)values(6,1004,91)
insert into  dbo.sc(sno,cno,socre)values(6,1005,83)
insert into  dbo.sc(sno,cno,socre)values(6,1006,95)
insert into  dbo.sc(sno,cno,socre)values(6,1007,92)
insert into  dbo.sc(sno,cno,socre)values(6,1008,86)
insert into  dbo.sc(sno,cno,socre)values(6,1009,98)
insert into  dbo.sc(sno,cno,socre)values(7,1001,95)
insert into  dbo.sc(sno,cno,socre)values(7,1002,84)
insert into  dbo.sc(sno,cno,socre)values(7,1003,95)
insert into  dbo.sc(sno,cno,socre)values(7,1004,95)
insert into  dbo.sc(sno,cno,socre)values(7,1005,83)
insert into  dbo.sc(sno,cno,socre)values(7,1006,85)
insert into  dbo.sc(sno,cno,socre)values(7,1007,94)
insert into  dbo.sc(sno,cno,socre)values(7,1008,96)
insert into  dbo.sc(sno,cno,socre)values(7,1009,97)
insert into  dbo.sc(sno,cno,socre)values(8,1001,96)
insert into  dbo.sc(sno,cno,socre)values(8,1002,89)
insert into  dbo.sc(sno,cno,socre)values(8,1003,95)
insert into  dbo.sc(sno,cno,socre)values(8,1004,85)
insert into  dbo.sc(sno,cno,socre)values(8,1005,83)
insert into  dbo.sc(sno,cno,socre)values(8,1006,99)
insert into  dbo.sc(sno,cno,socre)values(8,1007,92)
insert into  dbo.sc(sno,cno,socre)values(8,1008,96)
insert into  dbo.sc(sno,cno,socre)values(8,1009,94)
insert into  dbo.sc(sno,cno,socre)values(9,1001,93)
insert into  dbo.sc(sno,cno,socre)values(9,1002,89)
insert into  dbo.sc(sno,cno,socre)values(9,1003,86)
insert into  dbo.sc(sno,cno,socre)values(9,1004,95)
insert into  dbo.sc(sno,cno,socre)values(9,1005,83)
insert into  dbo.sc(sno,cno,socre)values(9,1006,95)
insert into  dbo.sc(sno,cno,socre)values(9,1007,92)
insert into  dbo.sc(sno,cno,socre)values(9,1008,96)
insert into  dbo.sc(sno,cno,socre)values(9,1009,92)
insert into  dbo.sc(sno,cno,socre)values(10,1001,96)
insert into  dbo.sc(sno,cno,socre)values(10,1002,88)
insert into  dbo.sc(sno,cno,socre)values(10,1003,75)
insert into  dbo.sc(sno,cno,socre)values(10,1004,95)
insert into  dbo.sc(sno,cno,socre)values(10,1005,83)
insert into  dbo.sc(sno,cno,socre)values(10,1006,95)
insert into  dbo.sc(sno,cno,socre)values(10,1007,82)
insert into  dbo.sc(sno,cno,socre)values(10,1008,96)
insert into  dbo.sc(sno,cno,socre)values(10,1009,87)

 

 

 

 

 

 插入數據找不到了

--1.查詢課程編號為“1001”的課程比“1002”的課程成績高的所有學生的學號
select a.sno  from sc a, sc b  where a.socre>b.socre and  a.cno=1001 and b.cno=1002 and a.sno=b.sno

select * from sc
--2.查詢平均成績大於60分的學生的學號和平均成績
select sno ,avg(socre) from sc group by sno having AVG(socre)>60

--3.查詢所有學生的學號、姓名、選課數、總成績
select s.sname,s.sno ,sum(socre) ,count(cno) from student s join  sc on s.sno=sc.sno  group by sc.sno,s.sname
--4、查詢姓“李”的老師的個數
select tname, count(1)  from teacher where SUBSTRING(tname,1,1)='' group by tname
--5、查詢沒學過“王海”老師課的學生的學號、姓名
  select sno,sname from student where sno not in(select sno from sc where cno=(select cno from course
  where tno=(select tno  from teacher where tname='王海'))) 

  select sno,sname from student where sno not in(select sno from SC where cno in(select cno from course
where tno in(select tno from teacher where tname='王海')))

--6、查詢學過“王海”老師所教的所有課的同學的學號、姓名
--(對原始表Course,SC稍作修改,讓王海交2門課
insert into course values('1010','Exercise','TS03')
select sno,count(socre) from  sc where socre<60 group by sno  having count(socre)>2
--查詢學生的總成績並進行排名
select  sno ,sum(socre) as s from sc group by sno  order by s desc
--查詢平均成績大於60分的學生的學號和平均成績
select sno ,avg(socre) as pingjun from sc group by sno  having avg(socre)>60; 
--查詢所有課程成績小於60分學生的學號、姓名
select s.sno,s.sname,sc.cno,sc.socre from student s left join sc on s.sno=sc.sno  where sc.socre<60
--查詢沒有學全所有課的學生的學號、姓名|
select s.sno,s.sname  from student s  where  s.sno   in  (select sno from sc    group by sc.sno having count(cno)<(select count(cno) from course))
--查詢出只選修了兩門課程的全部學生的學號和姓名|
select sc.sno,s.sname from student  s left join sc  on s.sno=sc.sno  group by sc.sno  ,s.sname having count(cno)=2
--查找1990年出生的學生名單
--select *  from student  where year()
--查詢所有學生的學號、姓名、選課數、總成績
select  s.sno,s.sname,count(sc.cno) as xuankeshu, sum(sc.socre) as zongchengji from student s left join sc on s.sno=sc.sno group by sc.sno,s.sno,s.sname
--查詢平均成績大於85的所有學生的學號、姓名和平均成績
select  s.sno,s.sname,count(sc.cno) as xuankeshu, sum(sc.socre) as zongchengji ,avg(sc.socre)as pingjun from student s left join sc on s.sno=sc.sno group by sc.sno,s.sno,s.sname  
having  avg(sc.socre)>85
--查詢學生的選課情況:學號,姓名,課程號,課程名稱
select s.sno,s.sname,sc.cno,c.cname from student s inner  join sc on sc.sno=s.sno inner  join course as c on sc.cno=c.cno
--查詢出每門課程的及格人數和不及格人數
select cno, count(sno),'及格' from sc where socre>59 group by cno
union 
select cno, count(sno),'不及格' from sc where socre<60 group by cno
-- 考察case表達式
select cno,sum(case when socre>=60 then 1      else 0     end) as 及格人數,sum(case when socre <  60 then 1 else 0   end) as 不及格人數 from sc  group by cno;
--使用分段[100-85],[85-70],[70-60],[<60]來統計各科成績,分別統計:各分數段人數,課程號和課程名稱
select  sum (case when socre BETWEEN 85 and 100 then 1 else 0 end) as [100-85], 
sum (case when socre>=70 and socre<85  then 1 else 0 end) as [85-70] ,
sum (case when socre >= 60 and socre<70 then 1 else 0 end) as [70-60] ,
sum (case when socre <60 then 1 else 0 end) as [<60] 
from sc ;
--檢索"0001"課程分數小於60,按分數降序排列的學生信息
select s.sno,s.sname,s.sage, sc.socre from student s left join sc on s.sno=sc.sno where sc.cno='1001' and sc.socre<60 order by sc.socre desc
--查詢不同老師所教不同課程平均分從高到低顯示
select t.tname,t.tno,avg(sc.socre) as pingjun from  teacher t inner join course c on t.tno=c.tno inner join sc on c.cno=sc.cno   group by t.tno ,tname  order by pingjun desc
--查詢課程名稱為"數學",且分數低於60的學生姓名和分數
select s.sname,sc.socre from student s inner join sc  on s.sno=sc.sno inner join course c on sc.cno=c.cno where c.cname='math' and sc.socre<60 order by socre --group by c.cname='' 
--查詢任何一門課程成績在70分以上的姓名、課程名稱和分數(與上題類似)
select s.sname,c.cname,sc.socre from student s inner join sc  on s.sno=sc.sno inner join course c on sc.cno=c.cno where sc.socre>70 order by socre desc
--查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select s.sname,s.sno,avg(sc.socre) as pingjun from student s left join sc  on s.sno=sc.sno left join course c on sc.cno=c.cno group by sc.sno,s.sname,s.sno having sum(case when sc.socre<60 then 1 else 0 end ) >=2
--select b.sname, avg(sc.socre)as pingju,sc.sno  from sc ​  inner join student  b ​​on sc.sno =b.sno where sc.socre <60 group by sc.sno, b.sname having count(sc.sno) >=2;  --cuowu 平均成績求錯
--查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
select sc.sno as 學生編號A,sc.cno as 課程編號A,sc.socre as 學生成績A, bsc.sno as 學生編號B,bsc.cno as 課程編號B,bsc.socre as 學生成績B from  sc  left join sc bsc  on sc.sno=bsc.sno where bsc.socre=sc.socre and bsc.cno!=sc.cno 

--查詢課程編號為“0001”的課程比“0002”的課程成績高的所有學生的學號
select sc.sno,sc.socre,sc.cno,bsc.sno,bsc.socre,bsc.cno from sc left join sc as bsc on sc.sno=bsc.sno where sc.cno='1001' and bsc.cno='1002' and sc.socre>bsc.socre
--查詢學過編號為“0001”的課程並且也學過編號為“0002”的課程的學生的學號、姓名
select * from student s left join sc on sc.sno=s.sno   where sc.cno='1001' or sc.cno='1002'
--查詢學過“王海”老師所教的所有課的同學的學號、姓名
select * from student s left join sc on sc.sno =s.sno  left join course c on sc.cno=c.cno left join teacher t on t.tno=c.tno where tname='王海'
--查詢沒學過"王海"老師講授的任一門課程的學生姓名(與上題類似,"沒學過"用not in來實現)
select bs.sno,bs.sname from student bs where  bs.sno not in (select s.sno from student s left join sc on sc.sno =s.sno  left join course c on sc.cno=c.cno left join teacher t on t.tno=c.tno where tname='王海')
--查詢學習“王海”老師所授課程的學生中成績最高的學生姓名及其成績(與上題類似,用成績排名,用 limit 1得出最高一個)
--select s.sname,sc.cno,sc.socre from student s left join sc on sc.sno =s.sno  left join course c on sc.cno=c.cno left join teacher t on t.tno=c.tno where tname='王海' order by sc.socre desc limit 1
--查詢至少有一門課與學號為“0001”的學生所學課程相同的學生的學號和姓名
--select s.sno,s.sname ,* from student s left join sc on sc.sno=s.sno  left join sc bsc on sc.sno=bsc.sno where sc.sno!=bsc.sno
 select sc.sno,sname,cno  from student left join sc on sc.sno=student.sno   where cno in (select sc.cno from  sc  where sc.sno=1) and  sc.sno!=1  --group by sc.sno,sname having count(sc.sno)>0 多此一舉
 --按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
 select sno,  avg(socre)as pingjun  from sc   group by sno,sno order by pingjun desc 
 select sno, max(case when c.cname='math' then sc.socre else null end)as 數學,
  max(case when c.cname='english' then sc.socre else null end)as yingyu, 
   max(case when c.cname='art' then sc.socre else null end)as yishu, 
    max(case when c.cname='dance' then sc.socre else null end)as wudao, 
     max(case when c.cname='physic' then sc.socre else null end)as wuli, 
     max(case when c.cname='cheministy' then sc.socre else null end)as huaxue,
      max(case when c.cname='paint' then sc.socre else null end)as paint, 
       max(case when c.cname='panio' then sc.socre else null end)as panio, 
        max(case when c.cname='computer' then sc.socre else null end)as computer, 
         max(case when c.cname='Exercise' then sc.socre else null end)as Exercise, 
 avg(socre)as  pingjun      from sc  inner join course as c on c.cno=sc.cno    group by  sno order by pingjun desc 

 --查詢學生平均成績及其名次
 --select  sno,avg(socre)as pingjun,row_number() over(order by avg(socre) desc) as mingci from sc  group by sno -- order by pingjun desc
 --查詢每門功課成績最好的前兩名學生姓名
 --select sc.cno,row_number()over(order by avg(socre) desc) as ranking from student s left join sc on sc.sno=s.sno group by sc.cno  where ranking<3  cuowu
 select a.cno,s.sname,a.socre,a.ranking from (select cno ,sno ,socre ,row_number() over(partition by cno order by socre desc) as ranking​ from  sc)as a inner join student s on a.sno =s.sno  where a.ranking<3
 --查詢所有課程的成績第2名到第3名的學生信息及該課程成績(與上一題相似)
  select a.cno,s.sname,a.socre,a.ranking from (select cno ,sno ,socre ,row_number() over(partition by cno order by socre desc) as ranking​ from  sc)as a inner join student s on a.sno =s.sno  where a.ranking in( 2,3) ;
  ---查詢各科成績前三名的記錄(不考慮成績並列情況)(與上一題相似)
select a.cno,s.sname,a.socre,a.ranking from (select cno ,sno ,socre ,row_number() over(partition by cno order by socre desc) as ranking​ from  sc)as a inner join student s on a.sno =s.sno  where a.ranking <4 

 繼續補充

--1.查詢" 01 "課程比" 02 "課程成績高的學生的信息及課程分數
select * from sc a left join sc b on a.sno=b.sno where a.cno='1001'  and b.cno='1002'  and  a.socre>b.socre  
--2.查詢同時存在" 001 "課程和" 002 "課程的學生數據
select s.sno,s.sname,a.cno,b.cno from  student s  left join  sc a on s.sno=a.sno join sc b on a.sno=b.sno where a.cno='1001' and b.cno='1002' 
--3.查詢存在" 01 "課程但可能不存在" 02 "課程的情況(不存在時顯示為 null )
select sno,cno,socre from sc where cno='1001'  and sno not in (select sno from sc where cno='1002')
select *
from (select * from sc where sc.cno = '1001')as t1
left join (select * from sc where sc.cno = '1002')as t2
on t1.sno = t2.sno;
select  *  from sc a left join sc b  on a.sno=b.sno
--4.查詢不存在" 01 "課程但存在" 02 "課程的情況
select * from (select * from sc a where a.cno='1002') as sc1 left join (select * from sc b where b.cno='1001') as sc2  on sc1.sno=sc2.sno 
select * from sc where sc.sno not in (select sno from sc where sc.cno = '1001') and sc.cno = '1002'
--5.查詢平均成績大於等於 60 分的同學的學生編號和學生姓名和平均成績
select s.sno,s.sname ,avg(sc.socre) pingjun from student s left join  sc on s.sno=sc.sno group by  s.sno,s.sname  having avg(sc.socre)>60

--方法一:
select st.sno,st.sname,r.avg_socre
from student as st,(select sno,avg(socre) as avg_socre from sc group by sno having avg(socre) >60) as r
where st.sno = r.sno;
 
--方法二:
select st.sno,st.sname from student as st
right join(select sno,avg(socre) as avg_socre from sc group by sno having avg(socre) >60) as t
on st.sno = t.sno;
 
--方法三:
select t.sno,r.sname,t.avg_socre
from (select sno,avg(socre) as avg_socre  from sc group by sno having avg(socre) >60) as t
left join (select st.sno,st.sname from student as st) as r
on t.sno = r.sno
--————————————————
--版權聲明:本文為CSDN博主「xGuardian」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
--原文鏈接:https://blog.csdn.net/xGuardian/article/details/93167928

--6.查詢所有同學的學生編號、學生姓名、選課總數、所有課程的成績總和
select s.sno,s.sname ,count(sc.sno)as xunekeshu,sum(sc.socre) as zongchengji  from student s left join  sc on sc.sno=s.sno group by s.sno,s.sname  --標准
--聯合查詢不會顯示沒選課的學生:
select st.sno,st.sname,t.cnums,t.socresum
from student as st,(select sno,count(cno) as cnums,sum(socre) as socresum
                    from sc
                    group by sno)as t
where st.sno = t.sno;
 
 
--顯示沒選課的學生(顯示為null),需要使用join
select st.sno,st.sname,t.cnums,t.socresum
from Student as st
left join (select sno,count(sc.cno) as cnums,sum(sc.socre) as socresum
            from sc
            group by sno) as t
on st.sno = t.sno;
 
select s.sno,s.sname,t.cnums,t.socresum
from ((select st.sno,st.sname from student as st) as s
        left join 
        (select sc.sno,count(sc.cno) as cnums,sum(sc.socre) as socresum
        from sc
        group by sc.sno)as t
on s.sno = t.sno)
--7.查詢學過「王海」老師授課的同學的信息
select * from student where sno in(select distinct  sno from sc where cno in(select cno from course where tno=(select tno from teacher where tname='王海')))
select * from student s left join sc on  s.sno=sc.sno left join course c on sc.cno=c.cno left join teacher t on c.tno=t.tno where t.tname='王海'
select st.*
from student as st,course,teacher,sc
where st.sno = sc.sno
and sc.cno = course.cno
and course.tno = teacher.tno
and teacher.tname = '王海'
--8. 查詢沒有學全所有課程的同學的信息
  select * from student where sno in(select  sc.sno from sc,(select count(cno)as c from course) as cc group by sc.sno,cc.c having count(sc.sno) <cc.c)
  select * 
from student
where student.sno not in 
(select sc.sno 
from sc
group by sc.sno
having count(sc.cno) = (select count(course.cno) from course))
--9.查詢至少有一門課與學號為" 01 "的同學所學相同的同學的信息
--select * from sc a,(select cno from sc where sno=1)as b where a.sno!=1   group by sno  having   count(case when a.cno in b then 1 else 0 end) >2  
select sno, count(cno) as 數量 from sc  where sno!=1 and  cno  in(select cno from sc where sno=1) group by  sno having count(cno)>0
--10.查詢和" 01 "號的同學學習的課程完全相同的其他同學的信息
select sno, count(cno) as 數量 from sc   where sno!=1 and  cno  in(select cno from sc where sno=1) group by  sno having count(sc.cno)=(select count(cno) from sc where sno=1)

--思路 :  查詢 和 01號同學 課程 數量一樣的 其他 同學
--再把數量一樣的 同學 inner join  01 號同學。  如果數量 還是一樣則課程相同 
--11. 查詢沒學過"王海"老師講授的任一門課程的學生姓名
select sname from student where sno not in(select s.sno from student s left join sc a on s.sno=a.sno left join sc b on a.sno=b.sno left join course c on b.cno=c.cno left join teacher t on c.tno=t.tno  
where t.tname='王海' group by s.sno)

--select *
--from student as st
--where st.sno not in (
--    select sno    from sc where sc.cno in (
--        select course.cno from course where course.tid  in (
--            select tid from teacher where tname = '張三')));
 
 
--select * from student
--where student.sno not in (select sc.sno from course,sc,teacher
--                    where sc.cno = course.cno
--                    and course.tid = teacher.tid
--                    and tname = '張三');

--12.查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select sc.sno,s.sname,avg(socre) from student s left join   sc on s.sno=sc.sno group by sc.sno,s.sname having  count(case when socre<60 then 1 else 0 end)>1
--select st.sno,st.sname,avg(sc.socre) as avg_socre
--from student as st,sc
--where st.sno = sc.sno
--and sc.socre < 60
--group by st.sno,st.sname
--having count(*) >=2;
 
 
--select student.sno, student.sname, AVG(sc.socre) as avg_socre from student,sc
--where student.sno = sc.sno and sc.socre<60
--group by student.sno,student.sname
--having count(*)>1;
--13.檢索" 01 "課程分數小於 60,按分數降序排列的學生信息
select * from student s left join sc on s.sno=sc.sno  where  cno='1001' and socre<60  order by socre desc
--14. 按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
 select *  from student s left join sc on s.sno=sc.sno left join  (select sno,avg(socre) as pingju from sc  group by sno)as b on b.sno=sc.sno order by b.pingju desc
-- 15.查詢各科成績最高分、最低分和平均分,選修人數,及格率,中等率,優良率,優秀率:
select cno ,max(socre)as '成績最高' ,min(socre)as '最低分',
avg(socre)as '平均分',count(socre)as'選修人數',
sum(case when sc.socre>=60 then 1 else 0 end)*100/count(*) as '及格率',
sum(case when sc.socre>=70 and sc.socre<80 then 1 else 0 end )*100/count(*)as'中等率',
sum(case when sc.socre>=80 and sc.socre<90 then 1 else 0 end )*100/count(*)as'優良率',
sum(case when sc.socre>=90 then 1 else 0 end )*100/count(socre)as'優秀率'  
from  sc  group by cno ORDER BY count(*)DESC, sc.cno ASC
--16.按各科成績進行排序,並顯示排名, socre 重復時保留名次空缺
select  *,rank()over(partition by cno order by socre desc) as 名次  from sc  -- order  by cno  ,socre desc
select  *,DENSE_RANK()over(partition by cno order by socre desc) as 名次  from sc
select  *,ROW_NUMBER()over(partition by cno order by socre desc) as 名次  from sc
--17.查詢學生的總成績,並進行排名,總分重復時不保留名次空缺 
select *,ROW_NUMBER()over(order by su desc) as 名次  from student s left join(select sno, sum(socre) as su from sc group by sno)as b on b.sno=s.sno 
--18.統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select sc.cno as'課程編號',course.cname as '課程名稱',
sum(case when sc.socre>=85 and sc.socre<101 then 1 else 0 end )as'[100-85]',sum(case when sc.socre>=85 and sc.socre<101 then 1 else 0 end )*100/count(*)as'所占百分比',
sum(case when sc.socre>=70 and sc.socre<85 then 1 else 0 end )as'[85-70]',sum(case when sc.socre>=70 and sc.socre<85 then 1 else 0 end )*100/count(*)as'所占百分比',
sum(case when sc.socre>=60 and sc.socre<70 then 1 else 0 end )as'[70-60]',sum(case when sc.socre>=60 and sc.socre<70 then 1 else 0 end )*100/count(*)as'所占百分比',
sum(case when sc.socre<61 then 1 else 0 end) as '[60-0]',sum(case when sc.socre<61 then 1 else 0 end)*100 /count(*) as '所占百分比' from  sc  left join course 
on sc.cno=course.cno group by sc.cno,course.cname order by sc.cno
--19.查詢各科成績前三名的記錄
select * from(select *, ROW_NUMBER() over (partition by cno order by socre desc) as '名次' from sc )as s where s.名次<4
--20.查詢出只選修兩門課程的學生學號和姓名

--聯合查詢
select s.sname,  sc.sno,count(*) as menshu from sc,student s where s.sno=sc.sno  group by sc.sno,s.sname  having count(*)=2
--嵌套查詢
--select sno,sname
--from student
--where student.sno in (select sc.sno from sc group by sc.sno having count(sc.cno)=2);
--21. 查詢同名學生名單,並統計同名人數 
select  sname ,count(sno) from student group by sname
select sname,count(sname) as 同名人數
from student
group by sname
having count(*) >1;
 
--嵌套查詢列出同名的全部學生的信息
select st.*
from student as st
where st.sname in (select sname from student group by sname having count(*) > 1)
--23.查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
select  cno ,avg(socre)as pingjun from sc group by cno order by avg(socre) desc ,cno   
--24.查詢平均成績大於等於 85 的所有學生的學號、姓名和平均成績
select sc.sno,s.sname,avg(socre)as pingjun from student s left join sc on sc.sno=s.sno group by sc.sno,s.sname  having avg(socre)>85
--25.查詢課程名稱為「數學」,且分數低於 60 的學生姓名和分數
select s.sno,s.sname,c.cname,sc.socre from student s left join sc on sc.sno=s.sno left join course c on c.cno=sc.cno  where c.cname='math' and sc.socre<60
--26.查詢所有學生的課程及分數情況(存在學生沒成績,沒選課的情況)
select * from (select * from student s)as s left join (select * from sc )as scc  on s.sno=scc.sno
select student.sname,cno,socre
from student
left join sc
on student.sno = sc.sno
---聯合查詢下---沒選課的不會被查詢出來
--select st.sname,cno,socre
--from student as st,sc
--where st.sno = sc.sno;
----group by st.sname,cno,socre;
 
----級聯查詢下===沒選課的情況也會根據用戶人數顯示選課為空
--select student.sname,cno,socre
--from student
--left join sc
--on student.sno = sc.sno
--27.查詢任何一門課程成績在 70 分以上的姓名、課程名稱和分數
select s.sname ,sc.cno,c.cname ,sc.socre from student s left join sc  on s.sno=sc.sno left join course c on c.cno=sc.cno where sc.socre>70
select student.sname,course.cname,sc.socre 
from student,course,sc
where sc.socre >70
and student.sno = sc.sno
and sc.cno = course.cno
--28. 查詢存在不及格的課程
select s.sname ,sc.cno,c.cname ,sc.socre from student s left join sc  on s.sno=sc.sno left join course c on c.cno=sc.cno where sc.socre<60
select cno
from sc
where socre < 60
group by cno;
 
select distinct sc.cno
from sc
where sc.socre <60;
--29.查詢課程編號為 01 且課程成績在 80 分及以上的學生的學號和姓名
 select student.sno,sname,sco.socre from student left join  ( select  sc.cno,sc.sno,sc.socre from sc  where sc.socre>79 and cno='1001')as sco on sco.sno=student.sno where sco.socre is not null

-- select st.sno,st.sname,sc.socre
--from student as st,sc
--where st.sno =sc.sno
--and sc.cno = '01'
--and sc.socre >=80;
--30.成績有重復的情況下,查詢選修「王海」老師所授課程的學生中,成績最高的學生信息及其成績
select top 1 socre, sno from sc, (select cno from course , (select tno from teacher where tname='王海')as t where t.tno=course.tno)as c where c.cno=sc.cno   order by socre  desc  
--31.查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
select * from sc a left join  sc b  on a.sno=b.sno  left join student s on a.sno=s.sno where a.socre=b.socre and a.cno!=b.cno 
--32.查詢每門功成績最好的前兩名
select * from (select * ,rank()over(partition by cno order by socre desc)as paiming from sc) as ss where ss.paiming<3
select  a.sno,a.cno,a.socre
from sc as a
left join sc as b
--此處使用cno進行級聯,因為判定的分組依據是每門成績
on a.cno = b.cno and a.socre < b.socre
group by a.sno,a.cno,a.socre
having count(b.cno) <2
order by a.cno;
--33. 統計每門課程的學生選修人數(超過 5 人的課程才統計)
select cno,  count(*) from sc group by cno  having count(*) >5
--34.檢索至少選修兩門課程的學生學號
select sno ,count(*)from sc group by sno having count(*)>1
--35.查詢選修了全部課程的學生信息
select cno,c.cou from sc,(select count(*)as cou from course)as c group by cno,c.cou having c.cou=count(*)
--select student.*
--from student,sc
--where student.sid = sc.sid
--group by student.sid,student.sname,student.sage,student.ssex
--having count(sc.cid) = (select distinct count(*) from course);
 
--select *
--from sc
--36. 按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一

--select student.sid,student.sname,datediff(year,student.sage,getdate()) as 學生年齡
--from student;
--37.查詢本周過生日的學生

--備注:37,38題在測試過程中建議添加當前(你練習時間)周的周一,周日,上一周的周日,下一周的周一,周日,下下周的周一 這些sage時間數據的學生,便於理解查詢代碼中的參數含義

---- 本周一
--declare @dt1 datetime
--set @dt1 = DATEADD(week, DATEDIFF(week, 0, GETDATE()), 0)
---- 下周一
--declare @dt2 datetime
--set @dt2 = DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) + 1, -1)
----此處的-1是為了調整一周的時間段,若為0,則會包含下周一,為-1時,則僅包含當周周末
--print @dt1
--select * from student
--WHERE DATEADD(Year,DATEDIFF(Year,student.sage,@dt1),student.sage)
--BETWEEN @dt1 AND @dt2
--OR DATEADD(Year,DATEDIFF(Year,student.sage,@dt2),student.sage)
--BETWEEN @dt1 AND @dt2
--38.查詢下周過生日的學生

---- 下周一
--declare @dt1 datetime
--set @dt1 = DATEADD(week, DATEDIFF(week, 0, GETDATE())+1, 0)
---- 下下周一
--declare @dt2 datetime
--set @dt2 = DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) + 2, -1)
--print @dt2
--select * from student
--WHERE DATEADD(Year,DATEDIFF(Year,student.sage,@dt1),student.sage)
--BETWEEN @dt1 AND @dt2
--OR DATEADD(Year,DATEDIFF(Year,student.sage,@dt2),student.sage)
--BETWEEN @dt1 AND @dt2;
 

--39.查詢本月過生日的學生 

--select *
--from student
--where month(student.sage) = month(getdate())
--40. 查詢下月過生日的學生

--select *
--from student
--where month(student.sage) = month(getdate())+1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM