Exercise

SQL Practice: Where New Immigrants Settled in Israel

Practice SELECT, JOIN, GROUP BY, HAVING and NULL on open government data about new immigrants and the localities that absorbed them, and learn to check a table before trusting a total.

Last updated:


Which localities absorbed the most new immigrants, and can we trust the total?

Every year, people move to Israel from many countries, and the city or town where they settle shapes local schools, housing and the job market.
The Ministry of Aliyah and Integration publishes open data on how many new immigrants settled in each locality (a city, town, village, kibbutz, moshav or institution).
In this practice set you will answer questions about it with SQL, and you will also check whether a total is trustworthy before you use it.

This practice continues the DB Browser SQL guide, part 1 and part 2.
Try each question before you open the answer. Every query below can also run in your browser:
select Try this query under it. Under each question there is an empty box where you write and run your own query.

Get the data

The data come from the New immigrants by absorbing locality dataset of the Ministry of Aliyah and Integration, on the Israeli open data portal data.gov.il (license: “Other (Open)”, which allows reuse).
We built the database from its two tables of figures, one for 2009–2018 and one for 2014–2023.
We did this with a short Python script.

Download sql-practice-aliyah.sqlite and open it with Open Database in DB Browser.
As always, work on a copy of the file.

The three tables

First, two words. In Hebrew, olim (singular oleh) are new immigrants who arrive in Israel under the Law of Return.
A region here is one of the absorption regions into which the ministry divides the country; the region name usually comes from a main city in it, but the region also includes many other localities, and the borders and names of the regions differ somewhat between the two source files.
Do not compare a region across periods.

TableOne row describesMain columns
regionsOne absorption regionregion_id, region_name
localitiesOne localitylocality_code, locality_name, region_id, regional_council
arrivalsNew immigrants in one locality during one periodlocality_code, period, total, f_ussr, ethiopia, india, france, usa, argentina, britain, other, residents, olim_percent

How the tables connect

  • localities.region_id points to regions.region_id.
  • arrivals.locality_code points to localities.locality_code.
  • To show a locality name or a region next to a number you need a JOIN.

What the columns hold

  • period is 2009-2018 or 2014-2023.
  • The columns f_ussr through other count immigrants by country of origin; f_ussr is the former Soviet Union.
  • residents is the locality’s population, and olim_percent is the immigrants’ share of it, in percent.

Localities missing from a table

  • The table localities lists 913 localities.
  • arrivals has rows for the period 2014-2023 for only 322 of them, and for the period 2009-2018 it has 910 rows in all.
  • A locality with no row in a period is absent from that table. The source does not say whether it had no immigrants or too few to be listed, so an absent locality is not a zero.

Names and missing values

  • Names are in Hebrew, for example 'אשדוד' (Ashdod) and 'באר שבע' (Beer Sheva).
  • NULL marks a missing value.

The columns of each table

regions:

ColumnTypeMeaning
region_idINTEGERThe region’s number, unique; the key that localities.region_id points to
region_nameTEXTThe region’s name, in Hebrew, for example 'ירושלים (מרחב)'

localities:

ColumnTypeMeaning
locality_codeINTEGERThe locality’s official code, unique; the key that arrivals.locality_code points to
locality_nameTEXTThe locality’s name, in Hebrew
region_idINTEGERThe absorption region of the locality; NULL for 5 localities that have no region in the data
regional_councilTEXTThe regional council the locality belongs to; NULL for localities that are not in a regional council (137 of 913 are NULL)

arrivals, one row for each locality and period:

ColumnTypeMeaning
locality_codeINTEGERThe locality, as in localities
periodTEXTThe period of the figures, 2009-2018 or 2014-2023; the periods overlap in 2014–2018
totalINTEGERAll the new immigrants who settled in the locality during the period
f_ussrINTEGEROf them, from the former Soviet Union
ethiopiaINTEGEROf them, from Ethiopia
indiaINTEGEROf them, from India; reported only for 2014-2023, NULL for every row of 2009-2018
france, usa, argentina, britainINTEGEROf them, from France, the United States, Argentina and Britain
otherINTEGEROf them, from all the other countries
residentsINTEGERThe number of residents of the locality; NULL where it is not reported
olim_percentREALThe immigrants’ share of the residents, in percent (5.7 means 5.7%)

The SQLite type describes how a value is stored. In 2009-2018 the source leaves out small counts, so an origin column can be NULL and the origin columns can add up to less than total.
In 2014-2023 the origin columns add up to total.

Practice

Write each query before opening the answer. In questions 1–5 we use only the period 2014-2023; question 6 shows why.

The queries use table aliases, IN, IS NULL and IS NOT NULL, calculations in a query and a JOIN of two or three tables.
Each is explained in the guide.

1. Which five localities absorbed the most new immigrants in 2014–2023?

Show a possible answer
SELECT l.locality_name, a.total
FROM arrivals a
JOIN localities l ON l.locality_code = a.locality_code
WHERE a.period = '2014-2023'
ORDER BY a.total DESC
LIMIT 5;

