大連理工大學軟件學院·數據庫實驗

  • 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章