I have one table (city_data) with the columns 'year', 'city_name' and 'avg_temp'. The other table is global_data and has the columns 'avg_temp' and 'year'. I am trying to create a table which has the average temperature of London, the global average temperature for all the years.
My SQL code is the following:
SELECT city_data.avg_temp, city_data.year
FROM city_data
JOIN global_data
ON city_data.year = global_data.avg_temp
WHERE city= 'Paris'
I get no error but cannot get the expected result. I think I am not able to understand what I need to put in the ON clause.
I suppose that the table global_data has one row per year, while city_data has one row per year and city. This means you can simply join the two on the year they have in common:
select year, g.avg_temp as global_average, c.avg_temp as paris_average
from global_data g
join city_data c using (year)
where c.city = 'Paris'
order by year;
If you don't like to join the two and only then mention Paris, you can also turn this into:
select year, g.avg_temp as global_average, p.avg_temp as paris_average
from global_data g
join (select * from city_data where city = 'Paris') p using (year)
order by year;
As of MySQL 8 you can even use a WITH clause to "prepare" Paris :-)
with paris as (select * from city_data where city = 'Paris')
select year, g.avg_temp as global_average, paris.avg_temp as paris_average
from global_data g
join paris using (year)
order by year;
Or consider Paris a join criteria:
select g.year, g.avg_temp as global_average, p.avg_temp as paris_average
from global_data g
join city_data p on p.year = g.year and p.city = 'Paris'
order by g.year;
As you see, there are many ways to write the same query and the DBMS would probably come up with the same execution plan for all. So, just pick the query you like best.
This is completely based on the partial understanding
SELECT city_name, AVG(city_data.avg_temp) as global_avg
FROM
city_data
JOIN global_data
ON city_data.year = global_data.year
WHERE city_name = 'Paris'
GROUP BY city_data.city_name
FROM city_data
JOIN global_data
ON city_data.year = global_data.year
-- it should be global_data.year in Join
WHERE city= 'Paris'
As year is the only common attribute, join using it. Then fetch the city average table as it is readily availble in the city_data. Lastly do the avg for global_data, as you want to fetch it across years.
SELECT `city_name`, `city_data`.`avg_temp` AS city_avg_temp, AVG(`global_data`.`avg_temp`) AS global_avg_temp
FROM `city_data`
JOIN `global_data`
ON `city_data`.`year` = `global_data`.`year`
WHERE `city_name` = 'London'
GROUP BY `city_data`.`city_name`
Related
Let's get to the point:
My first query:
select hotel, sum(adult) from bookings
group by hotel;
and it shows the correct sum number, but whenever I try to join with other table:
select b.hotel, avg(r.hotelfacilities) as FacilitiesScore,
avg(r.hotelservice) as ServiceScore,
avg(r.hotelroom) as RoomScore,
avg(b.rate) as PriceScore,
sum(b.adult) as Persons
from bookings b join reviews r
on b.id = r.booking_id
group by b.hotel;
the number is weird and a lot bigger than it should be. Where did I do wrong?
select b.hotel,
avg(r.hotelfacilities) as FacilitiesScore,
avg(r.hotelservice) as ServiceScore,
avg(r.hotelroom) as RoomScore,
avg(b.rate) as PriceScore,
sum(b.adult) as Persons
from bookings b join reviews r
group by b.hotel
on b.id = r.booking_id;
I have two databases
TABLE_ORDERS with id,created,user_id.....status_id
TABLE_STATUSES with it,title,ordering,month
ordering is an integer for ordering statuses and month is the month number (01-January, 02-February, ...)
I would like to create somethig like:
SELECT *
FROM TABLE_ORDERS,
TABLE_STATUSES
WHERE 1
AND TABLE_STATUSES.month >= '7'
ORDER BY TABLE_STATUSES.ordering
What should be the right syntax?
The wished result is a table of orders ordered by statues like "To be delivered on January, To be delivered on February" that will change automatically month by month.
Thank you for your support!
I think you should try
Select * from TABLE_ORDERS o inner join TABLE _STATUSES s ON o.id=s.id WHERE MONTH(s.month)>=7 ORDER BY s.ordering.
You can filter out months gretter than 7.
Good luck.
your query can go like this :
SELECT TABLE_ORDERS.*, TABLE_STATUSES.month
FROM TABLE_ORDERS INNER JOIN TABLE_STATUSES
ON TABLE_STATUSES.id = TABLE_ORDERS.status_id
ORDER BY TABLE_STATUSES.ordering
An example can be :
SELECT column_list
FROM table_1
INNER JOIN table_2 ON table_1.columnname = table_2.columnname;
I am working on an assignment:
Compare the average daily revenue (as I define it in Teradata Week 5
Exercise Guide) of the store with the highest msa_income and the store
with the lowest median msa_income (according to the msa_income field).
In what city and state were these two stores, and which store had a
higher average daily revenue?
... and the answer key has a inner join in ON statement, which confused me a lot. I have only learnt Join in FROM. So I searched online about an inner join in ON statement, there was not much about it.
I am a new learner, so this question might be really basic. Thanks in advance for your patience!
The line I where I had a problem is: ON m.store=t.store JOIN strinfo s
SELECT SUM(store_rev. tot_sales)
SUM(store_rev.numdays) AS daily_average,
store_rev.msa_income as med_income,
store_rev.city, store_rev.state
FROM (SELECT COUNT (DISTINCT t.saledate) as numdays,
EXTRACT(YEAR from t.saledate) as s_year,
EXTRACT(MONTH from t.saledate) as s_month, t.store,
sum(t.amt) as tot_sales,
CASE
when extract(year from t.saledate) = 2005 AND extract(month from t.saledate) = 8 then 'exclude'
END as exclude_flag, m.msa_income, s.city, s.state
FROM trnsact t JOIN store_msa m
ON m.store=t.store JOIN strinfo s
ON t.store=s.store
WHERE t.stype = 'P' AND exclude_flag IS NULL
GROUP BY s_year, s_month, t.store, m.msa_income, s.city, s.state
HAVING numdays >= 20) as store_rev
WHERE store_rev.msa_income IN ((SELECT MAX(msa_income)
FROM store_msa),(SELECT MIN(msa_income) FROM store_msa))
GROUP BY med_income, store_rev.city, store_rev.state;
Perhaps it would be easier to follow if written like this:
FROM trnsact t JOIN
store_msa m
ON m.store = t.store JOIN
strinfo s
ON t.store = s.store
The JOIN isn't in the ON statement. The FROM clause consists of multiple joins, chained together.
You can think of the JOIN keyword as ending the ON clause and starting a new join condition.
Having two tables with the following schema:
Population(country, population)
Economy(country, GDP, poverty).
I have to find the top 5 countries in terms of the poverty rate and the GDP per capita (whic is GDP/population). I tried to solve this problem with the following query, but I don't know whether it returns me the right values:
SELECT
A.country,
A.GDP_per_capita,
A.poverty
FROM (
SELECT
country(E."GDP"*1000000000)/P.PopulationGDP_per_capita,
poverty
FROM "Population" P
NATURAL JOIN "Economy" E
) AS A
JOIN (
SELECT (E."GDP"*1000000000)/P.Population AS GDP_per_capita,
min(poverty) AS min_poverty
FROM "Population" P
NATURAL JOIN "Economy" E
GROUP BY GDP_per_capita
ORDER BY GDP_per_capita DESC
LIMIT 5
) AS B ON A.GDP_per_capita = B.GDP_per_capita
AND A.poverty = B.min_poverty
;
Is this query right?
Assuming there are no ties, I would expect to see something like this...
SELECT p.*
, e.GDP
, e.poverty
. e.GDP/p.population gdp_per_cap
FROM Population p
JOIN Economy e
ON e.something = p.something
ORDER
BY one thing or another thing
LIMIT 5;
I have this query wherein I want to find out the sales for the current year and the sales for last year. I cannot make it into 2 separate queries since it has to be of the same item code. Meaning the item codes used in the sales for the current year must also be the item codes used for the sales last year.
The code below is working but it takes almost 8 to 9 minutes to fetch
select p.itemcode,
p.itemdescription,
( select round((SUM(SA.QUANTITY*P.SellingPrice)),2)
from sales s
join product p on s.itemcode=p.itemcode
where YEAR(s.date) = 2013
),
( select round((SUM(SA.QUANTITY * P.SellingPrice)),2)
from sales s
join product p on s.itemcode=p.itemcode
where YEAR(s.date) = 2012
)
from product p
join supplier s on p.suppliercode = s.suppliercode
join currency c on c.countrycode=s.countrycode
join country co on co.countrycode=c.countrycode
JOIN SALES SA ON SA.ITEMCODE=P.ITEMCODE
where c.countrycode = 'NZ'
group by p.itemcode
limit 10
Ideally the output should be
Itemcode Itemdescription SalesforCurrentYear SalesforLastYear
GS771516 BUBBLE PARTY MACHINE 1035300.00 2079300.00
GSNBC-025 X'MAS HOUSE 600612.25 1397163.25
GSNBC-031 BRANDENBURGER TOR 741010.75 1572207.25
Thanks!!
The query can be simplified by eliminating two joins:
select .......
.......
from product p
join supplier s on p.suppliercode = s.suppliercode
JOIN SALES SA ON SA.ITEMCODE=P.ITEMCODE
where s.countrycode = 'NZ'
group by p.itemcode
limit 10
Afterwards, two dependent subqueries in the select clause can be reduced to one outer join:
select p.itemcode,
p.itemdescription,
round((SUM( CASE WHEN YEAR(s.date) = 2013
THEN SA.QUANTITY*P.SellingPrice
ELSE 0 END
)),2) As Sum2013,
round((SUM( CASE WHEN YEAR(s.date) = 2012
THEN SA.QUANTITY * P.SellingPrice
ELSE 0 END
)),2) As Sum2012
from product p
join supplier s on p.suppliercode = s.suppliercode
LEFT JOIN SALES SA ON SA.ITEMCODE=P.ITEMCODE
where s.countrycode = 'NZ'
group by p.itemcode
limit 10
Please try this query and let us know how it will perform.
Follow any of these steps
1.You can parse your query.
2.Remove redundant statements.
3.Use inner join or outer join.
You've got sales three times in the same scope. I'd get rid of two of those, and that should help. Also, in terms of business logic, all of these tables seem mandatory for a sale. If that's true, you should use "inner join", for compatibility with standard SQL - even though it's the same in MySQL.