Hi, I’m working through World Populations SQL Practice II, and am confused about the question: How many countries have the word “The” in their name?
The solution given is:
SELECT COUNT(*) FROM countries
where name LIKE ‘%The%’;
However, I noticed that this counts the entry ‘Netherlands’, which doesn’t actually have ‘The’ as part of the name in this database. It does, of course, contain the three letters ‘the’. If I wanted to only count the countries with the actual word ‘the’, how would I do it?
I’ve tried using the above code with LIKE ‘The %’ and ‘The%’, but then the code won’t compile.
Thanks in advance!
Normally your solution with ‘The %’ should work, if the names are starting with exactly that string. Maybe there is a typo?
Do you get an error message?
By the way, your query is not compiled. Compiling means to convert code into a program in machine language so the computer can execute it.
With a SQL query you simply ask your Database System to look for something in the database and return the results.
Thanks for the help, and for correcting me.
I think I know what the problem is. The countries are listed in the format Country, The. Using ‘The %’ wouldn’t find them, as there are characters before the word ‘The’.
Ah I see, you are right. This is the reason, why it doesn’t count them.
But it is possible to get the result you want.
Did you already figure it out?