Database Weekly Test Summary

-- 1.
CREATE TABLE 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 score(
    id INT(10) PRIMARY KEY AUTO_INCREMENT,
    stu_id INT(10) NOT NULL,
    c_name VARCHAR(20),
    grade INT(10)
);
-- 2. Query all records in the student table
SELECT * FROM student;
-- 3. Query the 2nd to 4th records of the student table
SELECT * from student LIMIT 1,2;
-- 4. Query the student number (id), name (name) and Department (department) information
SELECT id, name, department FROM 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 student WHERE department in ('Computer department','English department');
-- 6. From the student table Query the information of students aged 18~22 (using BETWEEN AND)
SELECT * FROM student WHERE YEAR(NOW()) - birth BETWEEN 18 and 22;
-- 7. Query the number of people in each department from the student table
SELECT department as 'department', count(*) as 'number of people' FROM student GROUP BY department;
-- 8. Query the highest score of each subject from the score table
SELECT c_name,MAX(grade) as 'highest score' FROM score GROUP BY c_name;
-- 9. Query Li Si's test subjects (c_name) and test scores (grade)
SELECT sc.c_name,sc.grade FROM student as s LEFT JOIN 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 student as s LEFT JOIN score as sc ON s.id = sc.stu_id;
-- 11. Calculate the total score of each student
SELECT s.name,SUM(sc.grade) as 'total grade' from student as s LEFT JOIN score as sc ON s.id = sc.stu_id GROUP BY s.name;
-- 12. Calculate the average of each test subject Grade
SELECT sc.c_name,avg(grade) as 'average grade' FROM score as sc GROUP BY sc.c_name;
-- 13. Query the information of students whose computer score is lower than 95
SELECT * FROM student as s LEFT JOIN score as sc on s.id=sc.stu_id WHERE c_name='computer' AND grade < 95;
-- 14. Query the information of students who took the computer and English test at the same time *
SELECT * FROM student as s WHERE id in (SELECT stu_id from score as sc
WHERE c_name = 'Computer' OR c_name = 'English' GROUP BY stu_id HAVING COUNT(*) = 2);
-- two
SELECT * FROM student WHERE id IN
(SELECT stu_id FROM score WHERE stu_id IN(SELECT stu_id FROM score WHERE c_name = 'Computer') AND c_name = 'English');
-- 15. Sort computer test scores from high to low
SELECT grade FROM score as sc WHERE c_name = 'computer' ORDER BY grade DESC;
-- 16. Query the student number from the student table and the score table, and then merge the query results
SELECT id FROM student as s UNION SELECT stu_id FROM 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 student as s
LEFT JOIN 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 and examination subject of the students in Hunan Grade
SELECT s.name,s.birth,s.department,sc.c_name,sc.grade FROM student as s LEFT JOIN score as sc ON s.id = sc.stu_id WHERE s.address LIKE 'Hunan%';

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325213763&siteId=291194637