1

So I've been working on a Java program to query a database using SQL. While the majority of it is set up, I'm having some trouble formulating some of the queries.

For example, one query is meant to every unique identifier, 'ID', from one table, and then return the set of exams they attend, from a separate table which uses 'ID' as a foreign key. I'm not really sure how to form this as a query and I'm really stumped. Any help would be really appreciated.

Thanks

1
  • All of the answers here seem to be similar but the answer I'm looking for hasn't been supplied. I can only assume that I've explained badly? What I want is a query (in SQL) such that I can easily (in Java) create: A single line containing the ID, followed by the name of each exam they take, listed in increasing alphabetical order, for every student. Commented Dec 5, 2015 at 17:54

3 Answers 3

1
SELECT s.s_ID, e.ID, e.Exam_code, e.Exam_year, e.Score FROM student s JOIN exam e ON s.s_ID = e.s_ID WHERE s.s_ID = 1
Sign up to request clarification or add additional context in comments.

4 Comments

@CAFC_Sam if you want only ID from student table and exam subjects from exam table then my answer is perfect for you!
This looks good, but can you please explain to me the 'exSubject1' and 'exSubject2'? I don't understand why those are there? If it helps to make things clearer, the table 'EXAM' has fields: 'ID, Exam_code, Exam_year, Score'
@CAFC_Sam thank you for providing your preferred values, So here we go: SELECT s.s_ID, e.ID, e.Exam_code, e.Exam_year, e.Score FROM student s JOIN exam e ON s.s_ID = e.s_ID WHERE s.s_ID = 1 | I have updated the answer you can check that as well :)
All of the answers here seem to be similar but the answer I'm looking for hasn't been supplied. I can only assume that I've explained badly? What I want is a query (in SQL) such that I can easily (in Java) create: A single line containing the ID, followed by the name of each exam they take, listed in increasing alphabetical order, for every student
1

Not enough rep., can't comment... )-;

Have you tried a JOIN operation?

For example, if your exams table has a row for each participant in each exam,

SELECT * FROM exams JOIN students ON exams.participant_student_id = students.id'

As a corollary,

SELECT * FROM exams JOIN students ON exams.participant_student_id = students.id where student.id = 123;

will get you a sub-set of the exams table for the student with ID = 123.

You should think about the best way to store your data in the database. I would suggest that a row per participant, per exam is reasonable for the structure of the exams table. This gives you more flexibility in future queries. The downside is that you will have to loop over the rows in your result set if you want the output to be a Set (or String).

3 Comments

I didn't think a join operation would work? Because I need each unique identifier to retrieve the list of exams they sit, so I need many of the same query (I'd guess) to execute on table? But would this mean I need to write it into a for loop in java, or something else? I'm not sure. I don't see how a join operation would help?
All of the answers here seem to be similar but the answer I'm looking for hasn't been supplied. I can only assume that I've explained badly? What I want is a query (in SQL) such that I can easily (in Java) create: A single line containing the ID, followed by the name of each exam they take, listed in increasing alphabetical order, for every student
Aghh, still not enough rep. to comment on the OP! What I wanted to propose is that you take a look at some ORM (e.g. Hibernate), which has a non-negligible learning curve, but gives exactly what you asked for. See also Java Persistence API (JPA).
1

SELECT * FROM TABLE1 AS t1 JOIN TABLE2 AS t2 ON t2.id = t1.id

where t1 is the table with id as foreign key

1 Comment

All of the answers here seem to be similar but the answer I'm looking for hasn't been supplied. I can only assume that I've explained badly? What I want is a query (in SQL) such that I can easily (in Java) create: A single line containing the ID, followed by the name of each exam they take, listed in increasing alphabetical order, for every student

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.