-- 1. Create student and score table statements
CREATE TABLE t_student(
id INT(10) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
sex VARCHAR(4),
birth YEAR,
department VARCHAR(20) NOT NULL,
address VARCHAR (50)
);
CREATE TABLE t_score(
id INT(10) PRIMARY KEY AUTO_INCREMENT,
stu_id INT(10) NOT NULL,
c_name VARCHAR(20),
grade INT(10)
);
INSERT INTO t_student VALUES( 901,'Boss Zhang' , 'Male',1985,'Computer Department', 'Beijing Haidian District');
INSERT INTO t_student VALUES( 902,'Zhang Laoer', 'Male',1986,'Chinese Department', 'Beijing Changping District' );
INSERT INTO t_student VALUES( 903,'Zhang San', 'Female',1990,'Chinese Department', 'Yongzhou City, Hunan Province');
INSERT INTO t_student VALUES( 904,'Li Si', 'Male',1990,' English Department', 'Fuxin City, Liaoning Province');
INSERT INTO t_student VALUES( 905,'Wang Wu', 'Female',1991,'English Department', 'Xiamen City, Fujian Province');
INSERT INTO t_student VALUES( 906, 'Wang Liu', 'Male', 1988, 'Computer Department', 'Hengyang City, Hunan Province');
INSERT INTO t_score VALUES(NULL,901, 'Computer',98);
INSERT INTO t_score VALUES(NULL,901, ' English', 80);
INSERT INTO t_score VALUES(NULL,902, 'Computer',65);
INSERT INTO t_score VALUES(NULL,902, 'Chinese',88);
INSERT INTO t_score VALUES(NULL,903, 'Chinese' ,95);
INSERT INTO t_score VALUES(NULL,904, 'Computer',70);
INSERT INTO t_score VALUES(NULL,904, 'English',92);
INSERT INTO t_score VALUES(NULL,905, 'English',94);
INSERT INTO t_score VALUES(NULL,906, 'Computer',90);
INSERT INTO t_score VALUES(NULL,906, 'English',85);
-- 2. Query all records in the student table
SELECT * FROM t_student;
-- 3 , Query the 2nd to 4th records of the student table
SELECT * FROM t_student LIMIT 1,3;
SELECT * FROM t_student LIMIT 1,3;
-- 4. Query the student number (id), name (name) of all students from the student table ) and department (department) information
SELECT id,name,department FROM t_student ;
-- 5. Query the information of students in the computer department and the English department from the student table (using the IN keyword)
SELECT * FROM t_student WHERE department IN ( 'Computer Department' OR 'English Department');
-- 6. Query the information of students aged 18~22 from the student table (use BETWEEN AND)
SELECT * FROM t_student WHERE YEAR(NOW())-birth BETWEEN 28 AND 32 ;
SELECT * FROM t_student WHERE YEAR(NOW())-birth BETWEEN 28 AND 32;
-- 7. Query the number of students in each department from the student table
SELECT department,COUNT(id) FROM t_student GROUP BY department;
-- 8. Query the highest score of each subject from the score table
SELECT c_name,MAX(grade) FROM t_score GROUP BY c_name;
-- 9. Query Li Si's exam Subject (c_name) and exam score (grade)
SELECT s.name,sc.c_name,sc.grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
WHERE s.name='Li Si';
- - 10. Query all students' information and exam information by connection
SELECT *
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
-- 11. Calculate the total score of each student
SELECT s.name, SUM(grade)
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
GROUP BY s.name;
-- 12. Calculate the average score of each test subject
SELECT sc.c_name,AVG(grade)
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
GROUP BY sc.c_name;
-- 13. Query the information of students whose computer grades are lower than 95
SELECT *
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
WHERE sc.grade<=95 AND sc.c_name='computer';
-- 14. Query the information of students who took the computer and English test at the same time
-- the first
SELECT *
FROM t_student
WHERE id IN
(SELECT stu_id
FROM t_score
WHERE c_name = 'computer' OR c_name = 'English'
GROUP BY stu_id
HAVING COUNT(*) = 2);
-- the second
SELECT *
FROM t_student
WHERE id IN
(SELECT stu_id
FROM t_score
WHERE stu_id
IN(SELECT stu_id FROM t_score WHERE c_name = 'computer') AND c_name = 'English');
-- the third
SELECT s.*
FROM t_student AS s, t_score AS b ,t_score AS c
WHERE s.id = b.stu_id AND b.c_name = 'computer' AND s.id = c.stu_id AND c.c_name = 'English';
-- first
SELECT *
FROM t_student
WHERE id IN
(SELECT stu_id
FROM t_score
WHERE c_name = ' Computer' OR c_name = 'English'
GROUP BY stu_id
HAVING COUNT(*) = 2);
-- second
SELECT *
FROM t_student
WHERE id IN
(SELECT stu_id
FROM t_score
WHERE stu_id
IN(SELECT stu_id FROM t_score WHERE c_name = 'computer') AND c_name = 'English');
-- the third
SELECT s.*
FROM t_student AS s, t_score AS b ,t_score AS c
WHERE s.id = b.stu_id AND b.c_name = 'computer' AND s.id = c.stu_id AND c.c_name = 'English';
-- 15. Sort computer test scores from high to low
SELECT sc.c_name,sc.grade
FROM t_score AS sc
WHERE sc.c_name='computer'
ORDER BY sc.grade DESC;
-- 16. Query the student number from the student table and the score table, and then merge the query results
SELECT s.id
FROM t_student AS s
UNION SELECT sc .stu_id
FROM t_score AS sc;
-- 17. Query the names, departments, examination subjects and grades of students surnamed Zhang or Wang
SELECT s.name,s.department,sc.c_name,sc.grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
WHERE s.name LIKE 'Zhang%' OR s.name LIKE 'Wang%';
-- 18. The query is the name, age, department, examination subject and grade of the students in Hunan
SELECT s.name,YEAR(NOW())-birth AS 'age',s.department,sc.c_name,sc.grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
WHERE s.address LIKE '%Hunan%';
SELECT s.name,YEAR(NOW())-birth AS 'age',s.department,sc.c_name,sc.grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc .stu_id
CREATE TABLE t_student(
id INT(10) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
sex VARCHAR(4),
birth YEAR,
department VARCHAR(20) NOT NULL,
address VARCHAR (50)
);
CREATE TABLE t_score(
id INT(10) PRIMARY KEY AUTO_INCREMENT,
stu_id INT(10) NOT NULL,
c_name VARCHAR(20),
grade INT(10)
);
INSERT INTO t_student VALUES( 901,'Boss Zhang' , 'Male',1985,'Computer Department', 'Beijing Haidian District');
INSERT INTO t_student VALUES( 902,'Zhang Laoer', 'Male',1986,'Chinese Department', 'Beijing Changping District' );
INSERT INTO t_student VALUES( 903,'Zhang San', 'Female',1990,'Chinese Department', 'Yongzhou City, Hunan Province');
INSERT INTO t_student VALUES( 904,'Li Si', 'Male',1990,' English Department', 'Fuxin City, Liaoning Province');
INSERT INTO t_student VALUES( 905,'Wang Wu', 'Female',1991,'English Department', 'Xiamen City, Fujian Province');
INSERT INTO t_student VALUES( 906, 'Wang Liu', 'Male', 1988, 'Computer Department', 'Hengyang City, Hunan Province');
INSERT INTO t_score VALUES(NULL,901, 'Computer',98);
INSERT INTO t_score VALUES(NULL,901, ' English', 80);
INSERT INTO t_score VALUES(NULL,902, 'Computer',65);
INSERT INTO t_score VALUES(NULL,902, 'Chinese',88);
INSERT INTO t_score VALUES(NULL,903, 'Chinese' ,95);
INSERT INTO t_score VALUES(NULL,904, 'Computer',70);
INSERT INTO t_score VALUES(NULL,904, 'English',92);
INSERT INTO t_score VALUES(NULL,905, 'English',94);
INSERT INTO t_score VALUES(NULL,906, 'Computer',90);
INSERT INTO t_score VALUES(NULL,906, 'English',85);
-- 2. Query all records in the student table
SELECT * FROM t_student;
-- 3 , Query the 2nd to 4th records of the student table
SELECT * FROM t_student LIMIT 1,3;
SELECT * FROM t_student LIMIT 1,3;
-- 4. Query the student number (id), name (name) of all students from the student table ) and department (department) information
SELECT id,name,department FROM t_student ;
-- 5. Query the information of students in the computer department and the English department from the student table (using the IN keyword)
SELECT * FROM t_student WHERE department IN ( 'Computer Department' OR 'English Department');
-- 6. Query the information of students aged 18~22 from the student table (use BETWEEN AND)
SELECT * FROM t_student WHERE YEAR(NOW())-birth BETWEEN 28 AND 32 ;
SELECT * FROM t_student WHERE YEAR(NOW())-birth BETWEEN 28 AND 32;
-- 7. Query the number of students in each department from the student table
SELECT department,COUNT(id) FROM t_student GROUP BY department;
-- 8. Query the highest score of each subject from the score table
SELECT c_name,MAX(grade) FROM t_score GROUP BY c_name;
-- 9. Query Li Si's exam Subject (c_name) and exam score (grade)
SELECT s.name,sc.c_name,sc.grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
WHERE s.name='Li Si';
- - 10. Query all students' information and exam information by connection
SELECT *
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
-- 11. Calculate the total score of each student
SELECT s.name, SUM(grade)
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
GROUP BY s.name;
-- 12. Calculate the average score of each test subject
SELECT sc.c_name,AVG(grade)
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
GROUP BY sc.c_name;
-- 13. Query the information of students whose computer grades are lower than 95
SELECT *
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
WHERE sc.grade<=95 AND sc.c_name='computer';
-- 14. Query the information of students who took the computer and English test at the same time
-- the first
SELECT *
FROM t_student
WHERE id IN
(SELECT stu_id
FROM t_score
WHERE c_name = 'computer' OR c_name = 'English'
GROUP BY stu_id
HAVING COUNT(*) = 2);
-- the second
SELECT *
FROM t_student
WHERE id IN
(SELECT stu_id
FROM t_score
WHERE stu_id
IN(SELECT stu_id FROM t_score WHERE c_name = 'computer') AND c_name = 'English');
-- the third
SELECT s.*
FROM t_student AS s, t_score AS b ,t_score AS c
WHERE s.id = b.stu_id AND b.c_name = 'computer' AND s.id = c.stu_id AND c.c_name = 'English';
-- first
SELECT *
FROM t_student
WHERE id IN
(SELECT stu_id
FROM t_score
WHERE c_name = ' Computer' OR c_name = 'English'
GROUP BY stu_id
HAVING COUNT(*) = 2);
-- second
SELECT *
FROM t_student
WHERE id IN
(SELECT stu_id
FROM t_score
WHERE stu_id
IN(SELECT stu_id FROM t_score WHERE c_name = 'computer') AND c_name = 'English');
-- the third
SELECT s.*
FROM t_student AS s, t_score AS b ,t_score AS c
WHERE s.id = b.stu_id AND b.c_name = 'computer' AND s.id = c.stu_id AND c.c_name = 'English';
-- 15. Sort computer test scores from high to low
SELECT sc.c_name,sc.grade
FROM t_score AS sc
WHERE sc.c_name='computer'
ORDER BY sc.grade DESC;
-- 16. Query the student number from the student table and the score table, and then merge the query results
SELECT s.id
FROM t_student AS s
UNION SELECT sc .stu_id
FROM t_score AS sc;
-- 17. Query the names, departments, examination subjects and grades of students surnamed Zhang or Wang
SELECT s.name,s.department,sc.c_name,sc.grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
WHERE s.name LIKE 'Zhang%' OR s.name LIKE 'Wang%';
-- 18. The query is the name, age, department, examination subject and grade of the students in Hunan
SELECT s.name,YEAR(NOW())-birth AS 'age',s.department,sc.c_name,sc.grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc.stu_id
WHERE s.address LIKE '%Hunan%';
SELECT s.name,YEAR(NOW())-birth AS 'age',s.department,sc.c_name,sc.grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id=sc .stu_id
WHERE s.address LIKE '%湖南%';