I have two tables
TABLE 1 - Called Artista (artist) with an ID, Name, first year, second year.
ID NAME year1 year2 COUNTRY
41 Filipe Nobrega 2001 2051 Portugal
42 Bernardo Morais 2010 2060 Portugal
43 Fernando Evora 2013 2070 Portugal
44 Florenzo Giovanni 2003 2047 Italia
45 Tiago Alves 1980 1990 Portugal
46 Rui Gonzales 1975 1995 Espanha
47 Jose Almeida 1800 1876 Portugal
48 Jhon Snow 1900 1940 Winterfell
49 test 2001 2020 Espanha
TABLE 2 - Called autoria (author), with the ID of a piece of art and the ID of an artist, also it has the type of art( painting, music, sculpture...)
ART ARTIST TYPE_OF_ART
121 41 Pintura
122 41 Musica
123 42 Pintura
124 42 Cinema
125 42 Literatura
126 43 Teatro
127 43 Literatura
128 43 Danca
129 43 Arte_digital
130 43 Pintura
131 44 Pintura
132 44 Cinema
133 44 Pintura
134 45 Cinema
135 45 Literatura
136 46 Cinema
137 46 Literatura
138 46 Literatura
139 47 Arte_digital
140 47 Pintura
141 47 Teatro
142 48 Cinema
The problem is: Get all the artists that made less than 2 different pieces of art.
The result should be:
FILIPE NOBREGA - 41 he has 2 pieces of art
TIAGO ALVES - 45 he has 2 pieces of art
JOHN SNOW - 48 he has 1 piece of art
AND TEST - 49 he has 0
This is what I've got:
SELECT DISTINCT A.name, A.id
FROM artista A, autoria AUT
WHERE AUT.artist = A.id
GROUP BY(A.name, A.id)
HAVING (COUNT(*) <= 2);
And it returns all of the above except TEST.