8. Прості вправи з SQL

Використовуючи таблицю «nyc_census_blocks», дайте відповіді на наступні питання (не підглядайте відповіді!).

Ось деяка корисна інформація для початку. Згадайте з розділу Про наші дані визначення таблиці nyc_census_blocks.

blkid

15-значний код, який однозначно ідентифікує кожен блок перепису. Наприклад: 360050001009000

popn_total

Загальна кількість людей у переписному блоці

popn_white

Кількість людей, які самоідентифікуються як «білі» у цьому кварталі

popn_black

Кількість людей, які самоідентифікуються як «чорношкірі» в цьому кварталі

popn_nativ

Кількість людей, які самоідентифікуються як «корінні американці» в цьому кварталі

popn_asian

Кількість людей, які самоідентифікуються як «азіати» в блоці

popn_other

Кількість осіб, які самоідентифікуються з іншими категоріями в блоці

hous_total

Кількість житлових одиниць у кварталі

hous_own

Кількість житлових одиниць, що належать власникам, у кварталі

hous_rent

Кількість житлових одиниць, зайнятих орендарями в кварталі

boroname

Назва району Нью-Йорка. Манхеттен, Бронкс, Бруклін, Статен-Айленд, Квінс

geom

Полігональна межа блоку

And, here are some common SQL aggregation functions you might find useful:

  • avg() - the average (mean) of the values in a set of records

  • sum() - the sum of the values in a set of records

  • count() - the number of records in a set of records

Now the questions:

  • How many records are in the nyc_streets table?

    SELECT Count(*)
    FROM nyc_streets;
    
    19091
    
  • How many streets in NYC start with ‘B’?

    SELECT Count(*)
      FROM nyc_streets
      WHERE name LIKE 'B%';
    
    1282
    
  • What is the population of the City of New York?

    SELECT Sum(popn_total) AS population
      FROM nyc_census_blocks;
    
    8175032
    

    Примітка

    What is this AS? You can give a table or a column another name by using an alias. Aliases can make queries easier to both write and to read. So instead of our outputted column name as sum we write it AS the more readable population.

  • What is the population of the Bronx?

    SELECT Sum(popn_total) AS population
      FROM nyc_census_blocks
      WHERE boroname = 'The Bronx';
    
    1385108
    
  • How many «neighborhoods» are in each borough?

    SELECT boroname, count(*)
      FROM nyc_neighborhoods
      GROUP BY boroname;
    
       boroname    | count
    ---------------+-------
     Queens        |    30
     Brooklyn      |    23
     Staten Island |    24
     The Bronx     |    24
     Manhattan     |    28
    
  • For each borough, what percentage of the population is white?

    SELECT
      boroname,
      100.0 * Sum(popn_white)/Sum(popn_total) AS white_pct
    FROM nyc_census_blocks
    GROUP BY boroname;
    
       boroname    |    white_pct
    ---------------+------------------
     Brooklyn      | 42.8011737932687
     Manhattan     | 57.4493039480463
     The Bronx     | 27.9037446899448
     Queens        |  39.722077394591
     Staten Island | 72.8942034860154
    

8.1. Function List

avg(expression): PostgreSQL aggregate function that returns the average value of a numeric column.

count(expression): PostgreSQL aggregate function that returns the number of records in a set of records.

sum(expression): PostgreSQL aggregate function that returns the sum of records in a set of records.