1

I am running into some difficulty with the following query.

SELECT maker, speed 
FROM  
(
    SELECT * 
    FROM product 
    NATURAL JOIN laptop
) 
WHERE hd > 30;

I am trying to find the maker and speed of laptops with hard drives greater than 30 gigabytes.

I have two relations laptops and products. Laptops has the tuples (maker, model, type) and laptop has the tuples (model, speed, ram, hd, screen, and price).

What I think I am doing.

  • Joining product with laptop with natural join, which I think and does (when submitted by itself) give me a relation of laptop but with two more columns maker and type.
  • I then am trying to just select maker and speed from that table where the size of the hd is greater than 30.
2
  • Are you receiving an error? If so, please show us the error. Are you receiving a resultset other than what you're expecting? If so, please show us. Commented Apr 3, 2012 at 19:56
  • What is the difficulty? Does your query give an error? What database are you using? Commented Apr 3, 2012 at 19:57

2 Answers 2

3

A subquery in the FROM clause requires a table alias:

SELECT maker, speed 
FROM  
(
    SELECT * 
    FROM product 
    NATURAL JOIN laptop
  /* include an alias with AS */
) AS products_sub
WHERE hd > 30;

From the docs:

A table_subquery is also known as a subquery in the FROM clause. Such subqueries must include an alias to give the subquery result a table name. A trivial example follows; see also Section 12.2.9.8, “Subqueries in the FROM Clause”.

However for your example, the subquery isn't needed at all.

SELECT maker, speed 
FROM products NATURAL JOIN laptop
WHERE hd > 30;

Note that NATURAL JOINs are not usually recommended, and it is best to be explicit about the columns joined in an ON clause.

Sign up to request clarification or add additional context in comments.

2 Comments

The subquery serves no puprose and can be omitted
+1 for "natural joins are not usually recommended". Very good point.
0
 SELECT maker, speed 
  FROM products   JOIN laptop 
  WHERE hd > 30;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.