> For the complete documentation index, see [llms.txt](https://kyudo97.gitbook.io/library/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kyudo97.gitbook.io/library/sql_example/revising-the-select-query.md).

# Revising the Select Query

![CITY.jpg](https://s3.amazonaws.com/hr-challenge-images/8137/1449729804-f21d187d0f-CITY.jpg)

Query all columns for all American cities in the **CITY** table with populations larger than `100000`. The **CountryCode** for America is `USA`.&#x20;

```sql
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`.

```sql
SELECT name FROM city WHERE population>120000 AND countrycode='USA';
```

Query all columns (attributes) for every row in the **CITY** table.

```sql
SELECT * from CITY;
```

Query all columns for a city in **CITY** with the *ID* `1661`.

```sql
SELECT * FROM CITY WHERE ID='1661';
```

Query the names of all the Japanese cities in the **CITY** table. The **COUNTRYCODE** for Japan is `JPN`.&#x20;

```sql
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: \
![](https://s3.amazonaws.com/hr-challenge-images/9336/1449345840-5f0a551030-Station.jpg)

where **LAT\_N** is the northern latitude and **LONG\_W** is the western longitude.

```sql
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.&#x20;

```sql
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:

![](https://s3.amazonaws.com/hr-challenge-images/9336/1449345840-5f0a551030-Station.jpg)

where **LAT\_N** is the northern latitude and **LONG\_W** is the western longitude.

```sql
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.&#x20;

**Sample Input**

For example, **CITY** has four entries: **DEF, ABC, PQRS** and **WXY**.

**Sample Output**

```
ABC 3
PQRS 4
```

요놈은 생각하기 좀 힘들었다.. 내일 다시 곱씹어보자..! -> 1.30:킹만하쥬?

```sql
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>에서 알게되었음&#x20;

```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 (
    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을 짤라서 비교하는 방식도 있다는 것이..!

```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.

음.. 그렇게 어렵지 않게 푸니까 이렇게 나오는데 어떻게 더 간단하게 할 수 있을까를 생각해보자

```sql
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.

위의 쿼리는 할만한디... 문제는 밑에 쪽 쿼리다 -> 정규 표현식을 사용하는 방법을 조금 더 알아보

```sql
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.

```sql
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.

```sql
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.

```sql
SELECT DISTINCT CITY
FROM STATION
WHERE 
    NOT REGEXP_LIKE(CITY, '^[AEIOU]') 
    AND
    NOT REGEXP_LIKE(CITY, '[aeiou]$');
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kyudo97.gitbook.io/library/sql_example/revising-the-select-query.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
