Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: JOIN with nested queries

Tags:

sql

join

I am trying to perform this join operation. As I am new to sql I am finding problems understanding the syntax and stuff.

What do you think is wrong with the following query:

select top 1 * 
from 
    (select * 
     from dbo.transaction_unrated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and content_provider_code_id in (1)
    )   FULL OUTER JOIN 
    (select * 
     from dbo.transaction_rated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and entity_id in (1) 
            and mapping_entity_id = 1)
    ) 
    ON dbo.transaction_unrated.cst_id = dbo.transaction_rated.unrated_transaction_id
like image 387
mariner Avatar asked Jul 23 '12 20:07

mariner


People also ask

Which is better nested query or join?

A general rule is that joins are faster in most cases (99%). The more data tables have, the subqueries are slower. The less data tables have, the subqueries have equivalent speed as joins. The subqueries are simpler, easier to understand, and easier to read.

Which is faster nested query or join?

I won't leave you in suspense, between Joins and Subqueries, joins tend to execute faster. In fact, query retrieval time using joins will almost always outperform one that employs a subquery. The reason is that joins mitigate the processing burden on the database by replacing multiple queries with one join query.

How do you write a subquery in joins?

Rewriting Subqueries as JOINSA subquery using IN can be rewritten with the DISTINCT keyword, for example: SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table2); can be rewritten as: SELECT DISTINCT table1.

Can you inner join a subquery?

With INNER JOIN your Sub-Query will be execute only once and its records may gets stored internally in tempdb worktable on complex operations, then JOINed with the 1st table. With APPLY clause, the Sub-Query will be executed for every row in the 1st table.


1 Answers

You need to alias your derived tables.

select top 1 * 
from 
(
     select * 
     from dbo.transaction_unrated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and content_provider_code_id in (1)
) rsQuery1  
FULL OUTER JOIN 
(
     select * 
     from dbo.transaction_rated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and entity_id in (1) 
            and mapping_entity_id = 1)
) rsQuery2 ON rsQuery1.cst_id = rsQuery2.unrated_transaction_id

FULL OUTER JOIN is also unusual (in my experience). Are you sure that's what you want? Typically you will do an INNER JOIN which brings back rows that match on your criteria in both tables, or you will let one table be the driver and do a LEFT or RIGHT OUTER JOIN which will bring back all the rows in the driving table whether or not there is a match in the other table. A FULL OUTER JOIN will bring back all the rows in both tables regardless of whether they match.

like image 69
Bert Avatar answered Oct 16 '22 17:10

Bert