If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Some query questions

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-06-11, 09:37
Vancatu Vancatu is offline
Registered User
 
Join Date: Oct 2011
Posts: 8
Some query questions

Hello fellows,

I'm new into SQL query's and I'm currently performing some query exercises and I can need some help with it.

I'm using an ERD in MYSQL Workbench.

Now I want to know via a guery which departments don't have employees.

I also want to know which courses are plannend for which no one is registered. I want to display the code of that particular course.

Furthermore I want to know which emplyees don't act as teachers. I want to display the name of those employees.

This is the ERD:

The relations are head INT (head of department) with enr INT (employee number), head INT with dep INT (department), enr INT with student INT, chief INT with dep INT, enr INT with teacher INT, code with course, and course and startdate with course and start date.

http://i1097.photobucket.com/albums/...ncatu/eer2.jpg
Reply With Quote
  #2 (permalink)  
Old 10-06-11, 09:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
sorry, we will not do your homework assignment for you

please try the queries yourself, and post only specific questions that you might have about a query (and don't forget to post your query attempt too)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-06-11, 09:49
Vancatu Vancatu is offline
Registered User
 
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by r937 View Post
sorry, we will not do your homework assignment for you

please try the queries yourself, and post only specific questions that you might have about a query (and don't forget to post your query attempt too)
Most of the questions I did myself. These are the ones I'm stuck with.

For the first one I tried this query:

SELECT d.name
FROM department as d JOIN employee as e
ON d.depnr=e.enr

I think I need to specify a WHERE but I'm not sure how.
Reply With Quote
  #4 (permalink)  
Old 10-06-11, 10:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Vancatu View Post
SELECT d.name
FROM department as d JOIN employee as e
ON d.depnr=e.enr
this query will return departments which ~do~ have employees

for departments which don't have employees, you'll need a LEFT OUTER JOIN with an IS NULL check, or else use a NOT EXISTS subquery
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-06-11, 10:22
Vancatu Vancatu is offline
Registered User
 
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by r937 View Post
this query will return departments which ~do~ have employees

for departments which don't have employees, you'll need a LEFT OUTER JOIN with an IS NULL check, or else use a NOT EXISTS subquery
Something like this?

SELECT d.name
FROM department as a left outer join employee as e
ON d.depnr=e.enr
WHERE d.depnr is null
Reply With Quote
  #6 (permalink)  
Old 10-06-11, 10:27
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
I dunno
why don't you try it yourself and see if it works
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #7 (permalink)  
Old 10-06-11, 10:47
Vancatu Vancatu is offline
Registered User
 
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by healdem View Post
I dunno
why don't you try it yourself and see if it works
It doesn't work, it says 0 rows are selected. But that figures I guess. Sorry, I'm a newbie when it comes to SQL

Last edited by Vancatu; 10-06-11 at 11:36.
Reply With Quote
  #8 (permalink)  
Old 10-06-11, 11:36
Vancatu Vancatu is offline
Registered User
 
Join Date: Oct 2011
Posts: 8
I've got it.

SELECT dep.name
FROM department d left outer join employee e
ON depnr=dep
WHERE dep is null

Thanks for the help guys.
Reply With Quote
  #9 (permalink)  
Old 10-06-11, 12:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Vancatu View Post
SELECT dep.name
FROM department d left outer join employee e
ON depnr=dep
WHERE dep is null
ignoring the fact that there is no table called "dep" in this query (you probably meant d.name)...

you qualified only one column with its table name -- dep.name (or d.name) -- and this is likely because "name" is a column name that is common to both tables (not qualifying it gives an "ambiguous column" error)

the other columns mentioned in the query -- depnr and dep -- are unqualified

here's a tip that will save you cumulative weeks and months of time --

in any query involving more than one table, qualify ~all~ column references in the query with their table names (or table aliases)

you will thank me later

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 10-06-11, 12:20
Vancatu Vancatu is offline
Registered User
 
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by r937 View Post
ignoring the fact that there is no table called "dep" in this query (you probably meant d.name)...

you qualified only one column with its table name -- dep.name (or d.name) -- and this is likely because "name" is a column name that is common to both tables (not qualifying it gives an "ambiguous column" error)

the other columns mentioned in the query -- depnr and dep -- are unqualified

here's a tip that will save you cumulative weeks and months of time --

in any query involving more than one table, qualify ~all~ column references in the query with their table names (or table aliases)

you will thank me later


Yes I meanth d.name. Actually, I'm from the Netherlands and use the original ERD in dutch words. I translated it in english and made a screenshot so you guys would understand.

With qualifying all column names I guess you mean this?:

ON d.depnr=e.dep
WHERE e.dep is null
Reply With Quote
  #11 (permalink)  
Old 10-06-11, 12:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Vancatu View Post
With qualifying all column names I guess you mean this?:

ON d.depnr=e.dep
WHERE e.dep is null
yes, just like that

when you have to debug a query, when it stops working, even if you wrote it knowing which columns are in which tables, if you qualify the columns when you write the query then you don't have to try to figure out the table design first before understanding what ON depnr=dep WHERE dep is null is supposed to be doing...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 10-06-11, 12:40
Vancatu Vancatu is offline
Registered User
 
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by r937 View Post
yes, just like that

when you have to debug a query, when it stops working, even if you wrote it knowing which columns are in which tables, if you qualify the columns when you write the query then you don't have to try to figure out the table design first before understanding what ON depnr=dep WHERE dep is null is supposed to be doing...
Okay, Ill keep that in mind. I've adjusted the res of the query's on you're advice.

I've also done the third question as that was almost the same as the first.

But I'm still stuck with the second one:

Which courses are plannend for which no one is registered. I want to display the code of that particular course.

Any tips?

For instance, do I need to use registration, employee and course?
Reply With Quote
  #13 (permalink)  
Old 10-06-11, 12:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Vancatu View Post
For instance, do I need to use registration, employee and course?
no, just two of those
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 10-06-11, 13:06
Vancatu Vancatu is offline
Registered User
 
Join Date: Oct 2011
Posts: 8
Quote:
Originally Posted by r937 View Post
no, just two of those
SELECT r.course
FROM registration r left outer join employee e
ON r.course=r.start date
WHERE r.student is null

It doesn't work but am I in the right direction?
Reply With Quote
  #15 (permalink)  
Old 10-06-11, 16:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Vancatu View Post
It doesn't work but am I in the right direction?
no, you aren't

wrong tables
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On