SQL數據查詢

--1 獲取MyStudents表中年齡最大的前5個人,和前20%
select top 5 * from Mystudent order by MysAge Desc
select top 20 percent * from Mystudent order by MysAge Desc

--2 獲取當前時間
select GETDATE() as 當前時間
select CONVERT(char(8),getdate(),108)

--3 去掉重複distinct
select distinct * from Mystudent

--4 查詢英語成績最大,最小,總分和平均值
select MAX(MysEnglish) as 英語成績 from Mystudent
select MIN(MysEnglish) as 英語成績 from Mystudent
select SUM(MysEnglish) as 英語總分 from Mystudent
select AVG(MysEnglish) as 英語平均分 from Mystudent

--5 查詢班集總人數
select distinct COUNT(*) as 總人數 from Mystudent

--6 查詢數學成績爲null的人數(在數據庫中進行設置n各人的數學成績爲null看效果)
--drop table Text2
create table Text2
(
TId int identity(1,1) primary key,
TName nvarchar(10),
TGender nchar(2),
TAge int,
TMath float,
TEnglish float,
TBirthday datetime
)
insert into Text2
select '姜玉陽','男',22,90,89,'1991-10-04' union
select '李小琳','女',20,70,85,'1993-09-12' union
select '高小麗','女',19,80.5,90,'1994-01-17' union
select '包小白','男',20,60,99,'1993-12-08' union
select '關陽','男',20,100,80.5,'1991-12-05' union
select '劉霞','女',18,70,85,'1995-11-12' union
select '吳雙','女',19,100,98,'1994-09-09' union
select '路風','男',20,60,99,'1993-07-19' union
select '楊冪','女',21,null,99,'1992-04-20' union
select '董卿','女',20,null,80,'1993-05-26' union
select '步驚雲','男',20,null,70,'1993-09-23' union
select '謝濤','男',21,null,100,'1992-08-11'

select count(*) from Text2 where TMath is null
--select * from Text2

--7 男學生出生日期的最大值和最小值(設計出生日期字段)
select MAX(TBirthday) as 出生日期最大,MIN (TBirthday) as 出生日期最小 from Text2 where TGender='男'


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章