数据表:
例题:
1、查询student表的所有记录
mysql> select * from student;
2、查询student表的第2条到4条记录
mysql> select * from student LIMIT 1,3;
3、从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
mysql> select id,name,department from student;
4、从student表中查询计算机系和英语系的学生的信息
mysql> select * from student where department in ("计算机系", "英语系");
5、从student表中查询年龄1985~1990年份的学生信息
mysql> select * from student where birth between 1985 and 1990;
6、从student表中查询每个院系有多少人
mysql> select department,count(*) from student group by department;
7、从score表中查询每个科目的最高分
mysql> select c_name,max(grade) from score group by c_name;
8、查询李四的考试科目(c_name)和考试成绩(grade)
mysql> select c_name,grade from score where stu_id in (select id from student where name = "李四");
9、用连接的方式查询所有学生的信息和考试信息
mysql> select * from student left join score on student.id =score.stu_id;
10、计算每个学生的总成绩
mysql> select name,sum(score.grade) as total_grade from student join score on student.id=score.stu_id group by student.id,student.name;
11、计算每个考试科目的平均成绩
mysql> select score.c_name,avg(score.grade) as avg_grade from student join score on student.id=score.stu_id group by score.c_name;
12、查询计算机成绩低于95的学生信息
mysql> select student.* from student join score on student.id=score.stu_id where score.c_name='计算机' and score.grade < 95;
13、将计算机考试成绩按从高到低进行排序
mysql> select grade from score where c_name='计算机' order by grade desc;
14、查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
mysql> select name,department,c_name,grade from student left join score on student.id =score.stu_id where name regexp "^[王|张]";
15、查询都是北京的学生的姓名、年龄、院系和考试科目及成绩
mysql> select name,department,c_name,grade from student left join score on student.id =score.stu_id where address regexp "^北京";