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 > SQL Query Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-05-04, 15:19
haw1144 haw1144 is offline
Registered User
 
Join Date: Apr 2004
Posts: 3
SQL Query Help

My work recently gave me a test to see how good my SQL was even though it's not part of my job description I did fairly well but missed one question. I'm pretty disappointed I missed it because it seemed easy enough. Unfortunately they didn't tell me what the correct answer was... so hopefully someone here can tell me (or at least tell me whats wrong with my query):

2 tables:
Car

ID Make Model Price InStock
1 Honda Civic 15000 5
2 Ferrari Testarossa 100000 2
...

Buyers

ID Name City Gender CarID
1 Bob San Jose M 1
2 Linda Los Angeles F 2
...

For every city and gender combination, find the average price of a car bought where the average price of the car is over $25,000.

My Answer:

SELECT b.city, b.gender, avg(c.price)
FROM cars c, buyers b
where c.id = b.carid AND avg(c.price) > 25000
GROUP BY b.city, b.gender;
Reply With Quote
  #2 (permalink)  
Old 04-05-04, 15:33
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
This really smells like homework, but:

In SQL, aggregate function values are computed after the rollup, the WHERE clause is executed before the rollup. You need to move the condition to the HAVING clause.

-PatP
Reply With Quote
  #3 (permalink)  
Old 04-05-04, 15:38
haw1144 haw1144 is offline
Registered User
 
Join Date: Apr 2004
Posts: 3
Post

haha i wish it was homework. i'm a senior sqa engineer

as for your answer, i really have no idea what you're talking about =/
i haven't taken sql since my oracle classes way back in college.

i'm assuming:

SELECT b.city, b.gender, avg(c.price)
FROM cars c, buyers b
where c.id = b.carid
GROUP BY b.city, b.gender
HAVING avg(c.price) > 25000;

?

thanks
Reply With Quote
  #4 (permalink)  
Old 04-05-04, 15:42
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
You at least read fluently anyway! That makes me a lot more comfortable that you aren't just another leech looking for someone to do their homework for them.

Yes, that is exactly what I meant. I can elaborate a lot more if you want, but you've already gotten the answer you originally asked for.

-PatP
Reply With Quote
  #5 (permalink)  
Old 04-05-04, 15:45
haw1144 haw1144 is offline
Registered User
 
Join Date: Apr 2004
Posts: 3
Quote:
Originally posted by Pat Phelan
You at least read fluently anyway! That makes me a lot more comfortable that you aren't just another leech looking for someone to do their homework for them.

Yes, that is exactly what I meant. I can elaborate a lot more if you want, but you've already gotten the answer you originally asked for.

-PatP
no elaborations necessary. it just irked me that i missed something so simple. i totally forgot about the having clause.
i agree on the leeching thing while i don't specifically deal w/sql, i do use it now and then to set up test cases i need for particular results. still have no idea why management gave me this test...
thanks again!
Reply With Quote
  #6 (permalink)  
Old 04-05-04, 15:51
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
It smells like they might be considering you for an upgrade of some sort, possibly requiring some additional training... That or maybe they are getting worried that the QA types are getting dangerous!

Anywho, always glad to spread confusion.

-PatP
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