Click here to Skip to main content
16,011,611 members
Home / Discussions / Database
   

Database

 
AnswerRe: combining multiple rows into single column Pin
Eric Dahlvang7-Sep-06 9:26
Eric Dahlvang7-Sep-06 9:26 
Questionfunction vs stored procedure in sqlserver 2000 Pin
yazan_zahi7-Sep-06 1:42
yazan_zahi7-Sep-06 1:42 
AnswerRe: function vs stored procedure in sqlserver 2000 Pin
SeMartens7-Sep-06 1:52
SeMartens7-Sep-06 1:52 
AnswerRe: function vs stored procedure in sqlserver 2000 Pin
saqib827-Sep-06 4:29
saqib827-Sep-06 4:29 
AnswerRe: function vs stored procedure in sqlserver 2000 Pin
Sumitjh8213-Sep-06 20:02
Sumitjh8213-Sep-06 20:02 
QuestionHow would i query these tables? Pin
Red_Wizard_Shot_The_Food7-Sep-06 0:15
Red_Wizard_Shot_The_Food7-Sep-06 0:15 
AnswerRe: How would i query these tables? Pin
SeMartens7-Sep-06 1:48
SeMartens7-Sep-06 1:48 
AnswerRe: How would i query these tables? Pin
Mike Dimmick7-Sep-06 2:13
Mike Dimmick7-Sep-06 2:13 
To retrieve data from multiple tables in a single query, you join the tables. To do this, you use the JOIN keyword:
SELECT *
FROM CD 
JOIN CDTrack ON CD.id = CDTrack.cdid
In the ON clause, you give a boolean expression; the database engine conceptually evaluates every combination of rows from both tables, and if this expression evaluates to True for a given combination, the row is included in the results (unless further constrained by a WHERE or HAVING clause). If the boolean expression uses only equality expressions it's called an equijoin.

There are a number of types of joins. The most common, and the default if you don't specify a type of join, is an inner join. Here, only rows where the ON expression evaluates to True are included. There are also left (outer) joins, where if there are no rows on the right-hand side where the expression is true for a given row on the left-hand side, the output contains the data from the row on the left, while the fields that should contain the data from the right-hand side are set to NULL. A right (outer) join is the same as a left outer join, but with the roles of the left and right tables reversed. There's also a full outer join where rows from both tables with no match from the other table are output. Finally there's a cross join which simply outputs every row from the left-hand table combined with every row from the right-hand table - this uses no ON clause.

The outer joins are generally useful when you're joining a live table to a reference lookup table and can't guarantee that you have the corresponding data in the reference table, but it's imperative that you return all rows from the live table.

You can chain together join operations into a larger query, so for example to get a complete listing of every track on every CD you would write:
SELECT *
FROM CD
JOIN CDTrack ON CD.id = CDTrack.cdid
JOIN Track ON CDTrack.trackid = Track.id
JOIN ArtistTrack ON CDTrack.trackid = ArtistTrack.trackid
JOIN Artist ON ArtistTrack.artistid = Artist.id
To know which columns to include in an ON clause requires you to know the schema and what each column represents. In that example we could have joined ArtistTrack either using CDTrack.trackid or Track.id since they have the same value. I'm not sure whether it makes any difference to the query optimizer - probably not, so whichever is clearer.

Mentioning the query optimizer - the optimizer will normally reorder the joins into whatever order it thinks will execute most quickly, taking into account any filter expressions in a WHERE clause. You can force it to execute in the order you specify (might be useful for avoiding deadlocks) by adding OPTION (FORCE ORDER) to the end of the query (on Microsoft SQL Server).

Once you have joined tables together, you may have some ambiguous column names - for example, here all tables have a column named 'id'. You must qualify an ambiguous column name with the table name (e.g. 'Track.id'). Best practice is to always specify the table name, so that your query is robust if columns are later added to one of the tables which clash with a column name from one of the other tables. To avoid having to type out the full name of a table with a long name, you can add an alias for a table to the query, which goes after the table name (optionally after the AS keyword) in the FROM/JOIN clause.

Stability. What an interesting concept. -- Chris Maunder

GeneralRe: How would i query these tables? Pin
Red_Wizard_Shot_The_Food7-Sep-06 22:55
Red_Wizard_Shot_The_Food7-Sep-06 22:55 
QuestionC# DataSet DataAdapter Design Pin
llap4446-Sep-06 5:03
llap4446-Sep-06 5:03 
AnswerRe: C# DataSet DataAdapter Design Pin
Stephen McGuire6-Sep-06 7:02
Stephen McGuire6-Sep-06 7:02 
GeneralRe: C# DataSet DataAdapter Design Pin
llap4446-Sep-06 7:34
llap4446-Sep-06 7:34 
QuestionTranforming two text files using DTS Pin
dayakar_dn6-Sep-06 2:42
dayakar_dn6-Sep-06 2:42 
QuestionRunning Program(on VB .NET) Without SQL Pin
FriendlySoluations6-Sep-06 1:24
FriendlySoluations6-Sep-06 1:24 
AnswerRe: Running Program(on VB .NET) Without SQL Pin
Colin Angus Mackay6-Sep-06 3:14
Colin Angus Mackay6-Sep-06 3:14 
AnswerRe: Running Program(on VB .NET) Without SQL Pin
Stephen McGuire6-Sep-06 5:27
Stephen McGuire6-Sep-06 5:27 
Question.NET and metadata of database Pin
Imtiaz Murtaza5-Sep-06 22:51
Imtiaz Murtaza5-Sep-06 22:51 
Questionstoere procedure Pin
yazan_zahi5-Sep-06 22:44
yazan_zahi5-Sep-06 22:44 
AnswerRe: stoere procedure Pin
Eric Dahlvang6-Sep-06 12:11
Eric Dahlvang6-Sep-06 12:11 
Questionhow to pass a parameter in procedure Pin
yazan_zahi5-Sep-06 22:03
yazan_zahi5-Sep-06 22:03 
AnswerRe: how to pass a parameter in procedure Pin
albCode5-Sep-06 22:10
albCode5-Sep-06 22:10 
GeneralRe: how to pass a parameter in procedure Pin
yazan_zahi5-Sep-06 22:17
yazan_zahi5-Sep-06 22:17 
GeneralRe: how to pass a parameter in procedure Pin
albCode5-Sep-06 22:37
albCode5-Sep-06 22:37 
QuestionEditing a MS Access database Pin
myNameIsRon5-Sep-06 21:28
myNameIsRon5-Sep-06 21:28 
AnswerRe: Editing a MS Access database Pin
Stephen McGuire6-Sep-06 5:22
Stephen McGuire6-Sep-06 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.