1

I'm a complete SQL noob and have no idea how to utilize JOINs. If someone could help with this query, it would be great.

I have a table questions which contains two columns: queid and que. Another table options, contains the corresponding options for the questions, and has columns optionid, queid,option.

How can I do a SELECT statement such that I can join both tables together based on queid ?

Something like:

SELECT * from questions,options where queid=1

5 Answers 5

2

You should try this:

SELECT que.*, opt.* FROM questions que
INNER JOIN options opt ON que.queid = opt.queid
WHERE que.queid = 1

INNER JOIN loads questions and options having at least one corresponing record in every table.

If you need to get all questions (even the ones not having options) you could use

SELECT que.*, opt.* FROM questions que
LEFT JOIN options opt ON que.queid = opt.queid
WHERE que.queid = 1

LEFT JOIN always loads questions and, if they have options, their options too; if not you get NULL for options columns.

Sign up to request clarification or add additional context in comments.

2 Comments

I get a ambiguous column queid in where clause message.
@xbonez: maybe you were reading my answer while I was editing it. Try it now
2

May be by

SELECT * FROM questions q JOIN options o ON q.queid=o.queid WHERE q.queid=1

Comments

2
SELECT q.*,o.* FROM questions q 
JOIN options o ON  q.queid = o.queid
WHERE q.queid = 1

1 Comment

it should be q.queid = o.queid.
0
SELECT *
FROM questions
JOIN options ON questions.queid = options.queid
WHERE questions.queid = 1

Comments

0

You can join two related tables using the column that they have in common.

in your case you can write :

SELECT * FROM Questions as Q INNER JOIN Options O ON Q.queid=O.queid WHERE Q.quid=1

You can also omit the where part like this :

 SELECT * FROM Questions as Q INNER JOIN Options O ON Q.queid=O.queid AND Q.quid=1

There are different kind of joins :

INNER

OUTER(Left,Right,Full)

By inner join you mean that only records that are common in both tables are returned When you use outer join all the records on the given side are return plus the records that have corresponding values on the other side otherwise instead of the other side values you will get null.

Comments

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.