Pergunta

I am beginner at SQL and I am trying to create a query.

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)
 );

The query is: What's the name and the IC of doctors born after 1980 and who work in England and Spain? Thanks

Foi útil?

Solução 2

Try this:

SELECT d.name, d.ic
FROM doctor d
JOIN work w ON d.ic = i.ic
JOIN hospital h ON w.hid = h.hid
WHERE YEAR(d.date_of_birth) >= 1980
AND UPPER(h.country) IN ('ENGLAND', 'SPAIN');

I hope this is enough.

Outras dicas

You can use something similar to this:

select d.name
from doctor d
left join work w
  on d.ic = w.ic
left join hospital h
  on w.hid = h.id
where d.date_of_birth >= '1980-01-01'
  and h.country in ('England', 'Spain')
group by d.name
having count(distinct h.country) = 2;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top