user2743855
user2743855

Reputation: 51

Sql Query Using Joins

I have 2 tables - student, exam

Student Table Schema:

id  Name  Course
1   Kavi   Dr.
2   Priya  Engg.

Exam Table Schema:

Examid Student_id  Exam Date
1        1          22-03-2014
2        2          23-04-2014
3        1          24-04-2014

My requirement is, i need to join these 2 tables,so that i can access all columns in that table. So far i have tried Query using Innerjoin but i get the result to be as

id  Name  Course
1   Kavi   Dr.
2   Priya  Engg.
1   1      22-03-2014
2   2      23-04-2014

But I need it like ,

id Name Course Exam Date
1  Kavi  Dr.    22-03-2014
1  Kavi  Dr.    24-04-2014
2  Priya Engg.  23-04-2014

Pls Help!

EDIT:

SELECT *
FROM STUDENT S INNER JOIN
     EXAM E
     ON S.id=E.Student_id 

Upvotes: 0

Views: 102

Answers (6)

slifty
slifty

Reputation: 13791

I'll give you the query, but first you should take a moment to understand how a select statement works.

When you run a select statement you can imagine it as defining a completely temporary table that only exists for a moment while you're looking things up. This is better known as the results of your query.

The list of columns you select actually become the columns of your new table. The "FROM" decides what tables are used to map those columns. The "WHERE" and "HAVING" clauses decide what rules the engine should use when determining the rows to include.

So, what columns do you want in your results?

  • student.id
  • student.name
  • student.course
  • exam.date

You are selecting from the student and exam tables, but you want rows to be "merged" in terms of the student ID (this is where the JOIN comes in -- you should look at this for a better understanding of how JOIN works)

Finally, it sounds like you want everything, so there is no WHERE / HAVING clause.

So your query should look like:

SELECT student.id, student.Name, student.Course, exam.Date
  FROM student
  JOIN exam ON student.id = exam.Student_id

Now, side note, you should keep a consistent naming structure for your columns. I suggest you always start lower case (so it would be student.course) and personally I avoid upper case letters in database definitions at all times. At the very least, things starting with a capitol letter usually indicate a class or object name in the land of programming

It sounds like you're interested in dealing with duplicates. You also want to look into key words like "DISTINCT" and "GROUP BY."

Also... you need to provide a lot more information when asking a question. Explain what you have tried. Explain all the requirements and the goals in an organized way. You get the idea.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269953

I believe the problem with your query is subtle. The query itself is correct, so this is what you want:

SELECT *
FROM STUDENT S INNER JOIN
     EXAM E
     ON S.id=E.Student_id 

Somehow, though, you are getting a cross join rather than an inner join. This could be happening because you are really executing:

SELECT *
FROM STUDENT S INNER JOIN
     EXAM E

In MySQL the on clause is optional, so you don't get an error. If so, just adding in the on clause should fix the problem.

Note: this could be cause by a semicolon in the query:

SELECT *
FROM STUDENT S INNER JOIN
     EXAM E;
     ON S.id=E.Student_id 

Upvotes: 0

Charaf JRA
Charaf JRA

Reputation: 8334

Try this Query:

SELECT Student.id ,Student.Name,Student.Course,`Exam.Exam Date`
FROM Student
INNER JOIN EXAM
ON Student.id=EXAM.Student_id;

Upvotes: 0

planestepper
planestepper

Reputation: 3307

SELECT
    S.id, S.Name, S.Course, `E.Exam Date`
FROM
    Student S, Exam E
WHERE
    S.id = E.Student_id

I still feel there's a serious design flaw on your tables - there's no table that defines the courses as entities from where to fetch the Course information (such as name). Some normalization would do you good, if you're able to change that.

Upvotes: 0

Suvash sarker
Suvash sarker

Reputation: 3170

Try this one

SELECT st.id,st.name,st.course,ex.date FROM student AS st 
LEFT JOIN exam AS ex ON ex.student_id=st.id

Upvotes: 0

Chibuzo
Chibuzo

Reputation: 6117

Try this query

SELECT s.*, e.exam_date FROM student_tbl s
JOIN exam_tbl e ON e.student_id = s.id

Upvotes: 0

Related Questions