mySQL world DB Monarchy - mysql

I have an assignment where I need to use the world DB in mySQL to find the 5 largest cities and their countries in countries where the government form is any type of monarchy. I am trying it with the distinct clause, and the only info that appears correct is the government column. Everything else is for India. Does anyone have an idea?
select distinct city.name, governmentform, city.population, countrycode
from country, city
where governmentform like='%monarchy%'
order by city.population desc
limit 5;

Related

MySQL dynamic order

In general, all I need to do is to order a MySQL table.
But, it has to be a "smart" order and I would like to hear your opinions.
There is a table of customers
id, name, email, phone, country, language, registration_time
There is another table which holds the skills of sales managers as numeric values -
sales_manager_id, skill_type, skill_name, skill_value
7, language , English , 5
Which means that manager number 7 speaks English on level 5.
Every sales manager can have multiple skills.
Now, I want to order the customers table by country, language and registration_time (in this exact order) for a specific sales manager in such a way that the top rows will be from a country in which this sales manager has highest skills, after this by language in which he has the highest skills and after this by registration time.
Do you have any suggestions? The biggest problem is that this query should be simple and readable as much as possible because there would be modifications in the future and I don't want to deal with enormous queries.
I assume I understand your problem. If not please correct me, else here is my solution.
I did a few changes to your tabels in order to test the query and you can test my solution like I did here: SQL Fiddle
(1) You need to JOIN your customers table two times with the skills table - first for the country skill and second for the language skill of a sales manager:
select *
from customers
join skills as country_skills on country_skills.skill_name = customers.country
join skills as language_skills on language_skills.skill_name = customers.language
(2) You need to restrict your results for just the sales manager you want, e.g. sales manager with id = 11:
where country_skills.sales_manager_id = 11
and language_skills.sales_manager_id = 11
(3) The 'dynamic' order you want:
order by country_skills.skill_value desc, language_skills.skill_value desc, regTime desc
This would be my complete query:
select *
from customers
join skills as country_skills on country_skills.skill_name = customers.country
join skills as language_skills on language_skills.skill_name = customers.language
where country_skills.sales_manager_id = 11
and language_skills.sales_manager_id = 11
order by country_skills.skill_value desc, language_skills.skill_value desc, regTime desc
So you got all customers sorted by country skills of a specific sales manager > language skills of a specifiy sales manager > regTime of customer.
This query ignores customers with a country or language the sales manager got no skill... you can avoid that with a LEFT JOIN

How can I use a subquery on my query results and then ORDER BY a calculated result for each row?

Context
I'm having some trouble putting together the logic of subqueries(?) together in my head.
*---------*---------*---------*------------*------------*---------*
|GUEST_ID | Country | County | Attending | Donation | Party |
*---------*---------*---------*------------*------------*---------*
I have a database containing records about attendees of an charity ball.
GUEST_ID: Table key.
Country: Country the guest is from.
County: County the guest is from (i.e. a region within that country).
Attending: Whether or not the guest is attending (i.e. true or false).
Donation: Amount the guest is donating to the cause.
Party: Which Party the guest is attending.
Goal
I wish to display a table broken down by Country and County, showing the number of attendees from each Country + County, and the average donation of those who are attending from that Country + Country. I'd then like to order the rows from highest average donation to lowest. I understand the constituent parts of this query, however I'm not sure how to 'glue' it together as a whole.
I can GROUP BY Country, County.
I can SUM(Donation).
I can COUNT(*) WHERE ATTENDING = 'Yes'
And I know I can SET #variables to store results in the interim.
I also know I can ORDER BY DESC.
So far
My issue is with understanding how to combine these elements into a functioning query. I'm guessing I need to use subqueries however it's getting the order right I'm having trouble with. This is what I have so far -
SELECT SUM(`Donation`) AS `TotalDonations`, `Country`, `County`
FROM `GuestList`
WHERE `Party` = `2014CharityBall`
GROUP BY `Country`, `County`
I'm not sure how to add the subquery to find the COUNT of only those guests who are definitely attending, or how to calculate the TotalDonations / DefinitelyAttending and then ORDER BY this.
Results Required
*------------*------------*---------------*---------------*---------------*
| Country | County | # of Attendees|Total Donations|Avg. Donation |
*------------*------------*---------------*---------------*---------------*
Country: Country the guests are from.
County: County the guests are from (i.e. a region within that country).
# of Attendees: Number of attendees (Attending = 'true') within that country and county.
Total Donations: Total donations of all those attending (Attending = 'true') within that country and county (e.g. SUM(Donation)).
Avg. Donation: Average donation of all those attending (Attending = 'true') within that country and county (e.g. AVG(Donation) - that is, ofc, Total Donations / # of Attendees).
Extra Credit
Just an expression ;)
If I want to calculate the Total Donations among all donators invited to the party, and, separately, the Total Donations solely among those donators attending the party, how would I do that?
SELECT `Country`,
`County`,
SUM(`Donation`) AS `TotalDonations`,
FORMAT(AVG(`Donation`), 0) AS `AVGDonations`,
COUNT(1) Attending
FROM `GuestList`
WHERE `Party` = `2014CharityBall`
AND Attending = `Yes`
GROUP BY `Country`, `County`
Order by 4 desc

