From relational algebra to SQL

Treatment ( StaffNo , PatientID , StartDate, Reason)

Find the number of employees of all doctors who treat all patients who are treated by a doctor whose staff is 603.

In relational algebra

Separate (project processing by stateNo PatientId) From Project (Select a treatment where StaffNumber = '603) Over PatientId

I want this in SQL, please.

Is this SQL correct?

SELECT DISTINCT staff_no
FROM treatment AS t1
WHERE NOT EXISTS (SELECT *
                  FROM treatment as s2
                  WHERE s2.staff_no = '603'
                    AND NOT EXISTS (SELECT *
                                    FROM treatment AS t3
                                    WHERE t1.staff_no = t3.staff_no
                                      AND t3.patient_id = s2.patient_id));
+3
source share
2 answers

Thank you all for your help.

I have found the answer.

This SQL is unofficially equivalent to relational division.

SELECT DISTINCT staff_no
FROM treatment AS t1
WHERE NOT EXISTS (SELECT *
                  FROM treatment as s2
                  WHERE s2.staff_no = '603'
                   AND NOT EXISTS (SELECT *
                                    FROM treatment AS t3
                                    WHERE t1.staff_no = t3.staff_no
                                      AND t3.patient_id = s2.patient_id));
+1
source

SQL, SQL . ; , . ( SQL) , , .

SQL. , .

, , ( , , ); , .

0

All Articles