天天看點

SQL基礎3-面試題

1、編寫下列SQL 有兩張表

Table1,班級表class;字段:班級編号class_id ,班級名稱class_name

Table2,學生表student;字段:學生學号stu_id,學生姓名stu_name,班級編号cls_id,期末六門總成績exam_total_score

根據下列題目寫出sql:

求每個班級中的最高分數和最低分數,并且它的最小值小于200,最大值大于400。

使用where:在磁盤讀入資料時就進行判斷,是以要使用原始字段名

select MIN(total_score), stu_id, stu_name, cls_id from student where total_score<200 GROUP BY cls_id

select MAX(total_score), stu_id, stu_name, cls_id from student where total_score>400 GROUP BY cls_id

使用having:讀入記憶體後才開始判斷,可以使用max(total_score)或者别名

select max(total_score), stu_id, stu_name, cls_id from student  GROUP BY cls_id HAVING max(total_score)>400

slect MIN(total_score), stu_id, stu_name, cls_id from student  GROUP BY cls_id HAVING MIN(total_score)<200

select MAX(total_score) as a, stu_id, stu_name, cls_id from student  GROUP BY cls_id

内連接配接查詢兩張表

select MAX(total_score) as a, stu_id, stu_name, cls_id, class.class_name from student INNER JOIN class where cls_id=class.class_id  GROUP BY cls_id HAVING a>400

2.編寫SQL語句 建立一張學生表,包含以下資訊,學号,姓名,年齡,性别,家庭住址,聯系電話

Create table stu (學号 int not null primary key,

姓名 varchar(8),

年齡 int,

性别 varchar(4),

家庭位址 varchar(50),

聯系電話 int

);

3. 用一條SQL 語句 查詢出每門課都大于80 分的學生姓名

name kecheng fenshu 

張三 國文 81

張三 數學 75

李四 國文 76

李四 數學 90

王五 國文 81

王五 數學 100

王五 英語 90

 select distinct name from table where name not in (select distinct name from table where fenshu<=80);