Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 817223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:57:32+00:00 2026-05-15T01:57:32+00:00

Asking a simple question, just want everyone have fun to solve it. I got

  • 0

Asking a simple question, just want everyone have fun to solve it.
I got 2 tables.
1. Student
2. Course

Student

+----+--------+
| id | name   |
+----+--------+
|  1 | User1  |
|  2 | User2  |
+----+--------+

Course

+----+------------+------------+
| id | student_id | course_name|
+----+------------+------------+
|  1 |          1 | English    |
|  2 |          1 | Chinese    |
|  3 |          2 | English    |
|  4 |          2 | Japanese   |
+----+------------+------------+

I would like to get the result all student, who have taken English and Chinese, NOT English or Chinese.

Expected result:

    +----+------------+------------+
    | id | student_id | course_name|
    +----+------------+------------+
    |  1 |          1 | English    |
    |  2 |          1 | Chinese    |
    +----+------------+------------+

What we normally do is

select * from student join course on (student.id = course.student_id) WHERE course_name = 'English' OR course_name = 'Chinese'

but in this result I can get User2 record which is not my expected result. I want the record only display the User take the course English+Chinese only.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:57:33+00:00Added an answer on May 15, 2026 at 1:57 am

    Just join onto course twice: once for English, once for Chinese. That is:

    select student.*
    from student
         join course english_course on student.id = english_course.student_id
         join course chinese_course on student.id = chinese_course.student_id
    where english_course.course_name = 'English'
          and chinese_course.course_name = 'Chinese'
    

    or even

    select * from student
    where exists (select 1 from course
                  where course.course_name = 'English' and course.student_id = student.id)
          and exists (select 1 from course
                      where course.course_name = 'Chinese' and course.student_id = student.id);
    

    which will also eliminate duplicate (student_id,course_name) entries from course.

    I’m assuming (student_id,course_name) is indexed to drive both these. Your naming is a bit odd: the “course” table doesn’t describe a course, it describes the association from a student to a course. Personally, I’d call it “student_course” (or similar, maybe suffix “_map” or “_link”) and have it contain a “course_id” referencing a course table with an id and name.

    (I also prefer to have primary keys named consistently rather than calling them “id” in their own table, but that’s just being picky, and much more subjective)

    Just for fun:

    select student.*
    from student
         join course on student.id = course.student_id
    where course.course_name = 'English'
    intersect
    select student.*
    from student
         join course on student.id = course.student_id
    where course.course_name = 'Chinese'
    

    The reality is that using “intersect” to compare two result sets based on the same tables is a bit silly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Humbly asking for help with (of course) IE. I have a div that is
Just to make sure everyone understands what I am asking for, I will run
I feel somewhat foolish asking such a simple question, but I can't seem to
A simple question, I think: I want to put a tag into an ASP.NET
This is a very simple question and I feel stupid for asking it, but
Forgive me for asking such a simple question, I'm new to both HTML and
I feel bad about asking a question so simple, but I can't figure this
I am probably asking a noob question... however: I have a Drupal site that
I hope I'm not being too long winded in this question, I just want
After asking this question I was advised not to check in binaries into subversion.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.