Is it possible to list the city name which only contains unique Coast and Region? (i.e. there are multiple cities in the same coast and region, thus they should be excluded)
Example:
TABLE_NAME = MAP
City Coast Region
New York East 1
Buffalo East 1
LA West 2
Seattle West 1
San Jose West 3
Florida East 4
Boston East 2
San Diego West 2
In this example, I want the output to be:
Seattle
San Jose
Florida
Boston
New York and Buffalo are omitted because they share the same coast and region, just like LA and San Diego.
I tried:
SELECT DISTINCT COAST, REGION
FROM MAP
which gave me the Cost and Region, but not the name of the city.
I tried:
SELECT CITY
FROM MAP
GROUP BY COAST, REGION
But it gave me an error (see error below).
Thanks for any help!
EDIT:
I tried adding the HAVING clause, but it still gave me the same error as before. This is the error I had:
ERROR 1055 (42000); Expression #1 of SELECT list is not in GROUP BY clause and contain nonaggregated column run_iu98jon.MAP.CITY which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
You've very close, you just need a HAVING clause, which is like a WHERE clause for group by-aggregated metrics.
SELECT CITY
FROM MAP
GROUP BY COAST, REGION
HAVING COUNT(*) = 1;
Edit: Here's the SQL Fiddle link: http://sqlfiddle.com/#!9/8da83b/2/0
Edit 3: This seems needlessly complicated, but it works and avoids using any fields that aren't grouped.
SELECT City
FROM Map
INNER JOIN (
SELECT Coast, Region, COUNT(*) as RowCount
FROM Map
GROUP BY Coast, Region
) AS CoastRegion ON
Map.Coast = CoastRegion.Coast AND
Map.Region = CoastRegion.Region
WHERE
CoastRegion.RowCount = 1
As an aside, it seems pretty silly to use StackOverflow to solve your HackerRank problems. I'm including this as a learning exercise ;)
Related
I have a table named sales in a MySQL database that looks like this:
company manufactured shipped
Mercedes Germany United States
Mercedes Germany Germany
Mercedes Germany United States
Toyota Japan Canada
Toyota Japan England
Audi Germany United States
Audi Germany France
Audi Germany Canada
Tesla United States Mexico
Tesla United States Canada
Tesla United States United States
Here is a Fiddle: http://www.sqlfiddle.com/#!17/145ff/3
I would like to return the list of companies that ship ALL of their products internationally (that is, where the value in the manufactured column differs from the value in the shipped column for ALL records of a particular company).
Using the example above, the desired result set would be:
company
Toyota
Audi
Here is my (hackish) attempt:
WITH temp_table AS (
SELECT
s.company AS company
, SUM(CASE
WHEN s.manufactured != s.shipped THEN 1
ELSE 0
END
) AS count_international
, COUNT(s.company) AS total_within_company
FROM
sales s
GROUP BY
s.company
)
SELECT
company
FROM
temp_table
WHERE count_international = total_within_company
Essentially, I count the instances where the columns do not match. Then I check whether the sum of those mismatched instances matches the number of records within a given group.
This approach works, but it's far from an elegant solution!
Can anyone offer advice as to a more idiomatic way to implement this query?
Thanks!
We can GROUP BY company and use a HAVING clause to say all countries in shipped must differ to the country in manufactured:
SELECT company
FROM sales
GROUP BY company
HAVING COUNT(CASE WHEN manufactured = shipped THEN 1 END) = 0;
Try out here: db<>fiddle
The fiddle linked in the question is a Postgres DB, but MySQL is taged as DBMS.
In a MySQL DB, the above query can be simplified to:
SELECT company
FROM sales
GROUP BY company
HAVING SUM(manufactured = shipped) = 0;
In a Postgres DB, this is not possible.
You have to think in sets... you want to display all without a match -- find the matches display the rest
SELECT DISTINCT company
FROM sales
WHERE company NOT IN (
SELECT company
FROM sales
WHERE manufactured = shipped
)
I want to fetch records from a table that contains duplicate records. I want the output to be like only two duplicate records from each set of duplicate records in overall record output set.
example-
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Monesy
Russia
Bhumi
India
Peter
England
Bruice
England
Radhe
India
Output should have only two duplicate set of records from all duplicate of similar type as we can see in output below the country is repeating only two times and it took only first two counters of duplicate records in final record set -
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Peter
England
You can number the lines by the window and select only the first N.
Sorting should be chosen according to the business logic of the query.
For example:
;WITH numbered_name AS
(
SELECT *
, ROW_NUMBER() OVER (PARTITION BY t.Country ORDER BY t.Name) rn
FROM table t
)
SELECT Name
, Country
FROM numbered_name
WHERE rn <= 2
Name Place visited
Ash New york
Bob New york
Ash Chicago
Bob Chicago
Carl Chicago
Carl Detroit
Dan Detroit
Above is the sample table. The output should be two names who visited place together. I.e. the output should be Ash and Bob since the places visited by Ash also visited by Bob.
Output:
Name1 Name2
Ash Bob
What is a query for this using MySQL or even relational algebra?
The simplest method is to use group_concat(). Assuming no duplicates,
select places, group_concat(names) as names
from (select name, group_concat(place order by place) as places
from t
group by name
) t
group by places
having count(*) > 1;
This will return all the names with exactly the same places on a single row. The names will be in a comma-delimited list.
I have the following data in my database table, since I'm fairly new to MYSQL i'm having problems in querying it to give me the following output
City Subject
london english
toronto math
london math
london math
toronto english
toronto english
There can only be two subjects, english or math. Im trying to output the data this way, first the query should pick all the distinct items in the city column. Then tell me the count of each subject in that city.
output
city English Math
london 1 2
toronto 2 1
I tried grouping, but since I don't know mysql that well, I realized it just groups the subjects together and eats the cities while grouping.
try this:
SELECT city,
SUM(IF(subject='english',1,0)) AS English,
SUM(IF(subject='math',1,0)) AS Math
FROM foo
GROUP BY city;
I have a table of cities that all share the same area code:
367 01451 Harvard Worcester Massachusetts MA 978 Eastern
368 01452 Hubbardston Worcester Massachusetts MA 978 Eastern
369 01453 Leominster Worcester Massachusetts MA 978 Eastern
The table has multiple area codes, all with multiple cities.
What I'd like to do is only select one city from each area code and delete any extra cities from duplicate area codes. What would be the best query to accomplish this?
I believe:
Mysql4: SQL for selecting one or zero record
Is coming close to what I need but didn't quite get what/how those answers were working.
Note The "978" row is the "area_code" row, table name is "zip_code".
DELETE c.*
FROM zip_code c
JOIN (
SELECT area_code, MIN(id) AS mid
FROM zip_code
GROUP BY
area_code
) co
ON c.area_code = co.area_code
AND c.id <> co.mid