den: (Default)
[personal profile] den
bloody SQL assignment is killing me.

"List all data from the Person table who are niether staff nor patients."

fucked if I know.

Why does

WHERE person.person_id = staff.person_id
AND person.person_id = admission.patient_id


work and

WHERE person.person_id <> staff.person_id
AND person.person_id <> admission.patient_id


not?

Date: 30 Mar 2002 21:53 (UTC)
From: [identity profile] charles.livejournal.com

To answer the question.

Imagine the world's most fucking insane DBA came up with the following tables:



Now, try the first select: "Select first_name, last_name from table_1, table_2 where table_1.id = table_2.id"


Starting at the first row. We have table_1.id = 1, and first_name = 'Charles'. Looking across to the second table, we find all the rows where table_1.id = table_2.id. There's only one of those rows, and that joins me across to my last name, 'Miller'. So the results table will be:





Now for the second query. "Select first_name, last_name from table_1, table_2 where table_1.id <> table_2.id"


First, we look at the first row. The first_name is 'Charles', and the id is 1. Now we look at the second table, and find our join is telling us "find all the rows where table_1.id is NOT the same as table_2.id." The first row doesn't match this condition, but the other two do. So the result of this query would be:



Date: 30 Mar 2002 21:56 (UTC)
From: [identity profile] dewhitton.livejournal.com
Kilkennys for you!

Profile

den: (Default)
den

April 2023

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526 272829
30      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated 31 December 2025 01:51
Powered by Dreamwidth Studios