I have tried to do this query: What are the name of hospitals in Spain where work more than 2 doctors that only work for that hospital. But the result isn't what I expected.
I have these tables:
CREATE TABLE Hospital (
hid INT PRIMARY KEY,
name VARCHAR(127) UNIQUE,
country VARCHAR(127),
area INT
);
CREATE TABLE Doctor (
ic INT PRIMARY KEY,
name VARCHAR(127),
date_of_birth INT,
);
CREATE TABLE Work (
hid INT,
ic INT,
since INT,
FOREIGN KEY (hid) REFERENCES Hospital (hid),
FOREIGN KEY (ic) REFERENCES Doctor (ic),
PRIMARY KEY (hid,ic)
);
I tried with this:
SELECT DISTINCT H.name
FROM Doctor D, Work W, Hospital H
WHERE D.bi = W.bi AND H.country = 'Spain' AND H.hid = W.hid AND W.ic = D.ic
AND NOT EXISTS(
SELECT *
FROM Hospital H2
WHERE H2.hid = W.hid
)
GROUP BY (H.name)
HAVING COUNT(D.ic) > 2
;
Thanks.