1. 程式人生 > >SQL第九章資料查詢基礎上機題1-4

SQL第九章資料查詢基礎上機題1-4


//查詢學生相關基本資訊
SELECT * FROM Student WHERE GradeId=1
 SELECT StudentName,Phone FROM Student  WHERE GradeId=2
 SELECT StudentName+Phone FROM Student  WHERE GradeId=2
 SELECT * FROM Student WHERE GradeId=1 AND Sex='女'
 SELECT SubjectName FROM Subject WHERE ClassHour>60




//查詢學生相關複雜資訊
SELECT SubjectName FROM Subject WHERE GradeId=1
SELECT StudentName,[Address] FROM Student WHERE GradeId=2 AND Sex='男'
SELECT StudentName,GradeId FROM Student WHERE Email IS NULL
SELECT StudentName FROM Student WHERE GradeId=2 AND BornDate>1990
SELECT StudentResult FROM Result WHERE ExamDate='2013-2-15' AND SubjectId=1


//使用排序查詢學生相關資訊

SELECT * FROM Student WHERE GradeId=1 ORDER BY BornDate
SELECT * FROM Result WHERE SubjectId=1 ORDER BY ExamDate,StudentResult DESC
SELECT TOP 5 StudentResult FROM Result WHERE SubjectId=8 AND ExamDate='2013-3-22' ORDER BY StudentResult DESC
SELECT TOP 1 SubjectName FROM Subject WHERE  GradeId=3 ORDER BY ClassHour DESC
SELECT TOP 1 GradeId,StudentName FROM Student  ORDER BY BornDate DESC
SELECT TOP 1 SubjectId FROM Result WHERE ExamDate='2013-3-22' ORDER BY StudentResult
SELECT * FROM Result WHERE StudentNo='S1101003' ORDER BY ExamDate
SELECT TOP 1 StudentResult  ,SubjectId,ExamDate FROM Result WHERE StudentNo='S1101003' ORDER BY StudentResult DESC


//使用函式查詢學生相關資訊
ELECT * FROM Student WHERE GradeId=2 AND DATEDIFF(yy,BornDate,'11/22/2016')>20
SELECT * FROM Student WHERE DATENAME(mm,BornDate)=1
SELECT GradeId ,StudentName FROM Student WHERE CONVERT(VARCHAR(2),DATEPART(mm,GETDATE()))+CONVERT(VARCHAR(2),DATEPART(dd,GETDATE()))= CONVERT(VARCHAR(2),DATEPART(mm,BornDate))+CONVERT(VARCHAR(2),DATEPART(dd,BornDate))
SELECT RIGHT(Email,(LEN(Email)-CHARINDEX('@',Email,1)))AS Email域名 FROM Student WHERE StudentNo='Y21003012' 
SELECT TOP 1 'S1'+CONVERT(VARCHAR(4),DATEPART(YYYY,GETDATE()))+CONVERT(VARCHAR(2),DATEPART(mm,GETDATE()))+CONVERT(VARCHAR(2),DATEPART(dd,GETDATE()))+RIGHT(RAND(),4)+'@jbit.com' AS 郵件地址  FROM Student