MySQL綜合練習

這裡寫圖片描述

INSERT INTO t_student VALUES( 901,’張老大’, ‘男’,1985,’計算機系’, ‘北
京市海淀區’);
INSERT INTO t_student VALUES( 902,’張老二’, ‘男’,1986,’中文系’, ‘北京
市昌平區’);
INSERT INTO t_student VALUES( 903,’張三’, ‘女’,1990,’中文系’, ‘湖南省
永州市’);
INSERT INTO t_student VALUES( 904,’李四’, ‘男’,1990,’英語系’, ‘遼寧省
阜新市’);
INSERT INTO t_student VALUES( 905,’王五’, ‘女’,1991,’英語系’, ‘福建省
廈門市’);
INSERT INTO t_student VALUES( 906,’王六’, ‘男’,1988,’計算機系’, ‘湖南
省衡陽市’);
INSERT INTO t_score VALUES(NULL,901, ‘計算機’,98);
INSERT INTO t_score VALUES(NULL,901, ‘英語’, 80);
INSERT INTO t_score VALUES(NULL,902, ‘計算機’,65);
INSERT INTO t_score VALUES(NULL,902, ‘中文’,88);
INSERT INTO t_score VALUES(NULL,903, ‘中文’,95);
INSERT INTO t_score VALUES(NULL,904, ‘計算機’,70);
INSERT INTO t_score VALUES(NULL,904, ‘英語’,92);
INSERT INTO t_score VALUES(NULL,905, ‘英語’,94);
INSERT INTO t_score VALUES(NULL,906, ‘計算機’,90);
INSERT INTO t_score VALUES(NULL,906, ‘英語’,85);

1.寫出兩張表
DROP TABLE IF EXISTS t_student;
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)
);

DROP TABLE IF EXISTS t_score;
CREATE TABLE t_score(
id INT(10) PRIMARY KEY AUTO_INCREMENT,
stu_id INT(10) NOT NULL,
c_name VARCHAR(20),
grade INT(20)
);

2– 查詢student表的所有記錄
SELECT * FROM t_student ;

3– 查詢student表的第2條到4條記錄
SELECT * FROM t_student LIMIT 1,3;

4– 從student表查詢所有學生的學號(id)、姓名(name)和院系(department)的資訊
SELECT id, name, department FROM t_student;

5– 從student表中查詢計算機系和英語系的學生的資訊(用 IN 關鍵字)
SELECT * FROM t_student WHERE department in (‘計算機系’,’英語系’);

6– 從student表中查詢年齡18~22歲的學生資訊(用 BETWEEN AND)
SELECT * FROM t_student WHERE (year(NOW())-birth) BETWEEN 18 AND 27;

7– 從student表中查詢每個院系有多少人
SELECT department,COUNT(*) FROM t_student GROUP BY department;

8– 從score表中查詢每個科目的最高分
SELECT c_name,MAX(grade) FROM t_score GROUP BY c_name;

9– 查詢李四的考試科目(c_name)和考試成績(grade)
SELECT s.name, sc.c_name AS ‘考試科目’,sc.grade AS ‘考試成績’
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id = sc.stu_id
WHERE name=’李四’;

10– 用連線的方式查詢所有學生的資訊和考試資訊
SELECT*
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id = sc.stu_id;

11– 計算每個學生的總成績
SELECT 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– 計算每個考試科目的平均成績
SELECT 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– 查詢計算機成績低於95的學生資訊
SELECT *
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id = sc.stu_id
WHERE c_name = ‘計算機’ AND grade<95;

SELECT *
FROM t_student
WHERE id in
(SELECT stu_id FROM t_score
WHERE c_name= ‘計算機’AND grade < 95 );

)

14– 查詢同時參加計算機和英語考試的學生的資訊
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 = ‘計算機’) AND c_name = ‘英語’);

15– 將計算機考試成績按從高到低進行排序
SELECT c_name,grade
FROM t_score
WHERE c_name = ‘計算機’
ORDER BY grade DESC;

16– 從student表和score表中查詢出學生的學號,然後合併查詢結果
SELECT name, sc.stu_id AS ‘學生學號’
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id = sc.stu_id;

17– 查詢姓張或者姓王的同學的姓名、院系和考試科目及成績
SELECT name, department,c_name,grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id = sc.stu_id
WHERE name like ‘王%’OR name LIKE ‘張%’;

18– 查詢都是湖南的學生的姓名、年齡、院系和考試科目及成績
SELECT name, (year(NOW())- birth) as age,department,grade
FROM t_student AS s
LEFT JOIN t_score AS sc
ON s.id = sc.stu_id
WHERE address like ‘湖南%’;