当前位置:网站首页>Introduction to database system experiment report answer Experiment 5: database single table query

Introduction to database system experiment report answer Experiment 5: database single table query

2022-06-11 08:24:00 kl_ xiao

Experiment five : Database single table query

One 、 The experiment purpose

1. master SELECT The basic syntax of the statement and the expression method of query conditions ;

2. Master the expression and usage of query conditions ;

3. master GROUP BY The function and use of clause ;

4. master HAVING The function and use of clause ;

5. master ORDER BY The function and use of clause .

Two 、 Experimental hours

2 Class hours

3、 ... and 、 The experimental requirements

1. Proficient in database query statements ;

2. Master the implementation of database query ;

3. Finish the experiment report ;

Four 、 Experimental content

With SQL experiment 4 Based on the data in the database , Please use T-SQL Statement implements the following operations :

1.    List all non surnames “ Zhang ” Basic information of all students ;

select*

from XSKC.student

where sname notlike' Zhang %';

2.    List last names “ king ” And the full name is 3 Basic information of students with Chinese characters ;

select* 

from XSKC.student 

where sname like' king __';

What happened :1.name The type of should be varchar perhaps navarchar Instead of char

2.sname Because of copy and paste, there are many spaces in the data of Should be deleted

3.    Displayed in the 1986 Student number and name of students born after ;

select sno,sname 

from XSKC.student 

where (2021-Sage)>=1986;

4.    according to “ Gender 、 Student number 、 full name 、 Age 、 departments ” List student information in the order of , And rename the columns ;

select ssex Gender ,sno Student number ,sname full name ,sage Age ,sdept departments

from XSKC.student

5.    Query the names and student numbers of students who are not assigned departments ;

select sname,sno

from XSKC.student

where sdept isnull;

6.    The eighth or ninth digit of the student number is 1、2、3、4 perhaps 9 Student's student number 、 full name 、 Gender 、 Age and department ;

select sno,sname,ssex,sage,sdept

from XSKC.student

where sno like'_______[12349]_'or sno like'%[12349]';

7.    List of electives 01 Student number and grades of the students of course No , In descending order of grades ;

select sno

from XSKC.sc

where cno=1

orderby grade desc;

8.    According to the course number 、 The descending order of grades shows that the course grades are 70-80 The student number of the students between 、 Course number and grade ;

select sno,cno,grade

from XSKC.sc

where grade between 70 and 80

orderby sno,grade desc;

9.    Displays the of all students in descending order of department “ departments , Student number 、 full name 、 Gender 、 Age ” Etc , Among them, colleges and departments shall display... According to the following provisions : The Department is CS Displayed as computer department , The Department is IS Displayed as information system , The Department is MA Displayed as Department of Mathematics , The Department is EN Displayed as foreign language department , The Department is CM Displayed as Department of traditional Chinese Medicine , The Department is WM Displayed as Western Medicine Department , Others show that the Department is unknown ;

select

sdept=case

when sdept='CS'then' Department of Computer Science '

when sdept='IS'then' Information Department '

when sdept='MA'then' Department of mathematics '

when sdept='EN'then' Department of foreign languages '

when sdept='CM'then' Department of traditional Chinese Medicine '

when sdept='WM'then' Department of Western Medicine '

else' Department unknown 'end,sno,sname,ssex,sage

from XSKC.student

orderby sdept desc;

10.  Show elective 02 Student ID of the top two students in course No .

selecttop 2 sno

from XSKC.sc

where cno='2'

orderby grade desc;

11.  List concurrent elective courses “1” Course No. and “2” The student numbers of all students in course No ;

select sno

from XSKC.sc

where cno='1'and sno in(select sno from XSKC.sc where cno='2');

12.  Show all departments ( Requirements cannot be repeated , Excluding null values ), And add a column of fields in the result set “ Faculty size ”, Among them, if the number of students in the Department >=5 Then the field value is “ Large scale ”, If the number of students in the Department is greater than or equal to 4 Less than 5 Then the field value is “ Average size ”, If the number of students in the Department is greater than or equal to 2 Less than 4 Then the field value is “ A little smaller ”, Otherwise, it will show “ It's very small ”;

