Examine the fruit and wine tables:
Fruit
You execute this query:
SELECT fruited, fruitname FROM fruit
UNION
SELECT id, name, country FROM wine;
What is the result?
A.
The query succeeds and returns five columns of data.
B.
The query succeeds and returns two columns of data.
C.
The query falls because UNION does not work on tables with different number of
columns.
D.
The query falls because the number of columns in the SELECT in the SELECT clauses
are not equal.
I think is the C
I think it’s D.
The select works when there is an equal number of columns in both select statements:
SELECT fruited, fruitname from fruit
UNION
SELECT name, country from wine;
It’s either C or D but D seems to be the best due to the error I got.
create table fruit
(
fruitid int(11) primary key not null auto_increment,
fruitname varchar(30)
);
create table wine
(
id int(11) not null primary key auto_increment,
winename varchar(30),
country char(3)
);
select fruitid, fruitname from fruit
union
select id, winename, country from wine;
Error Code: 1222. The used SELECT statements have a different number of columns 0.000 sec
It’s D it doesn’t matter how many columns are in the tables, it matters how many columns are in the select statement
D
answer is D
not only the columns shoud be equal but also the data types
The data type can be different don’t lie. First test then write.
for example if you write:
SELECT id FROM wine
UNION
SELECT fruitname FROM fruit
The result will be
+—————+
|id
+—————+
|(numb)
|(numb)’
|(fruitname)
|(fruitname)—-+