NOT IN SQL query not working - mysql

This is a question from Hacker Rank.
Let NUM be the number of CITY entries in STATION table, and NUMunique
be the number of unique cities. Query the value of NUM−NUMunique from
STATION.
This is the query that I've written.
SELECT COUNT(CITY) FROM STATION WHERE CITY NOT IN (SELECT COUNT(DISTINCT CITY) FROM STATION);
This is yielding the wrong answer. Any idea what am I doing wrong here.

You can use the DISTINCT keyword inside a COUNT() to get your answer in one simple query
SELECT COUNT(CITY) - COUNT(DISTINCT CITY) as the_answer FROM STATION;

Related

What is wrong with this subquery?

SELECT name, countrycode, population, avg(population)
FROM city
WHERE population > (SELECT avg(population) FROM city);
I need to figure out what is wrong with this subquery. Personally I think it is something to do with the placement of avg(population) but I dont know exactly.
When you use an aggregation function without GROUP BY, it aggregates the entire table into a single result row. Then it makes no sense to include non-aggregated columns in the SELECT list, because it will just be one value from arbitrary rows in the table.
If you want to include the average over the entire table along with the selected rows, join with the subquery so you can refer to it in the SELECT list, instead of using the subquery in the WHERE clause.
SELECT name, countrycode, population, avg_population
FROM city
JOIN (
SELECT avg(population) AS avg_population
FROM city
) AS x ON population > avg_population

Using correlated mysql subquery, fetch records from same table

Using correlated subquery, i have to find which country on each populated continent has the largest population.
I got the answer but did not understand how it worked for me
select Continent, Name, Population
From Country c
WHERE Population = (
SELECT MAX(Population)
From Country c2
WHERE c.Continent = c2.Continent
AND Population > 0
);
All records are in same table. Please help me to understand how this query executes and gets the correct results, also please explain me how the aliases c and c2 are working in this query.

use for COUNT command to create query in MYSQL

Below shows my original MY SQL table.
SELECT name, COUNT(city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
I need following result.
SAM has 4 records. But there are city of London in two records.
I need How many different cities in SAM's Records.
I did lots of queries, but I couldn't create my SQL query.
Add DISTINCT to the count parameter:
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;

Use of COUNT to display distinct records with more than 2 occurrences in MySQL

I have a table where I am trying to build a distinct list of all the cities with more than two occurrences in the table. I am trying the current query am am told "function count does not exist"? What am I doing wrong?
SELECT COUNT (city)
FROM `table1`
GROUP BY city
HAVING COUNT (city) >=2
Your query is correct you have given a space between COUNT and (City) it must be COUNT(City). that will work fine. Your query should be like this:
SELECT City, COUNT(city) Counts
FROM `table1`
GROUP BY City
HAVING COUNT(city) >=2;
See this SQLFiddle
USE ALIAS
SELECT COUNT(city) as SOME_TEXT FROM table1 GROUP BY city HAVING SOME_TEXT >=2

Repeating column names when outputting a query

Is it possible to repeat the names of a database table's columns when executing a query whenever the GROUP BY info change? My query is group by a column.
SELECT name.fname as First_Name,
name.sales as Sales,
name.city as City
FROM name
GROUP BY city
ORDER BY name.fname;
I think you want this:
SELECT name.fname as First_Name,
name.sales as Sales,
name.city as City
FROM name
ORDER BY city, fname;