The result: Tel Aviv-Yafo (39,057), Netanya (38,987), Haifa (32,320), Jerusalem (26,288) and Bat Yam (16,474).
The JOIN is needed because arrivals holds only the locality code, not its name.
The filter on period matters: without it, many localities would appear twice, once for each period.

2. How many localities had immigrants making up more than 10% of their population in 2014–2023?

Show a possible answer
SELECT COUNT(*)
FROM arrivals
WHERE period = '2014-2023'
  AND olim_percent > 10;

The answer is 20. Look at which localities they are before drawing a conclusion:

SELECT l.locality_name, a.olim_percent, a.total, a.residents
FROM arrivals a
JOIN localities l ON l.locality_code = a.locality_code
WHERE a.period = '2014-2023'
  AND a.olim_percent > 10
ORDER BY a.total DESC;

Four are cities (Netanya with 14.8%, Bat Yam, Nahariya and Kiryat Yam), and their immigrants number between about 5,800 and 39,000.
The other 16 are small localities with a few hundred immigrants or fewer, and in Eyvim 648 of 682 residents (95.0%) are immigrants of the period.
A high share does not mean a large number of people: a ratio is only as meaningful as its denominator.

Scatter chart of 320 localities, 2014–2023: the number of residents across, on a scale where each step is ten times more, and the immigrants' share of the residents up. Localities with more than 10% (the dots above the dashed line, 20 of them) are mostly small, 16 of the 20 with fewer than 1,500 residents; Eyvim reaches 95%. The large cities have shares between 0.6% and 14.8%, for example Jerusalem 2.5% and Netanya 14.8%.
Each dot is one locality in 2014–2023: its number of residents across (each step to the right is ten times more) and the immigrants of the period as a percent of its residents up. The 20 amber dots are the localities above 10%. Two localities with no reported population have no dot, and a locality absent from the table is not drawn and is not a zero. The data name localities only in Hebrew, so only a few of them have a name here.

3. How many new immigrants settled in each region in 2014–2023? Show the largest first.

Show a possible answer
SELECT r.region_name,
       SUM(a.total) AS immigrants
FROM arrivals a
JOIN localities l ON l.locality_code = a.locality_code
JOIN regions r ON r.region_id = l.region_id
WHERE a.period = '2014-2023'
GROUP BY r.region_name
ORDER BY immigrants DESC;

The first rows: Netanya and the Sharon (52,442), Haifa (48,053), Tel Aviv (46,945), Holon (40,427) and Jerusalem (38,476).
Two JOINs are needed to go from an arrival row to a locality and then to its region.
A region collects many localities, and so its total is not the figure of the city it is named after.
The result has 11 rows, not 13: the regions Nazareth Illit and Rishon LeZion have no rows in the 2014–2023 table, so the JOIN leaves them out.
A region (or locality) that is absent from a table is a result to notice, not a zero.

Horizontal bar chart of new immigrants by absorption region, 2014–2023, for the 11 regions that have rows in that period's table. Netanya and the Sharon is largest with 52,442, then Haifa with 48,053, Tel Aviv with 46,945, Holon with 40,427 and Jerusalem with 38,476; Hadera is smallest with 11,109.
Each bar is one absorption region: the new immigrants who settled in its localities in 2014–2023 (this period only; the two periods are not added). Two of the 13 regions, Nazareth Illit and Rishon LeZion, have no rows in this period's table, so they have no bar; that is not a zero. A region is named after a main city but includes many localities.
A plausible mistake: no GROUP BY

The reasoning: SUM computes a total, so it seems enough to add the region name.
But without GROUP BY, SUM adds up the whole table and the query returns a single row, with one arbitrary region name next to the grand total.

SELECT r.region_name,
       SUM(a.total)
FROM arrivals a
JOIN localities l ON l.locality_code = a.locality_code
JOIN regions r ON r.region_id = l.region_id
WHERE a.period = '2014-2023';

4. In which regions did more than 30,000 immigrants settle in 2014–2023? Show the region name and the number of immigrants, largest first.

Then write a second query that compares, for each of those regions, the share of immigrants from the former Soviet Union.
The check below covers the first query only; the second one is in the answer.

Show a possible answer
SELECT r.region_name,
       SUM(a.total) AS immigrants
FROM arrivals a
JOIN localities l ON l.locality_code = a.locality_code
JOIN regions r ON r.region_id = l.region_id
WHERE a.period = '2014-2023'
GROUP BY r.region_name
HAVING SUM(a.total) > 30000
ORDER BY immigrants DESC;

Five regions pass: Netanya and the Sharon, Haifa, Tel Aviv, Holon and Jerusalem.
HAVING filters the groups after the sums are computed; WHERE filters rows before grouping, so WHERE SUM(a.total) > 30000 would raise an error.

For the share from the former Soviet Union, divide two sums:

SELECT r.region_name,
       ROUND(100.0 * SUM(a.f_ussr) / SUM(a.total), 1) AS percent_f_ussr
