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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:16:28+00:00 2026-06-12T16:16:28+00:00

My first query pulls out student details from 3 tables. Student, StudentAddress and StudentNote.

  • 0

My first query pulls out student details from 3 tables. Student, StudentAddress and StudentNote.

My query must list the student details, including id, name, address, note and find the earliest date they enrolled on a course.

I have created an aggregate query to do this.

SELECT STU_Name, MIN(ENR_StartDate)
FROM Student
INNER JOIN Enrolment
ON Student.STU_Student_ID = Enrolment.ENR_Student_ID
GROUP BY STU_Name

This is my main query which pull the details of the students.

SELECT STU_Name, ADD_Addr_Line_1, ADD_Addr_Line_2, ADD_Addr_Line_3, ADD_Postcode, NTE_Note, ENR_StartDate
FROM Student
INNER JOIN StudentAddress
ON Student.STU_Student_ID = StudentAddress.ADD_Student_ID
INNER JOIN StudentNote
ON Student.STU_Student_ID = StudentNote.NTE_Student_ID
INNER JOIN Enrolment
ON Student.STU_Student_ID = Enrolment.ENR_Student_ID

I want to link these queries so the result set tells me the earliest start date per student in the row.

Desired output for a student

row0: student1id, student1name, student1address, student1note1, student1startdate

row1: student1id, student1name, student1address, student1note2, student1startdate

This will allow a student to have multiple notes, but the start start will always be calculated as the same for a student.

I have built a test schema in sql fiddle.

http://sqlfiddle.com/#!2/cecea/4

Thanks for any 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-06-12T16:16:30+00:00Added an answer on June 12, 2026 at 4:16 pm

    Note that you have no noted for one of the students. That was corrected with an outer join.

    In another case, you have 2 notes for one student. So that student has two rows in the result.

    To resolve that, you need to choose a specific note (first, last, etc). You can do that using the same logic that is used to find the first enrollment record for each student.

    Adjusted to handle cases where the student may have more than one row for the same enrollment date. This approach can be used to break most any of these types of ties. Note that window functions are usually a better approach, but MySQL does not support them.

    SELECT STU_Name
         , ADD_Addr_Line_1
         , ADD_Addr_Line_2
         , ADD_Addr_Line_3
         , ADD_Postcode
         , NTE_Note
         , Enrolment.ENR_StartDate
         , Enrolment.ENR_ID
         , Student.STU_Student_ID
         , StudentAddress.ADD_ID
         , StudentNote.NTE_ID
      FROM Student
      JOIN StudentAddress
        ON Student.STU_Student_ID = StudentAddress.ADD_Student_ID
      LEFT JOIN StudentNote
        ON Student.STU_Student_ID = StudentNote.NTE_Student_ID
      JOIN Enrolment
        ON Student.STU_Student_ID = Enrolment.ENR_Student_ID
      LEFT JOIN Enrolment AS e2
        ON Enrolment.ENR_Student_ID = e2.ENR_Student_ID
       AND (Enrolment.ENR_StartDate, Enrolment.ENR_ID) > (e2.ENR_StartDate, e2.ENR_ID)
     WHERE e2.ENR_StartDate IS NULL
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My first query , retrieving date and hours worked from work_details of a given
The first query is: declare @myDate datetime = DATEADD(D,-2000,getdate()) SELECT * FROM [myTable] where
In the first query, I query list of steps for an ID. In the
I use my first query to get the id and name of the user
well this is my problem i use 2 source first query (select * from
I'm running a query that is timing out for first two times and returning
First, from BOL : Queries that modify table variables do not generate parallel query
I need to pull out the records whose First name,lastname and date of birth
I'm trying to pull out an object from a DbSet with a Linq query.
I am actually stuck in merging the result of this two queries: first query:

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.