I have a MySQL database with two tables in it:
Members
- id, name, city_id
and:
Cities
- id, name
I want a query that will return each city name and how many members are in it.
The output I need is:
city_name count
------------------
New York 15
Los Angeles 20
something like this:
SELECT a.name, COUNT(DISTINCT b.city_id)
FROM Cities a JOIN Members b
ON b.city_id = a.id
GROUP BY b.city_id
SELECT
c.name AS city_name,
COUNT(m.id) AS counter
FROM Members m
JOIN Cities c ON m.city_id = c.id
select
cities.name as city_name,
count(cities.id) as counter
from
members inner join cities
on
members.city_id = cities.city.id
Try this
SELECT c.name city_name, COUNT(m.id) counter
FROM Cities c INNER JOIN Members m
ON c.id = m.city_id
GROUP BY c.name
Try something like this:
select city.name, count(member.id)
from city
inner join members
on city.id = members.city_id
group by city.name
Hope this helps.
Related
I want to print the name of all companies that have done at least 3 sponsorizations on different cities
ps: kid is primary key of company
This is what I tied so far but this return all companies that has at least 3 sponsorizations but not filter about different cities.
company(kid, name)
sponsorization(kid, city, sum_of_sponsorization)
SELECT c.name, count(c.name)
FROM COMPANY c
INNER JOIN SPONSORIZATION s
ON c.kid = s.kid
GROUP BY
c.name
having count(c.name) > 3
You can try below- with count(distinct s.city)
SELECT c.name
FROM COMPANY c
INNER JOIN SPONSORIZATION s ON c.kid = s.kid
GROUP BY
c.name
having count(distinct s.city) > 3
count distinct city
SELECT c.name
FROM COMPANY c
INNER JOIN SPONSORIZATION s
ON c.kid = s.kid
GROUP BY
c.name
having count(distinct s.city) > 3
I have a query which is as follows :
SELECT CO.CONTINENT, FLOOR(AVG(CI.POPULATION))
FROM CITY CI INNER JOIN COUNTRY CO
ON CI.COUNTRYCODE = CO.CODE
GROUP BY CO.CONTINENT;
Now, I would want to get the same result but without using Group by as I am looking for an alternative for learning purpose. I am sure this can be achieved using subquery but I am unable to.
I have tried following ways:
SELECT CO.CONTINENT,
( SELECT FLOOR(AVG(CI2.POPULATION)) FROM CITY CI2 WHERE CI2.COUNTRYCODE = CO.CODE )
FROM CITY CI INNER JOIN COUNTRY CO
ON CI.COUNTRYCODE = CO.CODE;
or
SELECT CO.CONTINENT, FLOOR(AVG(CI.POPULATION)) OVER(PARTITION BY CI.COUNTRYCODE)
FROM CITY CI INNER JOIN COUNTRY CO
ON CI.COUNTRYCODE = CO.CODE;
Try this:
SELECT DISTINCT
t.CONTINENT,
(SELECT FLOOR(AVG(c2.POPULATION)) FROM COUNTRY c1 INNER JOIN CITY c2
ON c1.CODE = c2.COUNTRYCODE WHERE c1.CONTINENT = t.CONTINENT) AS pop_avg
FROM COUNTRY t
By the way, DISTINCT sometimes is implemented under the hood using GROUP BY, but I don't know how to report each continent only once without doing this.
I don't really recommend this approach, but you can just do:
SELECT DISTINCT CO.CONTINENT, FLOOR(AVG(CI.POPULATION))
FROM CITY CI INNER JOIN
COUNTRY CO
ON CI.COUNTRYCODE = CO.CODE;
I have the following tables:
flights: id, idcompany, idplane, fromCity, toCity, takeoff...
companies: id, name
planes: id, name
cities: id, name
I want to declare the names of the two cities "fromCity, toCity" in the same query.
MY purpose of this action is get the cities names at once, so I can display them to the client, without any need to do another query to get the cities names.
here is my try:
SELECT f.id, f.takeoff, f.arrival, ct.name as fromCity, f.toCity, c.name as company, p.name as plane
FROM flights f, companies c, planes p, cities ct
WHERE f.idCompany = c.id AND f.idPlane = p.id AND f.fromCity = ct.id
ORDER BY f.takeoff ASC
this query will return the name of the "fromCity" but the id of the "toCity", so what I can do to get the two names by the same query?
You could use this:
SELECT
f.id, f.takeoff, f.arrival, ct1.name as fromCity
,ct2.name as toCity, c.name as company, p.name as plane
FROM
flights f
inner join
companies c
on
f.idCompany = c.id
inner join
planes p
on
f.idPlane = p.id
inner join
cities ct1
on
f.fromCity = ct1.id
inner join
cities ct2
on
f.toCity = ct2.id
ORDER BY
f.takeoff ASC;
And try not using old-style-join from now on.
You have to join the table twice.
SELECT f.id, f.takeoff, f.arrival, ct.name as fromCity, f.toCity, c.name as company, p.name as plane , cf.name
FROM flights f, companies c, planes p, cities ct,cities cf
WHERE f.idCompany = c.id AND f.idPlane = p.id AND f.fromCity = ct.id AND f.toCity = cf.id
ORDER BY f.takeoff ASC
my solution is to make two copies of the table cities:
SELECT f.id, f.takeoff, f.arrival, ct1.name as fromCity, ct2.name as toCity, c.name as company, p.name as plane
FROM flights f, companies c, planes p, cities ct1, cities ct2
WHERE f.idCompany = c.id AND f.idPlane = p.id AND f.fromCity = ct1.id AND ct2.id = f.toCity
ORDER BY f.takeoff ASC
Hi I want to show the county in the following query but if county does not exist, return the city instead.
SELECT P.id AS id, SUM(P.price) AS price,
P.tax,
county
FROM Purchases AS P
JOIN Status AS S ON S.id = T.status_id
JOIN Shipping AS Ship ON S.shipping_id= Ship.id
JOIN Shipping_Addresses AS SA ON Ship.shipping_address_id = SA.id
JOIN Zip_Codes AS ZIP ON ZIP.zip_code = SA.zip_code
JOIN Counties AS C ON ZIP.city = C.city
JOIN States AS STATE ON STATE.id = C.county_state_id
GROUP BY ZIP.zip_code
ORDER BY county
Thanks
Try using COALESCE():
COALESCE(county, city) AS location
If county is NULL it will return the value of city instead.
So:
SELECT P.id AS id, SUM(P.price) AS price,
P.tax,
COALESCE(county, city) AS location
I have 2 tables:
Customers:
- ID
- NAME
Modulemembers:
- ID
- customer_id
- enabled
I use this to enable a function of my site for some customers.
I have a form where the admin of the site can add the module for a customer, so i need a query that looks for customers that are NOT member of the modulemembers table.
I made this:
SELECT customers.id,
customers.name
FROM customers,
modulemembers
WHERE customers_id != modulemembers.cust_id
ORDER BY customers.name
but it does not work. What am I doing wrong?
You can use NOT EXISTS in the WHERE clause to get the result:
SELECT c.id,
c.name
FROM customers c
WHERE not exists (select customer_id
from modulemembers m
where c.id = m.customer_id)
order by c.name
You can use a LEFT OUTER JOIN and filter based on NULLs. The query below will pull in all results from the customers table and the modulemembers table. If there is not a match in the modulemembers table then the custid will be NULL.
SELECT c.id, c.name
FROM customers c
LEFT OUTER JOIN modulemembers m ON c.customers_id = m.cust_id
WHERE m.custid IS NULL
ORDER BY c.name
Try something like this:
SELECT c.ID AS customerId,
c.NAME AS fullName,
m.ID AS memberId
FROM Customers AS c
LEFT JOIN Modulemembers AS m ON m.customer_id = c.ID
WHERE m.ID IS NULL;