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 7091911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:14:48+00:00 2026-05-28T08:14:48+00:00

For example: I have a database of students and last classes, and for each

  • 0

For example:

I have a database of students and last classes, and for each student in a class, I also return a list of all the other students in the class. Sample table would be like

StudentID ClassID
a          1
b          1
c          1
a          2
a          3
c          2
b          3

I want to select of studentID = a classes, but also know what other students will be in his class as so:

StudentID  ClassID  Classmates
a          1        a,b,c
a          2        a,c
a          3        a,b

I tried doing a query like this:

SELECT * FROM
(SELECT *, GROUP_CONCAT(StudentID)
FROM enrolled GROUP BY studentID, ClassID)
AS temporary WHERE temporary.StudentID=a

The problem is that GROUP BY condenses the rows, so returning a list of b‘s classes wouldnt show anything because a is prioritized in the studentID column.

I researched everywhere and couldn’t find anything — help?

  • 1 1 Answer
  • 0 Views
  • 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-28T08:14:49+00:00Added an answer on May 28, 2026 at 8:14 am

    Perhaps you’re looking for something like this:

    select e.studentid, e.classid, dt.classmates
    from (
        select classid, group_concat(studentid) as classmates
        from enrolled
        group by classid
    ) as dt
    join enrolled e on e.classid = dt.classid
    where e.studentid = 'a'
    

    The basic idea is to generate the classmates lists by grouping on just classid and then join that to enrolled so that you can select the student you’re interested in.

    When you use where e.studentid = 'a', you get this:

    +-----------+---------+------------+
    | studentid | classid | classmates |
    +-----------+---------+------------+
    | a         |       1 | a,b,c      |
    | a         |       2 | a,c        |
    | a         |       3 | a,b        |
    +-----------+---------+------------+
    

    filtering on e.studentid = 'b' yields:

    +-----------+---------+------------+
    | studentid | classid | classmates |
    +-----------+---------+------------+
    | b         |       1 | a,b,c      |
    | b         |       3 | a,b        |
    +-----------+---------+------------+
    

    and filtering on e.studentid = 'c' gives you this:

    +-----------+---------+------------+
    | studentid | classid | classmates |
    +-----------+---------+------------+
    | c         |       1 | a,b,c      |
    | c         |       2 | a,c        |
    +-----------+---------+------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have these tables: STUDENTS: Student ID - First name - Last name
I have 3 tables in my SQL Server database: Teacher Student Parent Each of
I have an example database, it contains tables for Movies, People and Credits. The
I have a database that contains a history of product sales. For example the
For example, if let's say I have a database of users. I have a
In my database I have tables that define types for example Table: Publication Types
Does anybody have an example of working with database using Visual C++ and OLEDB?
If I have a collection of database tables (in an Access file, for example)
I have 2 websites, lets say - example.com and example1.com example.com has a database
I have a big doubt. Let's take as example a database for a whatever

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.