FROM arrivals a
JOIN localities l ON l.locality_code = a.locality_code
JOIN regions r ON r.region_id = l.region_id
WHERE a.period = '2014-2023'
GROUP BY r.region_name
ORDER BY percent_f_ussr DESC;

The share ranges from about 89% in the Haifa region to about 23% in the Jerusalem region.
Write 100.0 and not 100, as in Calculations in a query. Dividing the two sums gives the share of all immigrants in the region; averaging the localities’ own shares would give every small locality the same weight as a large city.

5. Pick two localities you know. How do their immigrants split by country of origin in 2014–2023? Example: Ashdod and Beer Sheva.

Show a possible answer
SELECT l.locality_name, a.total, a.f_ussr, a.france, a.usa, a.other
FROM arrivals a
JOIN localities l ON l.locality_code = a.locality_code
WHERE a.period = '2014-2023'
  AND l.locality_name IN ('אשדוד', 'באר שבע');

In Ashdod, 10,683 of 14,642 immigrants came from the former Soviet Union and 2,578 from France.
In Beer Sheva, 6,259 of 10,094 came from the former Soviet Union, and the rest are spread across several countries.
The five numbers do not add up to total here only because the query shows four of the eight origin columns; add ethiopia, india, argentina and britain to the SELECT and in 2014–2023 the sum equals total (see Check that a table is believable).
Replace the names with localities of your choice. A name must match exactly:
'תל אביב' returns no row, because the table saves 'תל אביב - יפו', and 'קריית שמונה' returns no row, because it is saved as 'קרית שמונה'.
When a name finds nothing, look for the saved spelling with LIKE, as in the guide:

SELECT locality_name
FROM localities
WHERE locality_name LIKE '%אביב%';

6. Add the totals of the two periods to get the number of immigrants in 2009–2023. What goes wrong?

Show a possible answer
SELECT period, SUM(total) AS immigrants
FROM arrivals
GROUP BY period;

The two periods give 241,247 and 348,377. Their sum, 589,624, is not the number of immigrants in 15 years, because the periods overlap:
both include 2014–2018, so those years are counted twice. A table can be correct row by row and still produce a wrong total once you ignore what each row covers.
Always read the description of a column such as period before adding across it.

A plausible mistake: summing without filtering the period

The reasoning: in questions 1 and 3 the filter on period seems like a detail.
Without it, every immigrant counted in both periods appears twice in the sums.

SELECT SUM(total)
FROM arrivals;

7. In which localities is the population missing in the period 2014–2023? Write the condition with IS NULL.

Show a possible answer
SELECT l.locality_name
FROM arrivals a
JOIN localities l ON l.locality_code = a.locality_code
WHERE a.period = '2014-2023'
  AND a.residents IS NULL;

Two localities have no population figure: Shaar Shomron and Tel Zion.
IS NULL is explained in the guide.

A plausible mistake: = NULL

The reasoning: = NULL reads naturally as “is missing”. But a comparison with NULL is never true, so the query returns no rows and gives the impression that nothing is missing.
Use IS NULL.

SELECT locality_code
FROM arrivals
WHERE period = '2014-2023'
  AND residents = NULL;

Check that a table is believable

Before using any total, test it against something you can verify.

  1. Do the pieces add up? In the 2014–2023 period, the origin columns add up exactly to total. In 2009–2018 they can differ: a locality’s origin columns, counting a missing value as 0, sum to less than its total in 232 of 910 rows, and to more than it (by 1 to 16) in 45 rows. The source does not explain the gap. Use total for the whole and the origin columns only for rough shares there.
  2. What does each row cover? The two periods overlap (question 6), and a locality can appear under a new name or in a different region in a later period. localities keeps the most recent name and region.
  3. Is a percentage in line with the counts? In 2014–2023, olim_percent matches total divided by residents, times 100 (to within 0.15 percentage points) in all 320 rows that have both numbers. In 2009–2018 it differs by more than that in 94 of the 901 rows that have both numbers, so check it before you use it there. A very high share in a tiny locality is a reminder that a ratio is only as meaningful as its denominator.
  4. Is anything missing? In 2014–2023 two localities have no residents (and no olim_percent). In 2009–2018 every row has residents, but 9 rows have no olim_percent.

What to remember

  • A JOIN connects a number to a name or a region: use the key column on both sides, and filter the period.
  • GROUP BY collects rows into groups, WHERE filters rows before grouping and HAVING filters groups after.
  • A total is only as good as the definition behind it: check overlaps, missing values and what each row covers.
  • To analyze these data with money, such as local authority budgets, or to build charts and statistics, the next step is Python with pandas.

Source: Ministry of Aliyah and Integration, Israel, “New immigrants by absorbing locality”, via data.gov.il.
The figures are reproduced as published, with these changes:
we reorganized them into related tables, normalized the percentage column, turned blank cells and spreadsheet errors such as #DIV/0! into NULL, and kept each locality’s most recent name and region.