You have details of some companies that trade internationally and strengthen the economy of their countries. You know the nationality of each company and you have a list of trades between the various companies. Your task is to generate a summary that consists of sums of the value of goods imported and exported by every country. Note that when a company buys some goods, it contributes to its country's total import, and when the company sells some goods, it contributes to its country's total export.You are given two tables: companies and trades, with the following structure: create table companies ( name varchar(30) not null, country varchar(30) not null, unique(name) ); create table trades ( id integer not null, seller varchar(30) not null, buyer varchar(30) not null, value integer not null, unique(id) );A row in table companies contains the name of a company and the name of the nationality of the company. A row in table trades contains the unique ID of the trade, the name of the selling company, the name of the buying company and the value of the traded goods.Write an SQL query that returns a table consisting of three columns, country, export, import, which contain the sums of the values of the exported (sold to other countries) and imported (purchased from other countries) goods for every country. Each country should appear in this table. The result table should be sorted increasingly by country.For example, for: companies: +-------------+--------------------+ | name | country | +-------------+--------------------+ | Alice s.p. | Wonderland | | Y-zap | Wonderland | | Absolute | Mathlands | | Arcus t.g. | Mathlands | | Lil Mermaid | Underwater Kingdom | | None at all | Nothingland | +-------------+--------------------+ trades: +----------+-------------+------------+-------+ | id | seller | buyer | value | +----------+-------------+------------+-------+ | 20121107 | Lil Mermaid | Alice s.p. | 10 | | 20123112 | Arcus t.g. | Y-zap | 30 | | 20120125 | Alice s.p. | Arcus t.g. | 100 | | 20120216 | Lil Mermaid | Absolute | 30 | | 20120217 | Lil Mermaid | Absolute | 50 | +----------+-------------+------------+-------+your query should return: +--------------------+--------+--------+ | country | export | import | +--------------------+--------+--------+ | Mathlands | 30 | 180 | | Nothingland | 0 | 0 | | Underwater Kingdom | 90 | 0 | | Wonderland | 100 | 40 | +--------------------+--------+--------+Assume that:There is no trade between companies within a single country;Every company in the table trades also appears in the table companies;Every company appears in table companies exactly once.
Question
You have details of some companies that trade internationally and strengthen the economy of their countries. You know the nationality of each company and you have a list of trades between the various companies. Your task is to generate a summary that consists of sums of the value of goods imported and exported by every country. Note that when a company buys some goods, it contributes to its country's total import, and when the company sells some goods, it contributes to its country's total export.You are given two tables: companies and trades, with the following structure: create table companies ( name varchar(30) not null, country varchar(30) not null, unique(name) ); create table trades ( id integer not null, seller varchar(30) not null, buyer varchar(30) not null, value integer not null, unique(id) );A row in table companies contains the name of a company and the name of the nationality of the company. A row in table trades contains the unique ID of the trade, the name of the selling company, the name of the buying company and the value of the traded goods.Write an SQL query that returns a table consisting of three columns, country, export, import, which contain the sums of the values of the exported (sold to other countries) and imported (purchased from other countries) goods for every country. Each country should appear in this table. The result table should be sorted increasingly by country.For example, for: companies: +-------------+--------------------+ | name | country | +-------------+--------------------+ | Alice s.p. | Wonderland | | Y-zap | Wonderland | | Absolute | Mathlands | | Arcus t.g. | Mathlands | | Lil Mermaid | Underwater Kingdom | | None at all | Nothingland | +-------------+--------------------+ trades: +----------+-------------+------------+-------+ | id | seller | buyer | value | +----------+-------------+------------+-------+ | 20121107 | Lil Mermaid | Alice s.p. | 10 | | 20123112 | Arcus t.g. | Y-zap | 30 | | 20120125 | Alice s.p. | Arcus t.g. | 100 | | 20120216 | Lil Mermaid | Absolute | 30 | | 20120217 | Lil Mermaid | Absolute | 50 | +----------+-------------+------------+-------+your query should return: +--------------------+--------+--------+ | country | export | import | +--------------------+--------+--------+ | Mathlands | 30 | 180 | | Nothingland | 0 | 0 | | Underwater Kingdom | 90 | 0 | | Wonderland | 100 | 40 | +--------------------+--------+--------+Assume that:There is no trade between companies within a single country;Every company in the table trades also appears in the table companies;Every company appears in table companies exactly once.
Solution
To solve this task, we need to write an SQL query that calculates the total value of goods exported and imported by each country. We can achieve this by joining the 'companies' and 'trades' tables twice: once for the selling companies (exports) and once for the buying companies (imports). Here is the SQL query:
SELECT co.country,
IFNULL(ex.export, 0) AS export,
IFNULL(im.import, 0) AS import
FROM companies co
LEFT JOIN (
SELECT c.country, SUM(t.value) AS export
FROM companies c
JOIN trades t ON c.name = t.seller
GROUP BY c.country
) ex ON co.country = ex.country
LEFT JOIN (
SELECT c.country, SUM(t.value) AS import
FROM companies c
JOIN trades t ON c.name = t.buyer
GROUP BY c.country
) im ON co.country = im.country
ORDER BY co.country;
This query works as follows:
-
The subquery 'ex' calculates the total value of goods exported by each country. It joins the 'companies' and 'trades' tables on the company name and groups the results by country. The SUM function is used to calculate the total value of goods sold by companies from each country.
-
The subquery 'im' calculates the total value of goods imported by each country. It works similarly to the 'ex' subquery, but it joins the 'companies' and 'trades' tables on the buyer's name.
-
The main query joins the 'companies' table with the 'ex' and 'im' subqueries using a LEFT JOIN. This ensures that all countries are included in the result, even if they have no exports or imports. The IFNULL function is used to replace NULL values with 0.
-
Finally, the results are ordered by country name.
Similar Questions
Question 8 of 10A country imports goods and services worth $700 million and exports goods and services worth about $500 million. This means that the country has a(n) _____.A.trade deficitB.absolute advantageC.comparative advantageD.trade surplus
The table below shows productivity of labour, i.e., outputs per worker. They represent the quantity of output per unit of labor that each country can produce in the two industries, Xand Y. Product Country A Country BGood X 8 6Good Y 4 2Determine which country has an absolute advantage in good X, absolute advantage in good Y and a comparative advantage in good X. Choose the correct answer from the options below. Country A, Country A, Country B Country B, Country A, Country ACountry A, Country B, Country B
Multiple Choice QuestionWhat is the result when a country's exports exceed its imports?Multiple choice question.A trade deficitA trade surplusA trade equilibriumAn immediate asset transaction
Assume a small open economy's domestic output (Y) is $750 million, its domestic spending is $850 million, and total imports are $150 million. Therefore, the country runs a trade ________, and total exports are ________. a. deficit; $250 million b. surplus; $250 million c. deficit; $50 million d. surplus; $50 million
Retrieve the name and address of all the suppliers from the "suppliers" table. The "suppliers" table has three columns:"supplier_id": An integer primary key."name": A varchar(50) column for the supplier's name."address": A varchar(100) column for the supplier's address.Insert three rows of sample data into the "suppliers" table, each with a unique supplier_id, name, and address.Finally, write a SQL query to select the name and address of all the suppliers from the "suppliers" table.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.