SELECT stkcode, stkfirm, stkprice*stkqty*exchrate FROM stock JOIN nation ON stock.natcode = nation.natcode WHERE natname = 'Australia';
SELECT natname, SUM(stkdiv*stkqty*exchrate) FROM stock JOIN nation
ON stock.natcode = nation.natcode GROUP BY natname;
SELECT natname, AVG(stkdiv/stkprice*exchrate*100) FROM stock JOIN nation
ON stock.natcode = nation.natcode GROUP BY natname;
SELECT natname, AVG(stkdiv/stkprice*100) FROM stock JOIN nation
ON stock.natcode = nation.natcode GROUP BY natname HAVING AVG(stkdiv/stkprice*100) > (SELECT AVG(stkdiv/stkprice*100) FROM stock);
All exchange rates in nation are based on the UK pound, which as a result has a value of 1.00.
To convert to US dollars, all currency values must be divided by the US exchange rate (0.67 in nation). Similarly, dividing by 0.46 will convert currency values to Australian dollars. Thus, the total value of stocks in US dollars is:
SELECT SUM(stkqty*stkprice*exchrate/0.67) FROM stock JOIN nation ON stock.natcode = nation.natcode;
The question becomes more taxing when you want to extract the exchange rate from the nation table. Exchange rates vary daily, so the SQL code above is not very useful.
The query would be easy if in one row you had the stock, its exchange rate, and the exchange rate of the currency in which values should be expressed. To get a stock and its exchange rate in one row, you join the two tables and save as tempory table T1. Then you must concatenate to each row the exchange rate of the target currency -- this is done by using a product (see pages 258 of the text). The other difficult part is that you need to use the nation table twice (once for the exchange rate and once for the target currency). This query challenges your skills as this stage, but it is important to know that SQL can handle such requests.
SELECT `Value`/exchrate FROM (SELECT SUM(stkqty*stkprice*exchrate) AS `Value`
FROM stock JOIN nation
ON stock.natcode = nation.natcode) AS T1, nation
WHERE natname = 'United States';
Now, calculate the value of Australian stocks in Australian dollars.
The referential integrity constraint demands that for every foreign key in a table there is a corresponding primary key in another table (later, students will learn it can be in the same table, but at this stage it is easier to handle assuming another table). In other words, for every value of the foreign key, an identical value for the corresponding primary key must exist.
SELECT personid FROM person WHERE `person first` = 'Sheila' AND `person last` = "O'Hara";
personid |
---|
21 |
SELECT cartype, carcost, carsell FROM person JOIN car ON person.personid = car.personid WHERE `person first` = 'Bruce' AND `person last` = 'Bush';
cartype |
carcost |
carsell |
---|---|---|
coupe | 15500.00 | 14750.00 |
sports | 1255.00 | 1355.00 |
sedan | 950.00 | 2000.00 |
SELECT cartype, COUNT(cartype) FROM car GROUP BY cartype;
cartype |
expr1001 |
---|---|
coupe | 2 |
sedan | 5 |
sports | 3 |
SELECT `person first`, `person last`, cartype, carsell-carcost AS profit
FROM car JOIN person
ON person.personid = car.personid
WHERE (carsell-carcost)
< (SELECT AVG(carsell-carcost) FROM car);
person first | person last | cartype | profit |
---|---|---|---|
Nolan | Haley | sedan | 295.00 |
Bruce | Bush | coupe | -750.00 |
Bruce | Bush | sports | 100.00 |
Bruce | Bush | sedan | 1050.00 |
Sue | Lim | sedan | 600.00 |
Barbara | Capelli | sedan | 500.00 |
SELECT SUM(carsell-carcost) as `Gross profit` FROM car;
Gross profit |
---|
11295.00 |
CREATE VIEW PROFIT(id, type, cost, sell, profit) AS SELECT carid, cartype,carcost, carsell,carsell-carcost FROM car;
This page is part of the promotional and support material for Data Management (open edition) by Richard T. Watson |