DEV Community

Cover image for Hackerrank - SQL - Revising the Select Query II
Mr Punk da Silva
Mr Punk da Silva

Posted on • Edited on

Hackerrank - SQL - Revising the Select Query II

Repo: https://github.com/mrpunkdasilva/hackerrank/blob/main/sql/basic/revising-the-select-query-2/README.md

Problem Description

Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA.

Input Format

The CITY table is described as follows:

Field Type
ID NUMBER
NAME VARCHAR2(17)
COUNTRYCODE VARCHAR2(3)
DISTRICT VARCHAR2(20)
POPULATION NUMBER

Solution Approach

  1. Use a SELECT statement to query only the NAME column from the CITY table
  2. Apply a WHERE clause with two conditions:
    • COUNTRYCODE = "USA" (cities in America)
    • POPULATION >= 120000 (cities with populations larger than 120000)

Step-by-Step Explanation

  1. Start with the SELECT statement to retrieve only the NAME column:
   SELECT NAME
Enter fullscreen mode Exit fullscreen mode
  1. Specify the table to query from:
   FROM CITY
Enter fullscreen mode Exit fullscreen mode
  1. Add the WHERE clause with the required conditions:
   WHERE COUNTRYCODE = "USA" AND POPULATION >= 120000
Enter fullscreen mode Exit fullscreen mode
  1. The final query:
   SELECT
       NAME
   FROM 
       CITY
   WHERE
       COUNTRYCODE = "USA" AND
       POPULATION >= 120000
   ;
Enter fullscreen mode Exit fullscreen mode

Expected Output

The query will return the names of all cities in the USA with populations greater than 120000.

Top comments (0)