chevron_left

Problem:

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

Solution:

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:

SELECT column_names 
FROM table_name;

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:

SELECT column_names
FROM table_name
WHERE condition;

Example:

SELECT First_name
FROM users
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.

More Posts

How to query for null values in SQL sakshi - Jun 29
What does a relational database use to uniquely identify each row in a table? sanchi - May 2
SQL query syntax error in lambda function Tushar Shuvro - Jul 16, 2020
How to find table size in sql server? videepsinghal - Aug 14
How to partition a table in SQL server? videepsinghal - Aug 9
How to delete a table in SQL sakshi - Jul 11
CHANGE TABLE NAME OR COLUMN NAME IN SQL Faizan - Oct 18, 2020
sql query to find name ends with 'a' john_dev - Sep 22
Structured Query Language (SQL) is used to? john_dev - Sep 22
Flow of SQL Query Being human - May 24