I have got a column name country in which there are 3 entries which are shown below. In each countries i have set of people working in it in a different column. I want a count query which can count how people are working in each country in a single query.
country
________
India
America
China
try this:
SELECT country,COUNT(*)
FROM table
GROUP BY country;
Related
This is my very first question on Quora. Thanks for any recommendations , solutions and remarks. I was not provided with many specifics and data, this is a assignments where one must use his or her imagination to fill in particular data and variables. What counts is the correct logic, approach and covering possibilities. Your help is highly appreciated! Thank you!
Country and Continent table
Required Visual Result for Question 3
Questions:
Write a query that would select all countries with GDP of more than 1 000 000 000 USD
Write a query that would return all countries in Europe (specifically) with GDP of more than 1 000 000 000 USD
Write a query that lists all continents with GDP per continent (as the sum of the GDP of all countries). Each country belong to one continent only.
For what result should look like - please resort to "Required Visual Result for Question 3" image.
My Solutions:
select * from countries where GDP > 1000000000
select * from countries where continent_id = 2 and GDP>1000000000;
select sum(GDP) from countries where continent_id = 4;
However, here in 3) I can only have the GDP sum displayed, and do not know how to have the continent's name on the left side as well. Please, if possible, assist with having the continent's name displayed and then right next to it and on the right handside - the relevant GDP sum.
Welcome!
Your image shows the tables as country and continent but your queries refer to countries? So which is it please?
On the basis your image is correct but the queries are wrong then number 3 would be as below:
With no 3 your currently only going to get the some for the continent with the ID of 4.
select sum(GDP) from country where continent_id = 4;
So what you want to do is remove the WHERE and the GROUP BY continent_id to give you 1 result per continent.
select sum(GDP) from country where 1 GROUP BY continent_id
No to get the continent name included in your results you can use the JOIN syntax.
In this instance you want all your records from country and just the records from continent that match your join condition which will be the continent_id from country and the id from the continent table.
SELECT
`continent`.`name`, sum(`GDP`)
FROM `country`
LEFT JOIN `continent`
ON `country`.`continent_id` = `continent`.`id`
GROUP BY `continent_id`
ORDER BY `continent`.`name` ASC;
That should give you the results 1 per continent as required.
I've specified the table names as its clearer how to target specific columns from each table.
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.
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.
I have a table called visitors which contains IP and country columns.
Now I want to query the table such that I get unique countries from the table and display the count of rows of that country.
Table:
IP Country
1.1.1.1. xyz
1.2.3.4 xyz
2.2.3.6 abc
3.61.3.69 axy
Now I want the result as :
Country No_Visitors
xyz 2
abc 1
axy 1
I know how to do it by using 2 queries, get the unique country first and then again query the table for the country name. But how can I do it with single query.
use AGGREGATE FUNCTION COUNT() to get the total number of instances for each country.
SELECT Country, COUNT(IP)
FROM tableName
GROUP BY Country
SQLFiddle Demo
an easy one, its long time i do not use mysql queries so maybe someone can help me out with this nooby question, i have a contacts table, id, name, and city.. i want to get all the contacts listed as follows.
city 1
--------
contact1
contact2
contact3
city 2
--------
contact4
contact7
contact10
city 3
--------
contact5
contact6
contact8
I do not want to use any additional php coding, just get the sql result like this
city 1
-> contact 1
-> contact 2
-> contact 3
city 2
-> contact 4
-> contact 7
-> contact 10
...
then fill the result in a php object and do like this:
foreach (cities as city)
{
// code here
}
Thanks in advance
Use GROUP_CONCAT
SELECT city, GROUP_CONCAT(name)
FROM contacts
GROUP BY city
You just need to GROUP BY city column and use GROUP_CONCAT function in SELECT to get comma seprated list of names for each city.
SELECT city, GROUP_CONCAT(name) AS name
FROM contacts
GROUP BY city;