Why this query doesn't work with such condition?

I'm trying to solve some tasks from this http://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial
At the last task(number 8) I wrote a query:
select name, continent from world a
where a.population >
(select 3*max(population) from world b
where b.continent = a.continent)
but this query doesn't return any rows. But works almost the same query(just added an additional conditin in the end of subquery). But what's the matter? Why doesn't it return raws even if names of countries are the same?
select name, continent from world a
where a.population >
(select 3*max(population) from world b
where b.continent = a.continent and a.name <> b.name)
Let me translate what both query does to english, so you can realize the difference.
first query; compare and get all countries who are on the same continent and have more than 3 times of the maximum populated country in that continent.
second query; compare and get all countries who are on the same continent and have more than 3 times of the maximum populated country in that continent except himself.
in your first query the maximum populated country cannot be more than 3 times more populated than himself if he is the max populated country himself so your query returns 0 results.
but on the second query the maximum populated country EXCEPT himself can have population 3 times more than other countries in the same continent.

MySQL Query to display records from table

I have a query.I have table with two columns country and state.I want to display columns in following format
Country State
----------- ---------
India Delhi
Bangalore
Kolkata
Mumbai
USA California
Florida
Las Vegas
Virginia
It means "India" just appear one time in country column and and repeated values would come as blank value in country column when i select country and state from table.
Thanks in advance
Presentation is usually if not always better done outside of SQL so I'd recommend doing this in whatever your presentation layer runs, but if it's a requirement for the query, you can do it using session variables;
SELECT Country, State FROM (
SELECT IF(Country=#country, '', Country) Country, State, #country := Country
FROM (SELECT Country, State FROM Table1 ORDER BY Country, State) dummy1,
(SELECT #country:='') dummy2
) dummy3;
An SQLfiddle to test with.
Just to show a (probably) better way, you can use this to get a list of states per country, and process it further in your presentation layer;
SELECT Country, GROUP_CONCAT(State) FROM Table1 GROUP BY Country;
Another SQLfiddle.
use pl/sql.Moreover your table would be voilating 5th normal form.

MySQL Multiple query with a bit of logic

I have a Users table with these values (stripped down for the example):
Name
City
County
StateProvince
Country
Continent
Strength
Endurance
Intelligence
I basically want a query that returns the highest stat by region for yourself. If you have the highest Strength (67) in the country, highest Endurance (59) in your City, and not the highest Intelligence anywhere, I'd like a 2 row result of:
('Strength', 67, 'Country', 'United States')
('Endurance', 59, 'City', 'Redmond')
Note that if you have the highest Strength in the country, you will also have the highest strength in the state/province, county, and city (but I'd like those not to be returned in the result). I can do this all using multiple SELECT queries, however, I'd like to know how to do this in one single query for efficiency (or if that's even a good idea?). I'd imagine the logic would go something like (pseudo code):
(SELECT FROM Users ORDERBY Strength WHERE Country = 'MyCountry' LIMIT 1) IF Name != 'Me' (SELECT FROM Users ORDERBY Strength WHERE StateProvince = 'MyState' LIMIT 1) ... and so on
Furthermore, the above would also need to work with Endurance and Intelligence. Thanks!
Check this link MySQL control-flow-functions
SELECT CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END;