58. Students Enrolled in Courses

An online education platform wants to list students along with the courses they are enrolled in. You are given two tables Students and Enrollments. Both tables share a common column named student_id.

Table: Students

╔════════════╦══════════╗
║   Column   ║   Type   ║
╠════════════╬══════════╣
║ student_id ║   int    ║
║────────────┼──────────║
║    name    ║ varchar  ║
╚════════════╩══════════╝
  • student_id is the primary key.

Table: Enrollments

╔════════════╦══════════╗
║   Column   ║   Type   ║
╠════════════╬══════════╣
║ student_id ║   int    ║
║────────────┼──────────║
║   course   ║ varchar  ║
╚════════════╩══════════╝
  • Each row represents a student enrolled in a course.

Write an SQL query using NATURAL JOIN only to return:

  • name
  • course

Return the result in any order.

Example 1:

Input:

Students:

╔════════════╦══════════╗
║ student_id ║   name   ║
╠════════════╬══════════╣
║     1      ║  Aarav   ║
║────────────┼──────────║
║     2      ║   Neha   ║
║────────────┼──────────║
║     3      ║  Rohan   ║
╚════════════╩══════════╝

Enrollments:

╔════════════╦═══════════╗
║ student_id ║  course   ║
╠════════════╬═══════════╣
║     1      ║   Math    ║
║────────────┼───────────║
║     1      ║  Physics  ║
║────────────┼───────────║
║     3      ║ Chemistry ║
╚════════════╩═══════════╝

Output:

╔══════════╦═══════════╗
║   name   ║  course   ║
╠══════════╬═══════════╣
║  Aarav   ║   Math    ║
║──────────┼───────────║
║  Aarav   ║  Physics  ║
║──────────┼───────────║
║  Rohan   ║ Chemistry ║
╚══════════╩═══════════╝

Explanation:

Neha has no enrollment, so she is excluded.

Only matching student_id values appear.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
 
Test Case

Input:

Students
Enrollments