大连理工大学软件学院·数据库实验

  • Find the ID, names of all the students from departments whose name contain character ‘功’.
mysql> select id, name, dept_name from student where dept_name like '%功%';
+-------+--------+-----------+
| id    | name   | dept_name |
+-------+--------+-----------+
| 00128 | 丘处机 | 内功学院  |
| 12345 | 段誉   | 内功学院  |
| 54321 | 杨过   | 内功学院  |
| 76543 | 段正淳 | 内功学院  |
+-------+--------+-----------+
4 rows in set
  • Find the ID, names and total credits of students in 邪门学院 department or in 兵器学院 department whose total credits are higher than 50 credits.
//AND和OR的优先级:AND > OR, 去掉括号结果错误!!!
mysql> select id, name, tot_cred,dept_name
    -> from student
    -> where (dept_name='邪门学院' or dept_name='兵器学院') and tot_cred>50;
+-------+--------+----------+-----------+
| id    | name   | tot_cred | dept_name |
+-------+--------+----------+-----------+
| 19991 | 林平之 | 80       | 兵器学院  |
| 23121 | 穆念慈 | 110      | 兵器学院  |
| 44553 | 蓝凤凰 | 56       | 邪门学院  |
| 76653 | 胡斐   | 60       | 兵器学院  |
| 98765 | 李莫愁 | 98       | 兵器学院  |
+-------+--------+----------+-----------+
5 rows in set
  • For the instructor 83821, show course_id and title of all courses taught by the instructor.
mysql> select course.course_id, title, sec_id
    -> from course, teaches
    -> where course.course_id = teaches.course_id and id='83821';
+-----------+----------+--------+
| course_id | title    | sec_id |
+-----------+----------+--------+
| cn2       | 九阴真经 | 1      |
| cn2       | 九阴真经 | 2      |
| cn4       | 易筋经   | 2      |
+-----------+----------+--------+
3 rows in set
  • As above, but show the total number of credits for such courses (taught by that instructor). You should use SQL aggregation on courses taught by that instructor.
mysql> select course.course_id, title, sum(credits)
    -> from course, teaches
    -> where course.course_id=teaches.course_id and id='83821'
    -> group by course.course_id, title;
+-----------+----------+--------------+
| course_id | title    | sum(credits) |
+-----------+----------+--------------+
| cn2       | 九阴真经 | 8            |
| cn4       | 易筋经   | 3            |
+-----------+----------+--------------+
2 rows in set
  • As above, but display the total credits for each of the instructors, along with the ID of the instructor; don’t bother about the name of the instructors. (Don’t bother about instructors who have not taught any course, they can be omitted)
mysql> select id, sum(credits)
    -> from teaches, course
    -> where course.course_id=teaches.course_id
    -> group by id;
+-------+--------------+
| id    | sum(credits) |
+-------+--------------+
| 10101 | 10           |
| 12121 | 3            |
| 15151 | 3            |
| 22222 | 4            |
| 32343 | 3            |
| 45565 | 7            |
| 76766 | 8            |
| 83821 | 11           |
| 98345 | 3            |
+-------+--------------+
9 rows in set
  • Find average instructors’ salaries for each of courses, along with the course_id and title of the course, taught by instructors of 内功学院, the result should be sorted from the lowest to the highest according to the average salaries.
mysql> select course.course_id, title, avg(salary)
    -> from course, instructor, teaches
    -> where instructor.id=teaches.id and teaches.course_id=course.course_id and instructor.dept_name='内功学院'
    -> group by course.course_id, title
    -> order by avg(salary);
+-----------+----------+-------------+
| course_id | title    | avg(salary) |
+-----------+----------+-------------+
| cn5       | 太极     | 65000       |
| cn3       | 九阳神功 | 65000       |
| cn1       | 内功基础 | 70000       |
| cn4       | 易筋经   | 83500       |
| cn2       | 九阴真经 | 92000       |
+-----------+----------+-------------+
5 rows in set
  • Find the names of all courses which have been taught in 南疆雨林 ever. (there should be no duplicate names)
mysql> select distinct title
    -> from course, section
    -> where course.course_id=section.course_id and building='南疆雨林';
+----------+
| title    |
+----------+
| 枪法     |
| 内功基础 |
| 坑蒙拐骗 |
+----------+
3 rows in set
  • Display the IDs and names of all students who have never registered for a course.
mysql> select id, name
    -> from student
    -> where tot_cred=0;
+-------+------+
| id    | name |
+-------+------+
| 70557 | 杨康 |
+-------+------+
1 row in set
  • Find the id and names of the courses which have been registered by some students without evaluated grade.
mysql> select course.course_id, title, grade
    -> from course, takes
    -> where course.course_id=takes.course_id and grade is null;
+-----------+-------+-------+
| course_id | title | grade |
+-----------+-------+-------+
| cq2       | 散打  | NULL  |
+-----------+-------+-------+
1 row in set
  • Find the courses which are the Subsequence courses of other courses. The result should involve the ids and titles of the Subsequence courses and the ids and titles of its prerequisites. (note: the names of columns in result should show the roles of the courses clearly)
mysql> select a.course_id, a.title SUB, b.course_id, b.title PRE
    -> from course a, course b, prereq
    -> where a.course_id=prereq.course_id and b.course_id=prereq.prereq_id;
+-----------+----------+-----------+----------+
| course_id | SUB      | course_id | PRE      |
+-----------+----------+-----------+----------+
| cn2       | 九阴真经 | cn1       | 内功基础 |
| cn3       | 九阳神功 | cn1       | 内功基础 |
| cn4       | 易筋经   | cn1       | 内功基础 |
| cn5       | 太极     | cn1       | 内功基础 |
| cq2       | 散打     | cq1       | 少林长拳 |
| cq3       | 自由搏击 | cq1       | 少林长拳 |
+-----------+----------+-----------+----------+
6 rows in set

你可能感兴趣的:(数据库)