1. 程式人生 > >sql語句小練習

sql語句小練習

order by percent for 名稱 name delete not in like 1.4

1、用一條SQL 語句 查詢出每門課都大於80 分的學生姓名

name kecheng fenshu

張三 語文 81

張三 數學 75

李四 語文 76

李四 數學 90

王五 語文 81

王五 數學 100

王五 英語 90

A:select distinct name from table where name not in (select distinct name from table where fenshu<=80)   關鍵詞 DISTINCT 用於返回唯一不同的值

B:select name from table group by name having min(fenshu)>80                     關鍵詞 HAVING 可以與聚合函數(aggregate functions such as COUNT, SUM, AVG, MIN, or MAX)函數一起使用

C:select name from table group by name having count(kecheng)>=3 and min(fenshu)>=80

2、刪除除了自動編號不同, 其他都相同的學生冗余信息

自動編號 學號 姓名 課程編號 課程名稱 分數

1 2005001 張三 0001 數學 69

2 2005002 李四 0001 數學 89

3 2005001 張三 0001 數學 69

A: delete tablename where 自動編號 not in(select min( 自動編號) from tablename group by 學號, 姓名, 課程編號, 課程名稱, 分數)

3、怎麽把這樣一個表兒

year month amount

1991 1 1.1

1991 2 1.2

1991 3 1.3

1991 4 1.4

1992 1 2.1

1992 2 2.2

1992 3 2.3

1992 4 2.4

查成這樣一個結果

year m1 m2 m3 m4

1991 1.1 1.2 1.3 1.4

1992 2.1 2.2 2.3 2.4

A:select year,

(select amount from aaa m where month=1 and m.year=aaa.year) as m1,

(select amount from aaa m where month=2 and m.year=aaa.year) as m2,

(select amount from aaa m where month=3 and m.year=aaa.year) as m3,

(select amount from aaa m where month=4 and m.year=aaa.year) as m4

from aaa group by year

4、拷貝表( 拷貝數據, 源表名:a 目標表名:b)

A:insert into b(a, b, c) select d,e,f from a;

5、有一張表,裏面有3個字段:語文,數學,英語。

大於或等於80表示優秀,大於或等於60表示及格,小於60分表示不及格。

顯示格式:

語文 數學 英語

及格 優秀 不及格

A:select

(case when 語文>=80 then ‘優秀‘

when 語文>=60 then ‘及格‘

else ‘不及格‘) as 語文,

(case when 數學>=80 then ‘優秀‘

when 數學>=60 then ‘及格‘

else ‘不及格‘) as 數學,

(case when 英語>=80 then ‘優秀‘

when 英語>=60 then ‘及格‘

else ‘不及格‘) as 英語,

from table

6、表的創建修改刪除

創建一張學生表,包含以下信息,學號,姓名,年齡,性別,家庭住址,聯系電話

A:

Create table stu (學號 int ,

姓名 varchar(8),

年齡 int,

性別 varchar(4),

家庭地址 varchar(50),

聯系電話 int

);

修改學生表的結構,添加一列信息,學歷

Alter table stu add 學歷 varchar(6);

修改學生表的結構,刪除一列信息,家庭住址

Alter table stu drop column 家庭地址

向學生表添加如下信息:

學號 姓名年齡性別聯系電話學歷

1A22男123456小學

2B21男119中學

3C23男110高中

4D18女114大學

Insert into stu values(1,’A’,22,’男’,123456,’小學’)

Insert into stu values(2,’B’,21,’男’,119,’中學’)

Insert into stu values(3,’C’,23,’男’,110,’高中’)

Insert into stu values(4,’D’,18,’女’,114,’大學’)

修改學生表的數據,將電話號碼以11開頭的學員的學歷改為“大專”

Update stu set 學歷=’大專’ where 聯系電話 like ‘11%’

刪除學生表的數據,姓名以C開頭,性別為‘男’的記錄刪除

Delect from stu where 性別=’男’ and 姓名 like ‘c%’

查詢學生表的數據,將所有年齡小於22歲的,學歷為“大專”的,學生的姓名和學號示出來

Select 姓名,學號 from stu where 年齡<22 and 學歷=’大專’

查詢學生表的數據,查詢所有信息,列出前25%的記錄

Select top 25 percent * from stu

查詢出所有學生的姓名,性別,年齡降序排列

Select 姓名,性別 from stu order by 年齡 desc

按照性別分組查詢所有的平均年齡

Select avg(年齡) from stu group by 性別

7、查詢A(ID,Name)表中第31至40條記錄,ID作為主鍵可能是不是連續增長的列,完整的查詢語句如下:

A:select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by A ) T) order by A

8、查詢表A中存在ID重復三次以上的記錄,完整的查詢語句如下:

select * from(select count(ID) as count from table group by ID)T where T.count>3

9、說出以下聚合數的含義:avg ,sum ,max ,min , count ,count(*)

AVG:求平均值

SUM:求和

MAX:求最大值

MIN:求最小值

COUNT(*):返回所有行數

關鍵字含義

事務 Transaction 觸發器 TRIGGER 繼續 continue 唯一 unqiue

主鍵 primary key 標識列 identity 外鍵 foreign key 檢查 check

約束 constraint

10、隨機取出10條數據

select top 10 * from tablename order by newid()

11、查詢平均成績大於60分的同學的學號和平均成績;

select stuId,avg(score)

from Scores

group by stuId having avg(score) >60;

12、https://www.toutiao.com/a6400624100788601090/

13、

sql語句小練習