-
In this activity, we will be using the table
district
from thebank
database and according to the description for the different columns:-
A4: no. of inhabitants
-
A9: no. of cities
-
A10: the ratio of urban inhabitants
-
A11: average salary
-
A12: the unemployment rate
-
Answer:
-
A4: no. of inhabitants
SELECT
a2 AS district_name,
a4 AS no_of_inhabitants,
rank() over(
ORDER BY
a4 DESC
) AS ranking
FROM
district;
-
A9: no. of cities
SELECT
a2 AS district_name,
a9 AS no_of_cities,
rank() over(
ORDER BY
a9 DESC
) AS ranking
FROM
district;
ℹ️
|
Notice that the number of cities can repeat or tie. When this occurs the two values receive the same ranking number, and then a gap is left in the ranking numbers. The Rank() functions uses a Standard competition ranking strategy ( If I didn’t want a "gap" in the ranking numbers, then I should use the Dense_rank()
function which uses a Dense ranking strategy ( |
SELECT
a2 AS district_name,
a9 AS no_of_cities,
dense_rank() over(
ORDER BY
a9 DESC
) AS ranking
FROM
district;
-
A10: the ratio of urban inhabitants
SELECT
a2 AS district_name,
a10 AS ratio_of_urban_inhabitants,
rank() over(
ORDER BY
a10 DESC
) AS ranking
FROM
district;
-
A11: average salary
SELECT
a2 AS district_name,
a11 AS average_salary,
rank() over(
ORDER BY
a11 DESC
) AS ranking
FROM
district;
-
A12: the unemployment rate
SELECT
a2 AS district_name,
a12 AS unemployment_rate,
rank() over(
ORDER BY
a12 DESC
) AS ranking
FROM
district;
-
An overview ranked by unemployment rate
SELECT
a2 AS district_name,
a12 AS unemployment_rate,
rank() over(
ORDER BY
a12 DESC
) AS ranking,
a4 AS no_of_inhabitants,
rank() over(
ORDER BY
a4 DESC
) AS ranking,
a9 AS no_of_cities,
rank() over(
ORDER BY
a9 DESC
) AS ranking,
a10 AS ratio_of_urban_inhabitants,
rank() over(
ORDER BY
a10 DESC
) AS ranking,
a11 AS average_salary,
rank() over(
ORDER BY
a11 DESC
) AS ranking
FROM
district
ORDER BY
unemployment_rate DESC;
Answer:
SELECT
a3 AS region,
sum(a4) AS no_of_inhabitants,
rank() over(
ORDER BY
sum(a4) DESC
) AS ranking,
sum(a9) AS no_of_cities,
rank() over(
ORDER BY
sum(a9) DESC
) AS ranking,
avg(a10) AS ratio_of_urban_inhabitants,
rank() over(
ORDER BY
avg(a10) DESC
) AS ranking,
avg(a11) AS average_salary,
rank() over(
ORDER BY
avg(a11) DESC
) AS ranking,
avg(a12) AS unemployment_rate,
rank() over(
ORDER BY
avg(a12) DESC
) AS ranking
FROM
district
GROUP BY
1
ORDER BY
no_of_inhabitants DESC;
1. Use the transactions table in the bank
database to find the Top 20 account_ids
based on the amount
.
Answer:
SELECT
account_id,
amount,
rank() over(
ORDER BY
amount DESC
) AS top_20_transactions
FROM
trans
LIMIT
20;
-
Keep using the
bank
database.
Answer:
SELECT
district_id,
district.a2 AS district_name,
count(*) AS total_number_of_customers,
rank() over(
ORDER BY
count(*) DESC
) AS ranking
FROM
client
INNER JOIN district ON `client`.district_id = district.a1
GROUP BY
1;
Answer:
SELECT
district.a3 AS region_name,
count(*) AS total_number_of_customers,
rank() over(
ORDER BY
count(*) DESC
) AS ranking
FROM
client
INNER JOIN district ON `client`.district_id = district.a1
GROUP BY
1;
Answer:
SELECT
district_id,
district.a2 AS district_name,
sum(loan.amount) AS total_amount_borrowed,
floor(avg(loan.amount)) AS average_loan
FROM
loan
INNER JOIN account ON loan.account_id = account.account_id
INNER JOIN district ON account.district_id = district.a1
GROUP BY
1
ORDER BY
3 DESC;
Answer:
SELECT
a1 AS district_id,
a2 AS district_name,
count(*) AS accounts_opened,
concat('19', left(`date`, 2)) AS year
FROM
account
INNER JOIN district ON account.district_id = district.a1
GROUP BY
1,
4
ORDER BY
1,
4 DESC;