'SQL CROSS JOIN with WHERE CLAUSE not returning anything
I am trying to get a result for the query:
SELECT product, exportcountry
FROM is_exported_to CROSS JOIN is_produced_in
WHERE product = cheese AND origin = 'France';
But I cannot seem to get any return.
The two tables were created as follows:
CREATE TABLE is_produced_in(
cheese VARCHAR(32),
origin VARCHAR(32)
);
INSERT INTO is_produced_in (cheese, origin)
VALUES
('Brie', 'France'),
('Chrur', 'Tibet'),
('Halloumi', 'Greece'),
('Orgu', 'Turkey'),
('Salers', 'France'),
('Serrano', 'Brazil')
CREATE TABLE is_exported_to(
product VARCHAR(32),
exportcountry VARCHAR(32)
);
INSERT INTO is_exported_to (product, exportcountry)
VALUES
('Brie', 'Greece'),
('Halloumi', 'Brazil'),
('Halloumi', 'France'),
('Halloumi', 'Tibet'),
('Orgu', 'France'),
('Orgu', 'Tibet'),
('Salers', 'Greece'),
('Serrano', 'Tibet'),
('Serrano', 'Turkey')
Solution 1:[1]
You have missed ";" in your init scipt.
CREATE TABLE is_produced_in(
cheese VARCHAR(32),
origin VARCHAR(32)
);
INSERT INTO is_produced_in (cheese, origin)
VALUES
('Brie', 'France'),
('Chrur', 'Tibet'),
('Halloumi', 'Greece'),
('Orgu', 'Turkey'),
('Salers', 'France'),
('Serrano', 'Brazil');
CREATE TABLE is_exported_to(
product VARCHAR(32),
exportcountry VARCHAR(32)
);
INSERT INTO is_exported_to (product, exportcountry)
VALUES
('Brie', 'Greece'),
('Halloumi', 'Brazil'),
('Halloumi', 'France'),
('Halloumi', 'Tibet'),
('Orgu', 'France'),
('Orgu', 'Tibet'),
('Salers', 'Greece'),
('Serrano', 'Tibet'),
('Serrano', 'Turkey');
Query sql can be:
SELECT product, exportcountry
FROM is_exported_to CROSS JOIN is_produced_in
ON product = cheese AND origin = 'France';
Use "ON" instead of "WHERE"
Solution 2:[2]
There is no need to specify join condition in cross join. You can use the following query:
SELECT * from is_exported_to
CROSS JOIN is_produced_in
WHERE origin='France'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SeanH |
| Solution 2 | ejuhjav |
