Basic Select SQL Queries

Published: (March 29, 2026 at 01:30 AM EDT)
2 min read
Source: Dev.to

Source: Dev.to

HackerRank SQL Practice

Question 1

Task: Query all columns for a city in CITY with the ID 1661.

Solution:

SELECT * FROM CITY WHERE ID = 1661;

Question 2

Task: Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.

Solution:

SELECT * FROM CITY WHERE COUNTRYCODE = 'JPN';

Question 3

Task: Query the NAME field for all American cities in the CITY table with populations larger than 120,000. The COUNTRYCODE for the United States is USA.

Solution:

SELECT NAME
FROM CITY
WHERE POPULATION > 120000
  AND COUNTRYCODE = 'USA';

Question 4

Task: Query the list of city names from STATION that do not start with a vowel. The result must contain no duplicates.

Solution:

SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT LIKE 'A%'
  AND CITY NOT LIKE 'E%'
  AND CITY NOT LIKE 'I%'
  AND CITY NOT LIKE 'O%'
  AND CITY NOT LIKE 'U%';

Question 5

Task: Query a list of CITY and STATE from the STATION table.

Solution:

SELECT CITY, STATE
FROM STATION;

Question 6

Task: Query all columns for all American cities in the CITY table with populations larger than 100,000. The COUNTRYCODE for the United States is USA.

Solution:

SELECT *
FROM CITY
WHERE POPULATION > 100000
  AND COUNTRYCODE = 'USA';

Question 7

Task: Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

Solution:

SELECT COUNT(CITY) - COUNT(DISTINCT CITY) AS difference
FROM STATION;

Question 8

Task: Query the names of all Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.

Solution:

SELECT NAME
FROM CITY
WHERE COUNTRYCODE = 'JPN';
0 views
Back to Blog

Related posts

Read more »

Filtering a DB using SQL Queries

SQL Practice – Movie Rental Database Below are the queries I wrote while practicing various SQL concepts such as filtering, sorting, pagination, and pattern ma...

32 - Filter Assignments

SQL Queries for the film Table Movies where special_features is NULL sql SELECT FROM film WHERE special_features IS NULL; Movies where rental duration > 7 days...

Database- Querying and Filtering Data

Tasks 1. Retrieve film titles and their rental rates Alias title as Movie Title and rental_rate as Rate. sql SELECT title AS 'Movie Title', rental_rate AS 'Rat...