MS SQL JOIN Syntax

Kristen Jones
Kristen Jones used Ask the Experts™
on
I need help with an SQL Join/results

I have two tables SCHOOLS and STUDENTS  They both have a SCHOOLID that can be joined.  What I need to see are ALL of the SCHOOLS and fields from the SCHOOLS table and  a virtual field telling me if there are ANY STUDENTS that have that SCHOOLID.

Example:

SCHOOLS
   SchoolID
   School
   Active

STUDENTS
   name
   SchoolID

 RESULT Table:
SchoolID     Active     Used (yes or No)  

So the virtual field USED is telling me if there are ANY schools that are in the STUDENTS table.  

Then I want to make another query that I can add a where clause to the SchoolID  So SAME result be asking "Does SchoolID have any students from the STUDENTS table that have that SchoolID"  Then it would list that school (ONE RECORD)  SchoolID, School, Active, Used
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
For the first query try this.

SELECT Schools.SchoolID, Schools.School, Schools.Active, IIF(Students.SchoolID IS NULL, 'No', 'Yes') AS Used
FROM Schools
LEFT JOIN Students ON Schools.SchoolID = Students.SchoolID;
Kristen JonesGIS/APP Dev

Author

Commented:
It is a 2008 SQL server so IIF is not a Function  I just tried a direct replacement using CASE and it has a lot of other Syntax errors
Kristen JonesGIS/APP Dev

Author

Commented:
This worked (I had a typo on the field name)  But it appears to query all of the students rather than just the SCHOOLS


CASE WHEN Students.SchoolID IS NULL THEN 'Yes' ELSE 'No' end  
FROM Schools
LEFT JOIN Students ON Schools.SchoolID = Students.SchoolID;
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Kristen JonesGIS/APP Dev

Author

Commented:
Looking at this again the results are not right.  I tried an outer join and it still tries to do a one to many.  I just want each school (record) to look through all of the STUDENTS (SchoolD) for it..  if ANY of the STUDENT table has a match .... And there could be 1000's of matches, I just need to know if it was ever used then the single school record say 'Yes' OR 'No' then move to the next record in the SCHOOL table.
Analyst Assistant
Commented:
LEFT JOIN should work for that.

What exactly is being returned?

Do the no of records returned exceed the no of records in the Schools table?

Have you tried using SELECT DISTINCT?
Kristen JonesGIS/APP Dev

Author

Commented:
It is grabbing and returning all of the Students 1000's  there are only about 40 schools so I am expecting only the Schools  and either Yes or no on the USED
Kristen JonesGIS/APP Dev

Author

Commented:
That worked .. thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial