MySQL query data exercises 20,191,010

Test Data:

DROP TABLE IF EXISTS `student`;
CREATE  TABLE  student (
id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY  ,
NAME  VARCHAR(20)  NOT NULL ,
sex  VARCHAR(4)  ,
birth  YEAR,
department  VARCHAR(20) ,
address  VARCHAR(50) 
);
CREATE  TABLE  score (

id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY  AUTO_INCREMENT ,

stu_id  INT(10)  NOT NULL ,

c_name  VARCHAR(20) ,

grade  INT(10)

);
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');

INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');

INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');

INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');

INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');

INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');

INSERT INTO score VALUES(NULL,901, '计算机',98);

INSERT INTO score VALUES(NULL,901, '英语', 80);

INSERT INTO score VALUES(NULL,902, '计算机',65);

INSERT INTO score VALUES(NULL,902, '中文',88);

INSERT INTO score VALUES(NULL,903, '中文',95);

INSERT INTO score VALUES(NULL,904, '计算机',70);

INSERT INTO score VALUES(NULL,904, '英语',92);

INSERT INTO score VALUES(NULL,905, '英语',94);

INSERT INTO score VALUES(NULL,906, '计算机',90);

INSERT INTO score VALUES(NULL,906, '英语',85);

3. Query All records of the student table
queries the student table Article 2-4 recording
information 5. Number Search school students from the student table (id), the name (name) and the College (Department) is
from the student table Department of computer Science and information query students of the English Department
query in age from 18 to 22 student table year-old student information
query from a student table each department how many people
9. query highest points score for each subject from the table
10. John Doe query test subjects (c_name) and test scores (grade)
11. the query information for all students and examination information of the connected
calculate a total score for each student 12.
13. the computing the average score for each test subject
14. query results are less than 95 students computer information
15. the inquiry also participate in computer and English exam student information
16. the computer test scores are sorted in descending
18. Zhang query or classmate surnamed Wang name, faculty and achievement test subjects and
19 inquiries are students of Hunan's name, age, faculty and test subjects and grades

SELECT * FROM student;
SELECT * FROM student LIMIT 1,3;
SELECT id AS 学号,NAME AS 姓名, department AS 系别 FROM student;
SELECT * FROM student WHERE department IN ('中文系','计算机系');
SELECT * FROM student WHERE department = '中文系' OR department ='计算机系';
SELECT * FROM student WHERE department LIKE '中文系' OR department ='计算机系';
SELECT * FROM student WHERE birth BETWEEN 1991 AND 1996;
SELECT id,NAME,sex,2013-birth AS age,department,address FROM student WHERE 2013-birth BETWEEN  18 AND 22;
SELECT id,NAME,sex,2013-birth AS age,department,address FROM student WHERE 2013-birth>=18 AND 2013-birth<=22;
SELECT department,COUNT(*) AS 人数 FROM student GROUP BY department;
SELECT * FROM student AS st INNER JOIN score AS sc ON st.id = sc.stu_id;
SELECT  stu_id,SUM(grade) FROM score GROUP BY stu_id;
SELECT  student.id ,SUM(score.grade) FROM student,score WHERE student.id = score.stu_id GROUP BY student.id;
SELECT a.stu_id, AVG(a.grade + b.grade)  FROM score a,score b WHERE a.stu_id = b.stu_id  GROUP BY a.stu_id;
SELECT c_name,AVG(grade) FROM score GROUP BY c_name;
SELECT st.id,st.name,sc.c_name FROM student AS st LEFT JOIN  score AS sc ON sc.stu_id = st.id AND sc.c_name = "计算机" AND sc.c_name = "英语";


##查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
```sql
SELECT st.name,st.department,sc.c_name,sc.grade,st.address FROM student AS st INNER JOIN score AS sc ON st.address LIKE "湖南省%" AND sc.stu_id = st.id ;
SELECT st.name,st.department,sc.c_name,sc.grade,st.address FROM student AS st LEFT OUTER JOIN score AS sc ON sc.stu_id = st.id WHERE st.address LIKE "湖南省%" ;
SELECT st.name,st.department,GROUP_CONCAT(sc.c_name),GROUP_CONCAT(sc.grade),st.address FROM student AS st LEFT OUTER JOIN score AS sc ON sc.stu_id = st.id WHERE st.address LIKE "湖南省%" GROUP BY st.name  ;

Published 15 original articles · won praise 14 · views 5721

Guess you like

Origin blog.csdn.net/qq_39204060/article/details/102480380