SELECT' departments '=sdept,' Faculty size '=CASE

WHENCOUNT(*)>=5 THEN' Large scale '

WHENCOUNT(*)>=4 ANDCOUNT(*)<5 THEN' Average size '

WHENCOUNT(*)>=2 ANDCOUNT(*)<4 THEN' A little smaller '

ELSE' It's very small 'END

FROM XSKC.student

GROUPBY sdept 

13.  Displays the total number of students and their average age in the student information table , In the result set, the column headings are specified as “ The total number of students , Average age ”;

SELECTCOUNT(*)' The total number of students ',AVG(sage)' Average age '

FROM XSKC.student;

14.  Show that the number of elective courses is greater than 3 The number of elective courses for each student ;

SELECT sno,COUNT(cno)' Number of electives '

FROM XSKC.sc

GROUPBY sno

HAVINGCOUNT(*)>3;

15.  The total number of students who take each course is displayed in descending order of course number 、 Top grade 、 Minimum and average grades ;

SELECTCOUNT(*) The total number of ,MAX(grade) Top grade ,MIN(grade) Minimum score ,AVG(grade) Average score

FROM XSKC.sc

GROUPBY cno

ORDERBY cno DESC;

Problems arise :char type There is an error in descending sorting by course number

It is suggested to change to int

16.  Show average score greater than “ Zhao Jingjing ” The student number of each student with average grade 、 Average score ;

SELECT sno Student number ,AVG(grade) Average score

FROM XSKC.sc

GROUPBY sno

HAVINGAVG(grade)>(SELECTAVG(grade)FROM XSKC.sc WHERE sno in

(SELECT sno FROM XSKC.student WHERE sname=' Zhao Jingjing '));

17.  Show IS Student number of department students 、 full name 、 Gender 、 Age , And statistics IS Number of students in the Department ;

selectcount(sno) The number of

from XSKC.student

where sdept='IS'

go                    

select sno,sname,ssex,sage

from XSKC.student

where sdept='IS'

18.  Displays the student number with the largest number of elective courses and the student number with the smallest number of elective courses ;

SELECT sno Student ID with the largest number of elective courses

FROM XSKC.sc

GROUPBY sno

HAVINGCOUNT(sno)>=ALL(SELECTCOUNT(sno)FROM XSKC.sc GROUPBY sno)

SELECT sno The student number with the least number of elective courses

FROM XSKC.sc

GROUPBY Sno

HAVINGCOUNT(sno)<=ALL(SELECTCOUNT(sno)FROM XSKC.sc GROUPBY sno)

19.  Display the first two records of students in each department , And form a new table ceshi;

SELECT a.*

INTO ceshi

FROM XSKC.student a

WHERE sno IN

(SELECTtop 2 sno

FROM XSKC.student

WHERE sdept=a.sdept

orderby sno)

20.  * Displays the number of people who have passed each course ;

selectCOUNT

(case

when grade>=60

then' pass '

end)  

Number of people passing each course

from XSKC.sc

groupby cno

21.  * Displays the number of male and female students in each department , The column headings in the result set are specified as “ The name of the Department 、 Number of boys 、 Number of girls ”;

select sdept The name of the Department ,

count(casewhen ssex=' male 'then'nan'end) Number of boys ,

count(casewhen ssex=' Woman 'then'nv'end) Number of girls

from XSKC.student

groupby sdept

22.  * List more than two courses ( Including two doors ) The student number of the failed student and the average score of the student ;

SELECT sno,AVG(grade) Average score

FROM XSKC.SC

WHERE grade <60

GROUPBY Sno

HavingCOUNT(Sno)>=2

notes : You need to write the of each problem in the experimental report book T-SQL sentence .

5、 ... and 、 The experimental steps

Command mode : stay 【SQL Server Management Studio】 Select... At the top left of the window 【 New query 】 Button , start-up SQL Editor window , Enter... At the cursor T-SQL sentence , single click 【 perform 】 Button . For example, the first question can be entered :

select * from student where sname not like ' Liu %'

For reference only

原网站

版权声明
本文为[kl_ xiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020511110855.html