Page 1 of 1

[Solved] Using WHERE changing column results in queries

Posted: Sat Jan 07, 2023 5:25 am
by DatabaseNewbie
I'm working on a project where I have two tables. One is the students' information (names, phone number, address, etc.). The other table is the GPA scores and class (Freshman, Sophomore, Junior, etc.).

I'm supposed to generate a table where I pull the students' information based on them being a Senior. I'm somewhat familiar with SQL commands, but I'm still having trouble.

The problem with my query is that the resulting table will pull the students' information (for all 7 students) and change all their Class input to 'Senior' instead of just pulling the students' information for those who are actually Seniors (only 2 students) based on the GPA table.

Below are my SQL commands. Can anyone help me figure out what is wrong with it?

Please note that my primary key is StudentNumber because it appears in both tables. Not sure if I need to use that to get the desired result.

SELECT DISTINCT "StudentTable"."FirstName", "StudentTable"."LastName", "StudentTable"."Address", "StudentTable"."City", "StudentTable"."State", "StudentTable"."Zipcode", "StudentTable"."Phone", "GPATable"."Class"
FROM "StudentTable", "GPATable"
WHERE "GPATable"."Class" IN ( 'Senior' )

Re: Using WHERE changing column results in queries

Posted: Sat Jan 07, 2023 6:33 am
by FJCC
Try
SELECT DISTINCT "StudentTable"."FirstName", "StudentTable"."LastName", "StudentTable"."Address", "StudentTable"."City", "StudentTable"."State", "StudentTable"."Zipcode", "StudentTable"."Phone", "GPATable"."Class"
FROM "StudentTable" INNER JOIN "GPATable" ON "StudentTable"."StudentNumber" = "GPATable"."StudentNumber"
WHERE "GPATable"."Class" IN ( 'Senior' )

Re: Using WHERE changing column results in queries

Posted: Sat Jan 07, 2023 6:50 pm
by DatabaseNewbie
That worked just great! Thank you!