I have a City table and I want to query all the cities of Pakistan that have the population larger than 200000 and their names start in H. Country code for Pakistan is Pak. How can I do that using SQL?
I want someones' help me to solve this
Thanks in advance
You can retrieve the required records from the database using SELECT statement. We use SELECT statement to retrieve data from database. Following is the syntax to use SELECT command:
Now, in order to retrieve specific data from the database, we use WHERE clause with SELECT statement. WHERE clause is used to specify the condition which must be satisfied in order to retrieve that specific data. Syntax for using WHERE clause with SELECT statement is as follows:
WHERE username = 'xyz';
Above statements will retrieve the first names of all the users whose username equals xyz.
Similarly, you can use SELECT statement with WHERE clause to retieve all the cities of Pakistan that have a population of more than 200000 and whose name starts in ‘H’. Use following snippet in your SQL script, by replacing the column names according to your database, to solve your problem:
SELECT * FROM City
WHERE CountryCode = 'Pak' AND Population > 200000 AND CityName LIKE 'H%';
In above snippet, there are multiple operators, explanation of whom is as follows:
- AND: Used to evaluate the result of two or more conditions. Returns true only if all the conditions are true.
- LIKE: This operator is used to match the pattern of a certain column.
- %: A wildcard character used with LIKE operator. It is used when you do not know how many characters preceed or proceed a certaincharacter.