d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help With Basic Sql
Add Reply New Topic New Poll
Member
Posts: 11,881
Joined: Aug 17 2007
Gold: 0.00
Nov 20 2014 03:50pm
Practicing SQL on SQL Zoo: http://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial

I am having trouble with the following exercise:

Quote
Find the largest country (by area) in each continent, show the continent, the name and the area


Using the sample code provided, the answer is as follows:

Code
SELECT continent, name, area FROM world x
WHERE area >= ALL
(SELECT area FROM world y
WHERE y.continent=x.continent
AND area>0)


However, I wanted to solve the question along the lines of something like this:

Code
Select w.continent, w.name, w.area
From world w
Inner join
(
Select w2.continent, max(area) as area
From world as w2
Group by w2.continent
) as wj

on w.area = wj.area



The latter makes more intuitive sense to me, but the former is much easier to write (both are accepted as correct answers).
Would someone be willing to walk me through why it isn't necessary to write it out the way I have?

This post was edited by MidnightRider on Nov 20 2014 03:52pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 20 2014 03:54pm
your link doesnt work

Quote
SELECT within SELECT Tutoria

There is currently no text in this page. You can search for this page title in other pages, or search the related logs, but you do not have permission to create this page.


i dont think the book's answer is standard sql. i've never seen the syntax before, and i'd suggest staying away from it. what DBMS are you using?

/edit: after googling, looks like a mysql thing
http://dev.mysql.com/doc/refman/5.5/en/all-subqueries.html

This post was edited by carteblanche on Nov 20 2014 03:55pm
Member
Posts: 11,881
Joined: Aug 17 2007
Gold: 0.00
Nov 20 2014 03:58pm
Quote (carteblanche @ Nov 20 2014 05:54pm)
your link doesnt work



i dont think the book's answer is standard sql. i've never seen the syntax before, and i'd suggest staying away from it. what DBMS are you using?

/edit: after googling, looks like a mysql thing
http://dev.mysql.com/doc/refman/5.5/en/all-subqueries.html


Sorry, I corrected the link.
I had omitted the last "l" on tutorial.

Yes, the DBMS is MySQL.

How would you go about writing this query??

This post was edited by MidnightRider on Nov 20 2014 03:58pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 20 2014 04:08pm
Quote (MidnightRider @ Nov 20 2014 04:58pm)
Sorry, I corrected the link. 
I had omitted the last "l" on tutorial. 

Yes, the DBMS is MySQL.

How would you go about writing this query??


oddly, i found documentation in oracle for ALL as well. i guess i learned something new.

i would go for the join as well. just be careful that if two countries in the same continent have the same area, they'll both be shown. if requirements are fine, leave it be.

are you still unclear about how the answer's query works? the link i posted was pretty clear and included examples. for each continent, it's grabbing all countries which have areas >= countries in the same continent. the only countries that satisfies that are the ones with the largest area. i can try to go into more detail if you need.

This post was edited by carteblanche on Nov 20 2014 04:17pm
Member
Posts: 11,881
Joined: Aug 17 2007
Gold: 0.00
Nov 20 2014 05:08pm
Quote (carteblanche @ Nov 20 2014 06:08pm)
oddly, i found documentation in oracle for ALL as well. i guess i learned something new.

i would go for the join as well. just be careful that if two countries in the same continent have the same area, they'll both be shown. if requirements are fine, leave it be.

are you still unclear about how the answer's query works? the link i posted was pretty clear and included examples. for each continent, it's grabbing all countries which have areas >= countries in the same continent. the only countries that satisfies that are the ones with the largest area. i can try to go into more detail if you need.


Haha, glad to hear you learned something ^_^

I understand how the provided answer works, I just wouldn't go about solving the problem as such.

But I am under the impression the way I am approaching these sorts of queries is disadvantageous, as I am struggling with all of the sample questions.

For example, I cannot solve question 8 on the same website.

Or well, actually, I did get it, but it took way too long.


Code
Select w.name, w.continent
From
world w inner join
(
Select a.name, 3*max(b.population) as 3xMax
From
world a
Inner Join world b
On a.continent = b.continent
Where a.name <> b.name
Group by a.name
) as wj
on w.name = wj.name
Where w.population > 3xMax


especially when this works:

Code
select
a.name, a.continent
from world as a
where
a.population >
(
select 3*max(b.population)
from world as b
where b.continent = a.continent and b.name <> a.name
)


Maybe I just need more practice.

This post was edited by MidnightRider on Nov 20 2014 05:19pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 20 2014 05:28pm
Quote (MidnightRider @ Nov 20 2014 06:08pm)
Haha, glad to hear you learned something  ^_^

I understand how the provided  answer works, I just wouldn't go about solving the problem as such.

But I am under the impression the way I am approaching these sorts of queries is disadvantageous, as I am struggling with all of the sample questions.

For example, I cannot solve question 8 on the same website.


i wouldn't worry about it.

unless you wanna be a dedicated sql person, a lot of queries you write for companies are fairly straightforward. plus, you don't have to get it done in a single query. you're allowed to write stored procedures (cursors, loops, etc) to get the job done. you typically just write the sql to get the data you want. if people complain about performance, then you go back and tune it. it's usually only the big companies that get heavy load who are worried about performance of everything.

i remember i wrote a query that involved selecting / updating / deleting using multiple unions, which caused performance problems. the DBA had me change it to use cursors and loops instead.

This post was edited by carteblanche on Nov 20 2014 05:41pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll