database day01

1. There are 3 data tables in the student course selection database:

S(sno,sname,birthday,sdeptartment,tel,sex),

SC(sno,cno,grade), C(cno,cname,teacher,pcno)。

2. Use SQL Server Management Studio to insert the following data into the three tables of the database S_C: student table S:

sno

takes off

birthday

sdeptartment

tel

sex

J0401

Li Jun

1981-2-12

computer science

0576-85123464

male

J0402

Liu Chen

1980-5-22

Department of Mathematics

0576-85123466

female

J0403

Wang Min

1980-4-28

computer science

0576-85123464

female

J0404

Zhang Li

1979-9-8

computer science

0576-85123464

male

Curriculum C:

cno

cname

teacher

pcno

C01

database

GUO

C03

C02

VB

LIU

C03

C03

Computer Basics

LI

C04

math

WANG

C05

data structure

ZHANG

C03

C06

C language

CHEN

C03

Transcript SC:

sno

cno

grade

J0401

C05

92

J0401

C06

85

J0401

C02

88

J0401

C01

90

J0402

C04

80

J0403

C02

76

J0404

C03

88

3. In the query window, use the SELECT statement to complete the following query:

1) List " Wang Min " information ;

Sql statement: SELECT * FROM S WHERE sname=' Wang Min ';

Query result description :

2) The student number, name and age of the student are displayed in the student table ;

Sql语句:SELECT sno,sname,DATEDIFF(yy,birthday,getdate())  AS age FROM S;

Query result description :

3) Display all boys information ;

Sql statement: SELECT * FROM S WHERE sex = ' male' ;

Query result description :

4) Display all students in the Department of Computer Science, sorted by birthday in descending order;

Sql statement : SELECT * FROM ORDER BY birthday DESC ;

Query result description :

5) Display the course information of all course names containing "Number" ;

Sql statement: SELECT * FROM C WHERE cname LIKE '% number%' ;

Query result description :

6) Display the course number and course name whose pcno is "C03";

Sql语句:SELECT * FROM C WHERE pcno='C03';

Query result description :

7) Display the average grade of the course whose course number is " C02 " ;

Sql语句:SELECT AVG(grade)FROM SC WHERE cno='C02';

Query result description :

8) Retrieve the birth year of each student ;

Sql statement: SELECT * FROM S ;

Query result description :

9) Retrieve the student's name and year of birth in S , and the output column names are STUDENT_NAME and BIRTH_YEAR respectively ;

Sql语句:SELECT sname AS STUDENT_NAME, birthday AS BIRTH_YEAR FROM S ;

Query result description :

10) Display the highest score of the course whose course number is " C02 " ;

Sql语句:SELECT MAX(grade) FROM SC  WHERE cno='C02';

Query result description :

11) Find the number of students who have taken each course ;

Sql语句:SELECT cno,COUNT(sno) FROM SC GROUP BY cno ;

Query result description :

12) In SC , find the student numbers and scores of the students who took the elective course C01 , and sort the results in descending order of scores ;

Sql语句:SELECT sno,grade FROM SC WHERE cno='C01' ORDER BY grade DESC;

Query result description :

13) Find each student's student number and the average grade of elective courses ;

Sql语句:SELECT sno, AVG(grade) FROM SC GROUP BY sno;

Query result description :

14) List all course numbers and grades of students whose student ID is " J0401 ", sorted in descending order of grades ;

Sql语句:SELECT cno ,grade FROM SC  WHERE sno='J0401'ORDER BY grade DESC;

Query result description :

15) List all student numbers and course numbers with scores lower than (including) 85 points ;

Sql语句:SELECT sno,cno FROM SC  WHERE grade<=85;

Query result description :

16) List the highest grades of each classmate ;

Sql语句:SELECT sno,MAX(grade) FROM SC  GROUP BY sno;

Query result description :

17) List the minimum grades for each course ;

Sql语句:SELECT sno,MIN(grade) FROM SC  GROUP BY sno;

Query result description :

18) Retrieve the records of students with the surname "Wang" in S ;

Sql statement: SELECT * FROM S WHERE sname LIKE ' King%' ;

Query result description :

19) List all students born in May ;

Sql语句:SELECT * FROMWHERE MONTH(birthday)='5';

Query result description :

20) Count the number of courses taken by students ;

Sql语句:SELECT COUNT(DISTINCT cno) FROM SC  ;

Query result description :

21) Find the average age of the students who take the C04 course ;

Sql语句:SELECT AVG(DATEDIFF(yy,birthday,getdate())) AS AVERAGE FROM S,SC   WHERE cno='C04' ;

Query result description :

22) Ask for course information of courses taught by LIU teachers ;

Sql语句:SELECT * FROM C WHERE teacher='LIU' ;

Query result description :

23) List girls whose names contain Min ;

Sql statement: SELECT * FROM S WHERE sname LIKE '% min%' ;

Query result description :

24) Find the oldest boy ;

Sql statement: SELECT *

FROM S

WHERE SEX='男'AND DATEDIFF(yy,birthday,getdate()) >= ANY(SELECT  MAX(DATEDIFF(yy,birthday,getdate()))

FROM S ) ;

Query result description :

Get a part of a date:

YEAR('2022-3-11') --Year
MONTH('2022-3-11') --Month
DAY('2022-3-11') --Day

DATEPART ( datepart , date )
DATEPART(MM,' 2022-3-11')

year yy, yyyy
quarter qq, q
month mm, m
a certain day of the year dy, y
date dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms

Guess you like

Origin blog.csdn.net/m0_62321937/article/details/123452213