Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Correlated subquery

$
0
0
Hi Everyone,
I am new to the forum and learning sql.

I have a sql problem where i dont understand what the query is doing and why i am getting an error?

--The query is supposed to do the following: It looks at the orders table to see if a match of the cnum and snum in each row corresponds to that
in the customers table. This code comes from book: Understanding SQL

Topic
-- use correlated query to find errors

select *
from orders main
where not snum = (select snum from Customers where cnum = main.cnum);


Result: Msg 512, Level 16, State 1, Line 5
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


Changing the code i get 1 row of data. When the error said more than one value does that mean more than one field of data?


select *
from orders main
where not snum IN (select snum from Customers where cnum = main.cnum);


Do the following query do the following? I just want to make sure i understand this right?

The orders table is the main part of the query but the predicate is looking for and snum value that is not present in the customers table. So the query looks at one line of the orders table and runs the subquery to find out if an snum value is present in the customers table where there the cnum values between the tables match? Is this correct?

Andrew

Viewing all articles
Browse latest Browse all 13329

Trending Articles