I'm trying to do my OOP homework, but I am having problems with the identification of objects. The problem says:
A country has provinces, a capital city, limit with other countries
and is located on a continent. The provinces of a country bordering
other provinces of the same country and may limit with other
countries. The provinces have cities and one of them is its capital.
I think the objects are:
Continent
Country
Province
City
But all these objects are territories, and a city can be capital or not. So, in this case:
Territory
Continent (extends Territory)
Country (extends Territory)
Province (extends Territory)
City (extends Territory)
CapitalCity (extends City)
CommonCity (extends City)
So,
A Contienent has a collection of Country.
A Country has a collection of Province and a CapitalCity.
A Province has a collection of City and a CapitalCity.
This is well done or should I ignore the Territory, CapitalCity and CommonCity?
Related
I am trying to learn sql and i have downloaded a world database.
My problem is that i cannot find out how to pick the continents with more than 10 countries in them
My database is:
name: (alle countries in the world)
continent: (Africa, Americas, Asia-Pacific, Europe, Middle East, North America, South America, South Asia)
If someone can push me in the right direction, i would be really glad!
I know a part of what i need to do, but i am not sure where to put more code to get the result.
SELECT continent, COUNT(*)
FROM world
GROUP BY continent
I got the help i needed, thank you!
The code i'm using is:
SELECT continent, COUNT(*)
FROM world
GROUP BY continent
HAVING COUNT(name) > 10
What you are missing is the HAVING statement that allows you to filter the results of an aggregation. (As opposed to the WHERE clause, that is executed before the rest of the query).
SELECT continent, COUNT(*)
FROM world
GROUP BY continent
HAVING COUNT(*)>10
select addressline1, city
from person.address
where city LIKE '[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z] '
I want to get cities with 5 characters by operator LIKE. Is it possible?
And my result is empty.
A simple solution would be
SELECT addressline1, city FROM person.address WHERE LEN(city) = 5;
However, if you are hell bent on using LIKE, use this.
SELECT addressline1, city FROM person.address WHERE city LIKE '_____';
The LIKE solution above, however is practical only for small lengths, as your case.
I am stuck on another SQL exercise, here is the question:
Show the 20 biggest cities in the United States along with their rank in the state (with respect to their population) and percent of the city population in a state (call it: perc_pop_state).
Here is what i have so far. This produces the table i am looking for, but for some weird reason the percentages of city population to state population are 0's for all the states with multiple cities and 1 for all states with one city. Can anyone guide me as to what is wrong with my code.
select
city.name, city.population, city.district, rank() over (partition by district order by city.population desc), city.population / sum(city.population) over (partition by district) as perc_pop_state
from
city
inner join country on code = countrycode
where
country.name = 'United States'
order by
city.population desc
I don't know which database this is for, but most likely it's because you the figures are not decimal data type - they are whole numbers, so when you divide them the result is a whole number (0 or 1) rather than a fraction. So you should use something like this
CAST(city.population AS DECIMAL(19,4))
/
CAST(sum(city.population) AS DECIMAL(19,4))
I need help. I have these tables.
Patient (PatienID, names, gender, address, contact, occupation, doctorID, userID, registrarID)
Diagnosis (DiagnosisID, Provissional_Diagnosis, Alternative_Diagnosis, Definitive_Diagnosis, PatientID, DoctorID)
Treatment (TreatmenI, Medicine, Dosage, Type, Councelling, DiagnosisID)
So I want to write the following queries
What is the percentage of patients that have previously suffered from 'Malaria' were treated for Malaria in the future?
List the top 10 regions with the highest number of Malaria cases.
Similar to this question: NOT IN vs IN Do Not Return Complimentary Results
Basically I am trying to answer this question: Find each country that belongs to a continent where all populations are less than 25000000. Show name, continent and population.
Number 7 here has all the table details: http://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial
This query works: it effectively takes all countries belonging to a continent which has no country with a pop greater than 25mm
SELECT name, continent, population
FROM world x
WHERE continent NOT IN(SELECT DISTINCT continent FROM world
WHERE population >= 25000000)
This query does not work. I am trying to use having without having an aggregate function in the select statement. Is this allowed? Currently my subquery returns no results, so I am obviously mistaken somewhere.
SELECT name, continent, population
FROM world x
WHERE continent in (SELECT continent FROM world
having max(population) < 25000000)
Figured it out, thank you #Michael Berkowsiki for the hint in the comment above.
SELECT name, continent, population
FROM world x
WHERE continent in (SELECT continent FROM world
group by continent
having max(population) < 25000000)
"Each column referenced in the SELECT statement must be referenced in the GROUP BY clause, unless the column is an argument for an aggregate function included in the SELECT clause." ("Modern Database Management" 10th Edition, Jefferey A. Hoffer, Page 276)
So in this case, I believe your answer needs to be modified as:
SELECT name, continent, population
FROM world x
WHERE continent in (SELECT continent FROM world
group by name, continent, population
having max(population) < 25000000)