Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 question about joins

Author  Topic 

zaidqis
Yak Posting Veteran

63 Posts

Posted - 2006-06-08 : 16:01:30
hi all
i have question
what is joins , i know that joins is used to get data from multie table ,,
i found explanation about joins but i cant understand it because i didnt understand the tables structure
so ..
can you explain how to use joins in this case
i have two table
first :: student table it contains
ID : the id of the student
Name : the name of the student
second :: marks table it contains
ID :the id of the student
mark : student mark
i want to make sql steatment get the name of the student from student table
and the mark of the student from marks table and use parameter to determine the student from his ID
thank you

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-06-08 : 16:05:47
An obvious homework question, but I'm bored. So I'll answer it even though we shouldn't answer homework questions.

DECLARE @StudentID

SELECT s.Name
FROM Student s
INNER JOIN Mark m
ON s.ID = m.ID
WHERE s.ID = @StudentID

Read your class materials for more information joins. If you still don't understand it, ask your teacher to help you with the concept.

Tara Kizer
aka tduggan
Go to Top of Page

zaidqis
Yak Posting Veteran

63 Posts

Posted - 2006-06-08 : 16:26:01
thank you
but really it is not homework , and i dont have teacher
i am 17 years old from north of iraq
and i am learnning english now ,, and trying to study vb 2005 , sql server 2005
thank you
Go to Top of Page

zaidqis
Yak Posting Veteran

63 Posts

Posted - 2006-06-08 : 16:28:03
i have a question
quote:
FROM Student s
INNER JOIN Mark m

why you put s after student and m after mark ??
thank you
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-06-08 : 16:33:19
Those are aliases. It makes it easier to read and shortens your code. After aliasing an object, you can now say AliasName.ColumnName instead of TableName.ColumnName. One to three letter aliases are what I prefer. Developers who use XML will typically give the alias a more descriptive name.

Tara Kizer
aka tduggan
Go to Top of Page

zaidqis
Yak Posting Veteran

63 Posts

Posted - 2006-06-08 : 16:42:02
thank you very much
Go to Top of Page

sprite007
Starting Member

3 Posts

Posted - 2006-06-08 : 18:45:20
I understand the select statements in the code, but I am not sure what the following does:

DECLARE @StudentID
...
...
WHERE s.ID = @StudentID

can't the query work with just the following:

SELECT s.Name, mark
FROM Student s
INNER JOIN Mark m
ON s.ID = m.ID

Please forgive me as I am just a beginner and I feel like I have a decent understanding of writing the queries but when the queries are transformed into scripts, I am lost and clueless.

Dan
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-06-08 : 18:47:05
The query will work if you don't use the variable. But it will return the data for all students found in both tables. The question was how to do it for one student using a variable.

I actually missed a step in the code. It should be:

DECLARE @StudentID int

SET @StudentID = SomeIDGoesHere

SELECT s.Name
FROM Student s
INNER JOIN Mark m
ON s.ID = m.ID
WHERE s.ID = @StudentID



Tara Kizer
aka tduggan
Go to Top of Page

sprite007
Starting Member

3 Posts

Posted - 2006-06-08 : 18:57:51
Thank you so much. Do you know where I can go to for more information on basic sql programming or scripting? I am in no way a programmer. Thank you for quick response.

Dan
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-06-08 : 19:01:14
I don't have a beginner's book to recommend. I'd search amazon.com and check out the reviews for the beginner T-SQL books. For advanced books, I'd recommend Ken Henderson's books:
http://www.sqlteam.com/store.asp

Tara Kizer
aka tduggan
Go to Top of Page
   

- Advertisement -