8
I have some stored procedures in a SQL Server 2008 R2 database, these stored procedures have several joins, in some cases I used in it select, Inner Join and left Join, for example:
Tabela Pessoa
| IDPessoa | Nome |
| 1 | João |
| 2 | Maria |
| 3 | José |
Tabela Nascimento
| IDData | IDPessoa | DataNascimento |
| 1 | 1 | 01/01/2000 |
| 2 | 2 | 10/05/2001 |
| 3 | 3 | 25/09/2009 |
Tabela Telefone
| IDTelefone | IDPessoa | IDTipo | Numero |
| 1 | 1 | 1 | 1523-4565 |
| 2 | 3 | 5¹ | 8481-9847 |
¹ esse valor não existe na tabela TipoTelefone.
Tabela TipoTelefone
| IDTipo | Tipo |
| 1 | Comercial |
| 2 | Residencial |
SELECT
Nome,
DataNascimento,
Numero,
Tipo
FROM
Pessoa
INNER JOIN Nascimento ON (Pessoa.IDPessoa = Nascimento.IDPessoa)
LEFT JOIN Telefone ON (Pessoa.IDPessoa = Telefone.IDPessoa)
LEFT JOIN TipoTelefone ON (Telefone.IDTipo = TipoTelefone.IDTipo)
Note: the tables and the select above were only to exemplify the three cases:
- records that will always exist in both tables (Person x Birth)
- records that may not exist in one of the tables (Person x Phone); and
- records that depend on another table that is in the Join (Telephone x Tipotelefone)
I took a look at the question What is the difference between Ner Join and Uter Join? which explains in detail the joins, but it doesn’t talk about using more than one type of Join in a select.
I would like to know if there are any problems/implications of using joins exemplified above and if there is any point of attention when using this approach.
I don’t see any problem, because I’ve seen many queries in municipal systems that used a lot, but many joins and many Unions too, I think it all depends on the logic of your query, but using Unions leaves the query with less performance, I always try to avoid the use of Union, only in the last case where I cannot solve in other ways.
– Pablo Tondolo de Vargas
+1 by previous search and good explanation/formatting of the question, ;)
– Franchesco