Preparation
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 the score table. The SQL code is as follows:
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)
);
2. Add to the student table and score table Records
The INSERT statement to insert records into the student table is as follows:
INSERT INTO student VALUES( 901,'Boss Zhang','Male',1985,'Computer Department','Haidian District, Beijing');
INSERT INTO student VALUES( 902,' Zhang Laoer', 'Male',1986,'Chinese Department','Changping District, Beijing');
INSERT INTO student VALUES( 903,'Zhang San','Female',1990,'Chinese Department','Hunan Province Yongzhou');
INSERT INTO student VALUES( 904,'Li Si','Male',1990,'English Department','Fuxin City, Liaoning Province'); INSERT INTO student VALUES( 905,'Wang Wu','Female
',1991,' English Department', 'Xiamen City, Fujian Province');
INSERT INTO student VALUES( 906,'Wang Liu','Male',1988,'Computer Department','Hengyang City, Hunan Province'); INSERT
to insert records into the score table The statement is as follows:
INSERT INTO score VALUES(NULL,901, 'Computer',98);
INSERT INTO score VALUES(NULL,901, 'English', 80);
INSERT INTO score VALUES(NULL,902, 'Computer',65) ;
INSERT INTO score VALUES(NULL,902, 'Chinese',88);
INSERT INTO score VALUES(NULL,903, 'Chinese',95);
INSERT INTO score VALUES(NULL,904, 'Computer',70);
INSERT INTO score VALUES(NULL,904, 'English',92);
INSERT INTO score VALUES(NULL,905, 'English',94);
INSERT INTO score VALUES(NULL,906, 'Computer',90);
INSERT INTO score VALUES(NULL,906,'English',85);
mysql> select * from score;
+----+--------+-----------+-------+
| id | stu_id | c_name | grade |
+----+--------+-----------+-------+
| 1 | 901 | 计算机 | 98 |
| 2 | 901 | 英语 | 80 |
| 3 | 902 | 计算机 | 65 |
| 4 | 902 | 中文 | 88 |
| 5 | 903 | 中文 | 95 |
| 6 | 904 | 计算机 | 70 |
| 7 | 904 | 英语 | 92 |
| 8 | 905 | 英语 | 94 |
| 9 | 906 | 计算机 | 90 |
| 10 | 906 | 英语 | 85 |
+----+--------+-----------+-------+
10 rows in set (0.00 sec)
mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
1. Query all records of the student table
mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
2. Query the 2nd to 4th records of the student table
mysql> select * from student limit 1,3;
+-----+-----------+------+-------+------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+------------+--------------------+
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
+-----+-----------+------+-------+------------+--------------------+
3 rows in set (0.00 sec)
3. Query the student number (id), name (name) and department (department) information of all students from the student table
mysql> select id as `学号`,name as `姓名`,department as `院系` from student;
+--------+-----------+--------------+
| 学号 | 姓名 | 院系 |
+--------+-----------+--------------+
| 901 | 张老大 | 计算机系 |
| 902 | 张老二 | 中文系 |
| 903 | 张三 | 中文系 |
| 904 | 李四 | 英语系 |
| 905 | 王五 | 英语系 |
| 906 | 王六 | 计算机系 |
+--------+-----------+--------------+
6 rows in set (0.00 sec)
4. Query the information of the students of the computer department and the English department from the student table
mysql> select * from student where department = '计算机系' or department = '英语系';
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.00 sec)
5. Query the information of students aged 18~22 from the student table
mysql> select id,name,sex,year(curdate())-birth as age,department,address from student where year(curdate())-birth between 18 and 22;
6. Query how many people are in each department from the student table
mysql> select department,count(department) as `人数` from student group by department;
+--------------+--------+
| department | 人数 |
+--------------+--------+
| 计算机系 | 2 |
| 中文系 | 2 |
| 英语系 | 2 |
+--------------+--------+
3 rows in set (0.00 sec)
7. Query the highest score of each subject from the score table
mysql> select c_name as `科目`,max(grade) as `最高分` from score group by c_name;
+-----------+-----------+
| 科目 | 最高分 |
+-----------+-----------+
| 计算机 | 98 |
| 英语 | 94 |
| 中文 | 95 |
+-----------+-----------+
3 rows in set (0.00 sec)
8. Query Li Si's test subjects (c_name) and test results (grade)
mysql> select student.name,score.c_name,score.grade from student inner join score on student.id = score.stu_id where student.name = '李四';
+--------+-----------+-------+
| name | c_name | grade |
+--------+-----------+-------+
| 李四 | 计算机 | 70 |
| 李四 | 英语 | 92 |
+--------+-----------+-------+
2 rows in set (0.00 sec)
9. Query all student information and test information by connecting
mysql> select student.id,student.name,student.sex,student.birth,score.c_name,score.grade,student.address from student inner join score on student.id = score.stu_id;
+-----+-----------+------+-------+-----------+-------+--------------------+
| id | name | sex | birth | c_name | grade | address |
+-----+-----------+------+-------+-----------+-------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机 | 98 | 北京市海淀区 |
| 901 | 张老大 | 男 | 1985 | 英语 | 80 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 计算机 | 65 | 北京市昌平区 |
| 902 | 张老二 | 男 | 1986 | 中文 | 88 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文 | 95 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 计算机 | 70 | 辽宁省阜新市 |
| 904 | 李四 | 男 | 1990 | 英语 | 92 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语 | 94 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机 | 90 | 湖南省衡阳市 |
| 906 | 王六 | 男 | 1988 | 英语 | 85 | 湖南省衡阳市 |
+-----+-----------+------+-------+-----------+-------+--------------------+
10 rows in set (0.00 sec)
10. Calculate the total grade of each student
mysql> select student.name as `学生`,sum(score.grade) as `总成绩` from student inner join score on student.id=score.stu_id group by student.na me;
+-----------+-----------+
| 学生 | 总成绩 |
+-----------+-----------+
| 张老大 | 178 |
| 张老二 | 153 |
| 张三 | 95 |
| 李四 | 162 |
| 王五 | 94 |
| 王六 | 175 |
+-----------+-----------+
6 rows in set (0.01 sec)
11. Calculate the average grade for each exam subject
mysql> select department as `科目`,avg(grade) as `平均成绩` from studentt inner join score on student.id=score.stu_id group by department;
+--------------+--------------+
| 科目 | 平均成绩 |
+--------------+--------------+
| 计算机系 | 88.2500 |
| 中文系 | 82.6667 |
| 英语系 | 85.3333 |
+--------------+--------------+
3 rows in set (0.00 sec)
12. Query the information of students whose computer scores are lower than 95
mysql> select name,c_name,grade from student inner join score on studentt.id=score.stu_id where c_name = '计算机' and grade < 95;
+-----------+-----------+-------+
| name | c_name | grade |
+-----------+-----------+-------+
| 张老二 | 计算机 | 65 |
| 李四 | 计算机 | 70 |
| 王六 | 计算机 | 90 |
+-----------+-----------+-------+
3 rows in set (0.00 sec)
13. Query the information of students who take both computer and English exams
mysql> SELECT s.*
-> FROM student s
-> INNER JOIN score sc1 ON s.id = sc1.stu_id
-> INNER JOIN score sc2 ON s.id = sc2.stu_id
-> WHERE sc1.c_name = '计算机' AND sc2.c_name = '英语';
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
14. Sort computer test scores from high to low
mysql> select * from score where c_name = '计算机' order by grade desc;
+----+--------+-----------+-------+
| id | stu_id | c_name | grade |
+----+--------+-----------+-------+
| 1 | 901 | 计算机 | 98 |
| 9 | 906 | 计算机 | 90 |
| 6 | 904 | 计算机 | 70 |
| 3 | 902 | 计算机 | 65 |
+----+--------+-----------+-------+
4 rows in set (0.00 sec)
15. Query the student ID number from the student table and score table, and then merge the query results
mysql> select id from student union select stu_id from score;
+-----+
| id |
+-----+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
+-----+
6 rows in set (0.00 sec)
16. Query the name, department, examination subjects and grades of students surnamed Zhang or Wang
mysql> select stu.name,stu.department,sco.c_name,sco.grade from student as stu left join score as sco on stu.id=sco.stu_id where name like '张%' or name like '王%';
+-----------+--------------+-----------+-------+
| name | department | c_name | grade |
+-----------+--------------+-----------+-------+
| 张老大 | 计算机系 | 英语 | 80 |
| 张老大 | 计算机系 | 计算机 | 98 |
| 张老二 | 中文系 | 中文 | 88 |
| 张老二 | 中文系 | 计算机 | 65 |
| 张三 | 中文系 | 中文 | 95 |
| 王五 | 英语系 | 英语 | 94 |
| 王六 | 计算机系 | 英语 | 85 |
| 王六 | 计算机系 | 计算机 | 90 |
+-----------+--------------+-----------+-------+
8 rows in set (0.01 sec)
17. Query the names, ages, departments, examination subjects and grades of students from Hunan
mysql> select name,year(curdate())-birth as age,department,c_name,grade from student left join score on student.id=score.stu_id where address like '%湖南%';
+--------+------+--------------+-----------+-------+
| name | age | department | c_name | grade |
+--------+------+--------------+-----------+-------+
| 张三 | 33 | 中文系 | 中文 | 95 |
| 王六 | 35 | 计算机系 | 英语 | 85 |
| 王六 | 35 | 计算机系 | 计算机 | 90 |
+--------+------+--------------+-----------+-------+
3 rows in set (0.00 sec)