Revising the Select Query
https://www.hackerrank.com/challenges/revising-the-select-query/problem?isFullScreen=true

Query all columns for all American cities in the CITY table with populations larger than 100000
. The CountryCode for America is USA
.
select * from CITY where population>100000 AND countrycode='USA';
Query the NAME field for all American cities in the CITY table with populations larger than 120000
. The CountryCode for America is USA
.
SELECT name FROM city WHERE population>120000 AND countrycode='USA';
Query all columns (attributes) for every row in the CITY table.
SELECT * from CITY;
Query all columns for a city in CITY with the ID 1661
.
SELECT * FROM CITY WHERE ID='1661';
Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN
.
SELECT name FROM CITY where countrycode='JPN';
Query a list of CITY and STATE from the STATION table.
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
SELECT city, state FROM station;
Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.
SELECT DISTINCT city FROM station WHERE MOD(ID, 2)=0;
Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table. The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.
SELECT COUNT(city) - COUNT(DISTINCT city) FROM station;
Weather Observation Station 5
Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
Sample Input
For example, CITY has four entries: DEF, ABC, PQRS and WXY.
Sample Output
ABC 3
PQRS 4
์๋์ ์๊ฐํ๊ธฐ ์ข ํ๋ค์๋ค.. ๋ด์ผ ๋ค์ ๊ณฑ์น์ด๋ณด์..! -> 1.30:ํน๋งํ์ฅฌ?
SELECT CITY, LENGTH(CITY)
FROM (
SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY), CITY
)
WHERE ROWNUM=1;
SELECT CITY, LENGTH(CITY)
FROM (
SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY
)
WHERE ROWNUM=1;
Weather Observation Station 6
Query the list of CITY names starting with vowels (i.e., a
, e
, i
, o
, or u
) from STATION. Your result cannot contain duplicates.
DISTINCT๋ก ํ๋๋ง ์ถ๋ ฅ๋๊ฒ ํ๊ณ , WHERE ์ ์์ LIKE์ ์ฌ์ฉํ๊ณ OR์กฐ๊ฑด์ ์ฌ์ฉํด์ ์ฐพ๋ ๋ฐฉ์
์ ๊ทํํ์์ ์ฌ์ฉํด์๋ ๊ฐ๋ฅํ๊ตฌ๋ ์ ๋? ->https://yongku.tistory.com/entry/HackerRank-Weather-Observation-Station-6-์ค๋ผํดOracle์์ ์๊ฒ๋์์
SELECT DISTINCT CITY
FROM STATION
WHERE (
CITY LIKE 'A%' OR
CITY LIKE 'E%' OR
CITY LIKE 'I%' OR
CITY LIKE 'O%' OR
CITY LIKE 'U%'
);
SELECT DISTINCT CITY
FROM STATION
WHERE (
REGEXP_LIKE(CITY, '^A|^E|^I|^O|^U')
);
Weather Observation Station 7
Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.
์ ๋ฌธ์ ์ ๊ฐ์ ๋งฅ๋ฝ์ด๋ค. ๋ฐ์ ๋ฐฉ์๋ ๋งค์ฐ ํฅ๋ฏธ๋ก์ ๋ค..! sql์ ์งค๋ผ์ ๋น๊ตํ๋ ๋ฐฉ์๋ ์๋ค๋ ๊ฒ์ด..!
SELECT DISTINCT CITY
FROM STATION
WHERE(
CITY LIKE '%a' OR
CITY LIKE '%e' OR
CITY LIKE '%i' OR
CITY LIKE '%o' OR
CITY LIKE '%u'
);
---------------------------------------------------------------------------
SELECT DISTINCT CITY
FROM STATION
WHERE SUBSTR(CITY, LENGTH(CITY), LENGTH(CITY))
IN ('a','e','i','o','u');
---------------------------------------------------------------------------
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '[aeiou]$';
Weather Observation Station 8
Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.
์.. ๊ทธ๋ ๊ฒ ์ด๋ ต์ง ์๊ฒ ํธ๋๊น ์ด๋ ๊ฒ ๋์ค๋๋ฐ ์ด๋ป๊ฒ ๋ ๊ฐ๋จํ๊ฒ ํ ์ ์์๊น๋ฅผ ์๊ฐํด๋ณด์
SELECT DISTINCT CITY
FROM (
SELECT CITY
FROM STATION
WHERE (
CITY LIKE 'A%' OR
CITY LIKE 'E%' OR
CITY LIKE 'I%' OR
CITY LIKE 'O%' OR
CITY LIKE 'U%'
)
)
WHERE (
CITY LIKE '%a' OR
CITY LIKE '%e' OR
CITY LIKE '%i' OR
CITY LIKE '%o' OR
CITY LIKE '%u'
);
---------------------------------------------------------------------------
SELECT DISTINCT CITY
FROM STATION
'^[aeiou]*[aeiou]$'
Weather Observation Station 9
Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.
์์ ์ฟผ๋ฆฌ๋ ํ ๋งํ๋... ๋ฌธ์ ๋ ๋ฐ์ ์ชฝ ์ฟผ๋ฆฌ๋ค -> ์ ๊ท ํํ์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์กฐ๊ธ ๋ ์์๋ณด
SELECT DISTINCT CITY
FROM STATION
WHERE NOT (
CITY LIKE 'A%' OR
CITY LIKE 'E%' OR
CITY LIKE 'O%' OR
CITY LIKE 'I%' OR
CITY LIKE 'U%'
);
---------------------------------------------------------------------------
SELECT DISTINCT CITY
FROM STATION
WHERE NOT REGEXP_LIKE(CITY, '^[AEIOU]');
Weather Observation Station 10
Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates.
SELECT DISTINCT CITY
FROM STATION
WHERE NOT REGEXP_LIKE(CITY, '[aeiou]$');
์์์ ๋ํ ์ ๊ท์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ^[๋ฌด์จ์ํ๋ฒณ~] ์ด๋ ๊ฒ ์ฌ์ฉํ๊ณ
๋๋๋ ๊ฒ์ ๋ํ ์ ๊ท์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ [๋ฌด์จ์ํ๋ฒณ~]$ ์ด๋ ๊ฒ ์ฌ์ฉํ๋ค!
Weather Observation Station 11
Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.
SELECT DISTINCT CITY
FROM STATION
WHERE
NOT REGEXP_LIKE(CITY, '^[AEIOU]')
OR
NOT REGEXP_LIKE(CITY, '[aeiou]$');
Weather Observation Station 12
Query the list of CITY names from STATION that do not start with vowels and do not end with vowels. Your result cannot contain duplicates.
SELECT DISTINCT CITY
FROM STATION
WHERE
NOT REGEXP_LIKE(CITY, '^[AEIOU]')
AND
NOT REGEXP_LIKE(CITY, '[aeiou]$');
Last updated
Was this helpful?