"Not EXISTS (b except A)" solution under MySQL

Source: Internet
Author: User

Introduction to Database Systems 51 pages of the Chinese version of the sixth edition, there is an example of "not EXISTS (b except A)", asking for "Find students who have selected all the courses offered by the biology department." Experimental platform built to my blog search
The SQL commands on the book are as follows:
Select S.id, S.name
From student as S
Where NOT EXISTS (select course_id
From course
where dept_name = ' biology ')
Except
(Select t.course_id
From takes as T
where s.id = t.id));
This is no problem running on SQL Server, but if running under MYQL is the following error:
Error 1064 (42000): You have a error in your SQL syntax; Check the manual that
Corresponds to your MySQL server version for the right syntax to use near ' excep
T
(Select t.course_id
From takes as T
where s.id = t.id)) ' at line 6
Mysql>
Because MySQL does not support the except command, so we have to change the way to find out the students who have selected all the courses offered by the biology department.
In fact, not EXISTS (B except A) and not in the same. So, we can use the following SQL command to meet the query requirements:
First look at the records in the student table:
Mysql> select * from student;
+-------+----------+------------+----------+
| ID | name | Dept_name | tot_cred |
+-------+----------+------------+----------+
| 00128 | Zhang | Comp. Sci. | 102 |
| 12345 | Shankar | Comp. Sci. | 32 |
| 19991 | Brandt | History | 80 |
| 23121 | Chavez | Finance | 110 |
| 44553 | Peltier | Physics | 56 |
| 45678 | Levy | Physics | 46 |
| 54321 | Williams | Comp. Sci. | 54 |
| 55739 | Sanchez | Music | 38 |
| 70557 | Snow | Physics | 0 |
| 76543 | Brown | Comp. Sci. | 58 |
| 76653 | Aoi | Elec. Eng. | 60 |
| 98765 | Bourikas | Elec. Eng. | 98 |
| 98988 | Tanaka | Biology | 120 |
+-------+----------+------------+----------+
Rows in Set (0.00 sec)
Records in the takes table:
Mysql> select * from takes;
+-------+-----------+--------+----------+------+-------+
| ID | course_id | sec_id | Semester | Year | Grade |
+-------+-----------+--------+----------+------+-------+
| 00128 | CS-101 | 1 | Fall | 2009 | A |
| 00128 | CS-347 | 1 | Fall | 2009 | A-|
| 12345 | CS-101 | 1 | Fall | 2009 | C |
| 12345 | CS-190 | 2 | Spring | 2009 | A |
| 12345 | CS-315 | 1 | Spring | 2010 | A |
| 12345 | CS-347 | 1 | Fall | 2009 | A |
| 19991 | HIS-351 | 1 | Spring | 2010 | B |
| 23121 | FIN-201 | 1 | Spring | 2010 | c+ |
| 44553 | PHY-101 | 1 | Fall | 2009 | B-|
| 45678 | CS-101 | 1 | Fall | 2009 | F |
| 45678 | CS-101 | 1 | Spring | 2010 | B + |
| 45678 | CS-319 | 1 | Spring | 2010 | B |
| 54321 | CS-101 | 1 | Fall | 2009 | A-|
| 54321 | CS-190 | 2 | Spring | 2009 | B + |
| 55739 | MU-199 | 1 | Spring | 2010 | A-|
| 76543 | CS-101 | 1 | Fall | 2009 | A |
| 76543 | CS-319 | 2 | Spring | 2010 | A |
| 76653 | EE-181 | 1 | Spring | 2009 | C |
| 98765 | CS-101 | 1 | Fall | 2009 | C |
| 98765 | CS-315 | 1 | Spring | 2010 | B |
| 98988 | BIO-101 | 1 | Summer | 2009 | A |
| 98988 | BIO-301 | 1 | Summer | 2010 | NULL |
+-------+-----------+--------+----------+------+-------+
Rows in Set (0.00 sec)
Records in the course table:
Mysql> SELECT * from course;
+-----------+----------------------------+------------+---------+
| course_id | Title | Dept_name | Credits |
+-----------+----------------------------+------------+---------+
| BIO-101 | Intro. To Biology | Biology | 4 |
| BIO-301 | Genetics | Biology | 4 |
| BIO-399 | Computational Biology | Biology | 3 |
| CS-101 | Intro. to Computer | Comp. Sci. | 4 |
| CS-190 | Game Design | Comp. Sci. | 4 |
| CS-315 | Robotics | Comp. Sci. | 3 |
| CS-319 | Image Processing | Comp. Sci. | 3 |
| CS-347 | Database System Concepts | Comp. Sci. | 3 |
| EE-181 | Intro. to Digital Systems | Elec. Eng. | 3 |
| FIN-201 | Investment Banking | Finance | 3 |
| HIS-351 | World History | History | 3 |
| MU-199 | Music Video Production | Music | 3 |
| PHY-101 | Physical Principles | Physics | 4 |
+-----------+----------------------------+------------+---------+
Rows in Set (0.00 sec)
And then look at what the ' Biology ' department has in total:
Mysql> Select course_id
-> from Course
-> where dept_name = ' biology ';
+-----------+
| course_id |
+-----------+
| BIO-101 |
| BIO-301 |
| BIO-399 |
+-----------+
3 Rows in Set (0.00 sec)
Through observation, we can easily see that "to find the students who have taken all the courses offered by the biology department," The result is that only one is called Tanaka on the biology course.
So, we can change the book to except command:
Select DISTINCT S.id, s.name
From student as S, takes as T
where s.id = T.id and course_id in (
Select course_id
From course
where dept_name = ' biology ');
Query results:
+-------+--------+
| ID | name |
+-------+--------+
| 98988 | Tanaka |
+-------+--------+
1 row in Set (0.03 sec)
We changed the question to "find students who have enrolled in all courses offered by Comp. Sci."
Perform:
Select DISTINCT S.id, s.name
From student as S, takes as T
where s.id = T.id and course_id in (
Select course_id
From course
where dept_name = ' Comp. Sci. ');
Query results:
+-------+----------+
| ID | name |
+-------+----------+
| 00128 | Zhang |
| 12345 | Shankar |
| 45678 | Levy |
| 54321 | Williams |
| 76543 | Brown |
| 98765 | Bourikas |
+-------+----------+
6 rows in Set (0.00 sec)
Finish the Tutorial!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.