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 > Data Access, Manipulation & Batch Languages > ANSI SQL > In need for a solution for practical problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-06-04, 21:32
zlatko99 zlatko99 is offline
Registered User
 
Join Date: Dec 2003
Posts: 8
In need for a solution for practical problem

Hi,

I'm in need for a solution for a very practical problem.
(Despite the following example, this is NOT a homework)!!!

Let's say, I have 2 tables: the first one populated with students taking courses, and, the second one populated with students taking classes at one or more campuses...

+========================+
| student | subject |
+==========+=============+
| student1 | physics |
| student1 | chemistry |
| student1 | math |
| student2 | english |
| student2 | math |
| student3 | history |
| student3 | chemistry |
| student3 | programming |
| student3 | physics |
| student3 | math |
| ..... | ..... |
+------------+-----------------+

+========================+
| student | campus |
+==========+=============+
| student1 | north |
| student1 | west |
| student2 | south |
| student2 | north |
| student2 | east |
| student3 | south |
| ..... | ..... |
+-------------+----------------+


The question I want to "ask" the database is:
"Give me all students where each student takes at least Physics and Math, and takes classes at the North campus.". This query should give me only the student1, because student1 takes Physics and Math (among the others) and, also, takes subjects at the North campus.

How should I assemble the query for this task? What if I have many tables (many conditions)?

Thanks in advance!

Zlatko
Reply With Quote
  #2 (permalink)  
Old 08-07-04, 09:00
zlatko99 zlatko99 is offline
Registered User
 
Join Date: Dec 2003
Posts: 8
In need for a solution for practical problem

Well, I forgot to mention that the solution query should not contain subselects (because the query should work on MySql 3.23 which doesn't support subselects).

Zlatko
Reply With Quote
  #3 (permalink)  
Old 08-08-04, 12:45
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool

The solution is so simple that you may want to try and solve it yourself !
You say this is not HOMEWORK, however it is basic SQL!

HINT: Try using a JOIN.
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #4 (permalink)  
Old 08-08-04, 18:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
it's not that easy

using only joins, you would need to join three tables (two from a "self-join")

you can also do it with a join of just the two tables involved, but also a GROUP BY
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-08-04, 21:27
zlatko99 zlatko99 is offline
Registered User
 
Join Date: Dec 2003
Posts: 8
Rudy,

can you tell me, please, how exactly to build the query using join on only 2 tables?

Thank you,
Zlatko
Reply With Quote
  #6 (permalink)  
Old 08-08-04, 23:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select s.student
  from courses s
inner
  join classes c
    on s.student = c.student
 where s.subject in ('physics','math')
   and c.campus = 'north'
group
    by s.student
having count(distinct subject) = 2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-09-04, 21:23
zlatko99 zlatko99 is offline
Registered User
 
Join Date: Dec 2003
Posts: 8
I agree with that query, but what if I want to set more conditions for the campuses as well? For example, how would you build the query having this condition:

.....
where s.subject in ('physics','math')
and c.campus in ('north', 'south', 'east')
.....

I tried something with this new one, but failed...

Zlatko
Reply With Quote
  #8 (permalink)  
Old 08-09-04, 21:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
i would try this --
Code:
 where s.subject in ('physics','math')
   and c.campus in ( 'north','south','west' )
group
    by s.student
having count(distinct subject) = 2    
   and count(distinct campus) = 3
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-10-04, 00:59
orababa@kshema orababa@kshema is offline
Registered User
 
Join Date: Nov 2003
Location: Bangalore,India
Posts: 51
I disagree with all of them ...
IN clause will show result if student takes either of the subjects. I guess 2 subjects are mandatory. Its difficult to find some searches like this when u have data in Vertical rather than horizontal....

Please consider changing the structure of the table...
Cheers
Reply With Quote
  #10 (permalink)  
Old 08-10-04, 06:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by orababa@kshema
IN clause will show result if student takes either of the subjects
yes, and HAVING ensures student takes both

the table structure is fine
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 08-16-04, 12:17
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
incorrect, HAVING would allow Physics and one other course or Math and one other course to qualify for the result-set. You are looking for Math AND Physics.
Reply With Quote
  #12 (permalink)  
Old 08-16-04, 12:28
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Urquel, you are wrong. Rudy's query ensures that:
1) only Maths and Physics are included:
Code:
 where s.subject in ('physics','math')
and
2) 2 different subjects are included:
Code:
having count(distinct subject) = 2
The only way both conditions can be true is if the student takes both Maths and Physics!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #13 (permalink)  
Old 08-16-04, 14:25
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
you are right.
Reply With Quote
  #14 (permalink)  
Old 08-16-04, 16:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
thanks tony

urquel, don't worry about it, i've done the same

sql can be very deceptive, no?

especially in the area of ANDs and ORs and HAVINGs...
__________________
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