Solved

Select Statement from mutliple tables

Posted on 2013-11-19
13
327 Views
Last Modified: 2013-11-19
Hi,
I am trying select all the class names from tbl_class_Names that are not listed in
tbl_students.

I have tried this select statement however it does not work..

select tbl_Class_Names.Class_Name 
from tbl_Class_Names
join tbl_Class on tbl_Class.Class_ID = tbl_Students.Class_ID
join tbl_Class_Names on tbl_Class_Names.CN_ID = tbl_Class.CN_ID
where tbl_Students.Class_ID <> tbl_Class on tbl_Class.Class_ID

Open in new window


I have inherited the database so the structure is not ideal.

tbl_Students
Student_Number            Class_ID                  Lecture_Room
511                              18                             5
512                              15                             9


tbl_Class

Class_ID            CN_ID
18            55            
15            76


tbl_Class_Names
CN_ID            Class_Name
55               ScienceY2      
76               MathY3
0
Comment
Question by:jay_eire
  • 6
  • 5
  • 2
13 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 39660208
This should work:

select tbl_Class.Class_ID, tbl_Class.CN_ID, tbl_Class_Names.Class_Name
from tbl_Class
JOIN tbl_Class_Names on tbl_Class.CN_ID = tbl_Class_Names.CN_ID
where tblStudents.Class_ID not in (select tbl_Class.Class_ID from tbl_Class)

Open in new window

0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39660224
select cn.class_name
from tbl_class_names cn
   INNER JOIN class c ON cn.cn_id = c.class_id
WHERE c.class_id not in (select class_id from tbl_students)

Open in new window

0
 
LVL 7

Author Comment

by:jay_eire
ID: 39660308
thanks for reply

@sjwales
I am getting this error message back
Error Code: 1054. Unknown column 'tbl_Students.Class_ID' in 'IN/ALL/ANY subquery'      0.000 sec
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39660328
corrected join condition :

select cn.class_name
from tbl_class_names cn
   INNER JOIN class c ON cn.cn_id = c.cn_id
WHERE c.class_id not in (select class_id from tbl_students)

Open in new window

0
 
LVL 7

Author Comment

by:jay_eire
ID: 39660366
@ jacko72 thanks what is c and cn? are these temporary values?
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39660372
They are aliases, you use them as a shortcut for the table names
0
 
LVL 7

Author Comment

by:jay_eire
ID: 39660399
thanks jack072 I have an error

Error Code: 1052. Column 'cn_id' in field list is ambiguous      0.000 sec
0
 
LVL 7

Author Comment

by:jay_eire
ID: 39660405
my bad, I have corrected it had  WHERE c.cn_id instead of WHERE c.class_id
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39660409
ensure you have prefixed all the column names with an alias or table name.
0
 
LVL 7

Author Comment

by:jay_eire
ID: 39660470
if I need to select additional columns from tbl_Class_Names how do I do this with the alias?
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 39660479
My query was incorrect, reversed the conditions, Jacko's looks more like the trick.

Tbl_Class_Names is aliased above as "cn".

You can add extra columns by cn.col1, cn.col2.  Just because a table is aliased, by the way, doesn't mean you still can't reference it by its full name.

So you could do cn.col1 or you could do tbl_Class_names.col1
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39660483
The alias for tbl_class_names is cn, you can see the alias after the tablename in the FROM clause : from tbl_class_names cn
You just need to prefix any colum name with the alias then a full stop and then the column name like : cn.class_id

so:

select cn.class_id, cn.class_name
from tbl_class_names cn
   INNER JOIN class c ON cn.cn_id = c.cn_id
WHERE c.class_id not in (select class_id from tbl_students) 

Open in new window

0
 
LVL 7

Author Comment

by:jay_eire
ID: 39660589
thank you kindly for the help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question