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

SQL Syntax question, with 2 example questions

$
0
0
In the following code:

select name,
(select SUM(quantitysold) from tblproductsales
where tblProducts.id = productId)
from tblproducts
order by name

Why isn't it necessary to qualify productId? I know it comes from tblproductsales.

Here is my 2nd question:

Here are 2 code snippets with the original table code:


Comment: I much prefer the 1st snippet of code because it tells me the id for the product not sold by putting a 1 in the name column. Normally I would think that you do, select col1, col2 .....
So the syntax with ID name was confusing....? its like col1 col2 with no comma? Could someone please shed some light on what is happening and what other syntax rules like this might be lurking? :S

Andrew

create table tblproducts
(
id int,
name nvarchar(20),
description nvarchar(50)
);

create table tblproductsales
(
id int,
productid int,
unitprice numeric,
quantitysold int
);



-- non correlated SQ where this item was not ever sold

select ID name, description
from tblproducts
where ID NOT IN (select distinct productid from tblProductsales)

Results:

------------------------------
name description
1 52 inch tv
------------------------------

-- non correlated SQ where these items have been sold

select name, description
from tblproducts
where ID IN (select distinct productid from tblProductsales)


Results:

------------------------------
name description
tv 52 inch tv
------------------------------

Viewing all articles
Browse latest Browse all 13329

Trending Articles