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

Mr Punk da Silva @mrpunkdasilva

About: I'm a Brazilian Software Engineer passionate about creating elegant solutions for complex problems. I specialize in full-stack development with a focus on clean code, solid architecture, and exception

Location:
In my mind
Joined:
Jul 19, 2023

Hackerrank - SQL - Revising the Select Query II

Publish Date: Jul 21
1 0

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.

Comments 0 total

    Add comment