1

Could anyone help me understand why this query is not working?

Modify the query to list all the products (product) and their associated category (product_category) if they have one. Otherwise, do not display a value (null) for the category.

-- SQL request(s)​​​​​​‌​​‌​​​‌​‌‌‌‌​‌​​‌​​​​​‌‌ below
SELECT P.name as PRODUCT_NAME, PC.name as CATEGORY_NAME
FROM product as P
OUTER JOIN PRODUCT_CATEGORY as PC on P.product_id =  PC.product_category_id

it keeps showing me this error:

Syntax error in SQL statement "                       
SELECT P.NAME AS PRODUCT_NAME, PC.NAME AS CATEGORY_NAME
FROM PRODUCT AS P
OUTER[*] JOIN PRODUCT_CATEGORY AS PC ON P.PRODUCT_ID =  PC.PRODUCT_CATEGORY_ID
"; SQL statement:
-- SQL request(s) below
SELECT P.name as PRODUCT_NAME, PC.name as CATEGORY_NAME
FROM product as P
OUTER JOIN PRODUCT_CATEGORY as PC on P.product_id =  PC.product_category_id
 [42000-200]

enter image description here

Thanks a lot

Adouani Riadh
  • 1,162
  • 2
  • 14
  • 37

3 Answers3

2

OUTER JOIN is not valid syntax in ANSI SQL or any implementation I've used.

Here's a good reference: https://crate.io/docs/sql-99/en/latest/chapters/30.html#joined-tables

You can say JOIN or INNER JOIN. These are synonyms.

You can say LEFT JOIN or LEFT OUTER JOIN. These are synonyms.

You can say RIGHT JOIN or RIGHT OUTER JOIN. These are synonyms.

You can say FULL JOIN or FULL OUTER JOIN. These are synonyms.

You should see the pattern now. The words INNER and OUTER are optional keywords.

OUTER JOIN (without LEFT, RIGHT, or FULL) is not one of the choices.

I suggest to satisfy your recruiter, you use LEFT OUTER JOIN.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Thanks. I have tested your answer and It works like a charm. Thanks for the reference I will used it for my future preparation :) – Adouani Riadh Apr 12 '21 at 03:16
1

Hmm, I don't know if OUTER JOIN is synonymous with FULL [OUTER] JOIN in any DBMS... So OUTER JOIN should probably read FULL OUTER JOIN or simply FULL JOIN.

But regardless, you don't need a full join here. A full join would list categories without a product. You need a a left join.

SELECT p.name AS product_name,
       pc.name AS category_name
       FROM product AS p
            LEFT JOIN product_category AS pc
                      ON p.product_id = pc.product_category_id;
sticky bit
  • 36,626
  • 12
  • 31
  • 42
0

as long as category null values are not allowed, then I think you should use inner join

Israa
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 02 '21 at 13:11