I have the following two queries:
Return the number of flights for each manufacturer:
SELECT COUNT(flight) AS num_flights, manufacturer
FROM flights, planes
WHERE flights.tailnum = planes.tailnum
GROUP BY manufacturer
ORDER BY num_flights DESC
That returns (not all results visible here, 27 rows in total):
-------------+---------------------------------+--+--+--+
| num_flights | manufacturer | | | |
+-------------+---------------------------------+--+--+--+
| 67623 | "BOEING" | | | |
+-------------+---------------------------------+--+--+--+
| 36244 | "AIRBUS INDUSTRIE" | | | |
+-------------+---------------------------------+--+--+--+
| 11676 | "AIRBUS" | | | |
+-------------+---------------------------------+--+--+--+
| 8932 | "MCDONNELL DOUGLAS AIRCRAFT CO" | | | |
+-------------+---------------------------------+--+--+--+
| 4856 | "EMBRAER" | | | |
+-------------+---------------------------------+--+--+--+
| 3998 | "MCDONNELL DOUGLAS" | | | |
+-------------+---------------------------------+--+--+--+
| 1259 | "MCDONNELL DOUGLAS CORPORATION" | | | |
+-------------+---------------------------------+--+--+--+
| 247 | "CESSNA" | | | |
+-------------+---------------------------------+--+--+--+
| 162 | "PIPER" | | | |
+-------------+---------------------------------+--+--+--+
| 65 | "BELL" | | | |
+-------------+---------------------------------+--+--+--+
| 63 | "DEHAVILLAND" | | | |
+-------------+---------------------------------+--+--+--+
| 63 | "FRIEDEMANN JON" | | | |
+-------------+---------------------------------+--+--+--+
| 55 | "STEWART MACO" | | | |
+-------------+---------------------------------+--+--+--+
| 54 | "LAMBERT RICHARD" | | | |
+-------------+---------------------------------+--+--+--+
| 51 | "KILDALL GARY" | | | |
+-------------+---------------------------------+--+--+--+
| 47 | "BEECH" | | | |
+-------------+---------------------------------+--+--+--+
| 44 | "MARZ BARRY" | | | |
+-------------+---------------------------------+--+--+--+
| 42 | "AMERICAN AIRCRAFT INC" | | | |
+-------------+---------------------------------+--+--+--+
| 40 | "LEBLANC GLENN T" | | | |
+-------------+---------------------------------+--+--+--+
| 32 | "AGUSTA SPA" | | | |
+-------------+---------------------------------+--+--+--+
| 27 | "SIKORSKY" | | | |
+-------------+---------------------------------+--+--+--+
| 25 | "PAIR MIKE E" | | | |
+-------------+---------------------------------+--+--+--+
| 22 | "DOUGLAS" | | | |
+-------------+---------------------------------+--+--+--+
| 19 | "LEARJET INC" | | | |
+-------------+---------------------------------+--+--+--+
| 18 | "AVIAT AIRCRAFT INC" | | | |
+-------------+---------------------------------+--+--+--+
| 17 | "HURLEY JAMES LARRY" | | | |
+-------------+---------------------------------+--+--+--+
| 13 | "GULFSTREAM AEROSPACE" | | | |
+-------------+---------------------------------+--+--+--+
And another one:
Return manufacturers with more than 200 planes:
SELECT COUNT(tailnum) AS num_planes, manufacturer
FROM planes
GROUP BY manufacturer
HAVING COUNT(*) > 200
ORDER BY num_planes DESC
That returns:
+------------+--------------------+
| num_planes | manufacturer |
+------------+--------------------+
| 1630 | "BOEING" |
+------------+--------------------+
| 400 | "AIRBUS INDUSTRIE" |
+------------+--------------------+
| 368 | "BOMBARDIER INC" |
+------------+--------------------+
| 336 | "AIRBUS" |
+------------+--------------------+
| 299 | "EMBRAER" |
+------------+--------------------+
Now I am trying to query the number of flights for each manufacturer that has more than 200 planes.
Wrote the following query:
SELECT COUNT(flight) AS num_flights, pl.manufacturer
FROM flights fl, planes pl JOIN
(SELECT COUNT(tailnum) AS num_planes, pl2.manufacturer
FROM planes pl2
GROUP BY pl2.manufacturer
HAVING COUNT(*) > 200
ORDER BY num_planes DESC) tm
ON pl.manufacturer = tm.manufacturer
GROUP BY pl.manufacturer
ORDER BY num_flights DESC
However this query returns incorrect number of flights, and takes ages to execute:
+-------------+--------------------+
| num_flights | manufacturer |
+-------------+--------------------+
| 262029020 | "BOEING" |
+-------------+--------------------+
| 64301600 | "AIRBUS INDUSTRIE" |
+-------------+--------------------+
| 59157472 | "BOMBARDIER INC" |
+-------------+--------------------+
| 54013344 | "AIRBUS" |
+-------------+--------------------+
| 48065446 | "EMBRAER" |
+-------------+--------------------+
What am I doing wrong here?
Table structures:
planes:
CREATE TABLE planes
(
tailnum VARCHAR(6),
manufacturer VARCHAR(50)
)
+----------+--------------------+
| tailnum | manufacturer |
+----------+--------------------+
| "N10156" | "EMBRAER" |
+----------+--------------------+
| "N102UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N103US" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N104UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N10575" | "EMBRAER" |
+----------+--------------------+
| "N105UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N107US" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| ... | ... |
+----------+--------------------+
flights:
CREATE TABLE flights
(
flight INT,
tailnum VARCHAR(6)
)
+--------+----------+
| flight | tailnum |
+--------+----------+
| 1545 | "N14228" |
+--------+----------+
| 1714 | "N24211" |
+--------+----------+
| 1141 | "N619AA" |
+--------+----------+
| 461 | "N668DN" |
+--------+----------+
| 1696 | "N39463" |
+--------+----------+
| ... | ... |
+--------+----------+
You can try this (joining planes and flights by tailnum), counting tailnums and flights grouping by manufacturer and filtering by having clause on COUNT(tailnum)
.
SELECT manufacturer AS "Manufacturer",
COUNT(DISTINCT tailnum) AS "Number of planes",
COUNT(flights) as "Number of flights",
FROM planes
INNER JOIN flights
ON (planes.tailnum=flights.tailnum)
GROUP BY manufacturer
HAVING COUNT(DISTINCT tailnum)>200
ORDER BY 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.