[HackerRank/SQL] Basic Select 문제풀이

Sooyeon·2023년 10월 23일
0

문제풀이 

목록 보기
8/95
post-thumbnail

[HackerRank/SQL] Basic Select


Revising The Select Query 1

Problem

Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.

The CITY table is described as follows:

solution

SELECT *
FROM CITY
WHERE population >=100000 and countrycode ='USA'


Revising The Select Query 2

Problem

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

solution

SELECT name
FROM city
WHERE population >=120000 and countrycode='USA';

Select ALL

Problem

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

The CITY table is described as follows:

solution

SELECT *
FROM CITY


Select By ID

Problem

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

solution

SELECT *
FROM city
WHERE ID=1661;


Japanese Cities' Attributes

Problem

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

Solution

SELECT *
FROM city
WHERE countrycode='JPN'


Japanese Cities' Names

Problem

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

Solution

SELECT name
FROM city
WHERE countrycode='JPN';

Weather Observation Station1

Problem

Query a list of CITY and STATE from the STATION table.
The STATION table is described as follows:

Solution

SELECT city,state 
FROM station


Weather Observation Station3

Problem

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.

Solution

SELECT DISTINCT city
FROM station
WHERE MOD(ID,2)=0

extra solution

# 같은 쿼리 
SELECT DISTINCT city
FROM station
WHERE id%2=0

=> ID number가 짝수인 city 추출하는 것
MOD함수 결과 값은,%로 2로 나누었을때 나머지가 0인값 추출하는것과 동일


Weather Observation Station4

Problem

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)
FROM station


Weather Observation Station6

Problem

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.

Solution

SELECT DISTINCT city
FROM station
WHERE left(city,1) in ('a','e','i','o','u')

=> 문자열을 다루는 조건을 넣어 데이터 추출시,
LIKE OR SUBSTR OR 정규식 표현
활용할 수 있음

extra solution

# LIKE와 와일드카드사용하기 
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%'

# SUBSTR사용하기 
SELECT DISTINCT city
FROM station
WHERE SUBSTR(city,1,1) in ('a','e','i','o','u')

# 정규식 표현식 사용하기
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^[aeiou]'

Weather Observation Station 7

Problem

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

solution

SELECT DISTINCT city
FROM station
WHERE RIGHT(city,1) in ('a','e','i','o','u');

extra solution

# LIKE와 와일드 카드 사용하기 
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'

# SUBSTR 사용하기 
SELECT DISTINCT city
FROM station
WHERE SUBSTR(city,-1,1) in ('a','e','i','o','u')

# 정규식 표현 사용하기 
SELECT DISTINCT city
FROM station
WHERE city REGEXP '[aeiou]$'


Weather Observation Station 8

Problem

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.

solution

SELECT DISTINCT city
FROM station
WHERE city REGEXP'^[aeiou].*[aeiou]$'

extra solution

# LEFT,RIGHT 사용하기 
SELECT DISTINCT CITY
FROM STATION
WHERE LEFT(city,1) in ('a','e','i','o','u') 
AND RIGHT(city,1) in ('a','e','i','o','u')

# SUBSTR 사용하기 
SELECT DISTINCT city
FROM station
WHERE SUBSTR(city,1,1) in ('a','e','i','o','u') 
AND SUBSTR(city,-1,1) in ('a','e','i','o','u')

# 정규식 표현 사용하기 
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[aeiou]' AND CITY REGEXP '[aeiou]$'

Weather Observation Station 9

Problem

Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.

solution

SELECT DISTINCT city
FROM station
WHERE city NOT REGEXP '^[aeiou].*';

extra solution

# LIKE 사용 (AND) 
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%'

# LEFT 사용
SELECT DISTINCT city
FROM station
WHERE LEFT(city,1) NOT IN ('a','e','i','o','u')

# SUBSTR 사용 
SELECT DISTINCT city
FROM station
WHERE SUBSTR(city,1,1) NOT IN ('a','e','i','o','u')

Weather Observation Station 10

Problem

Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates.

solution

SELECT DISTINCT city
FROM station
WHERE city NOT REGEXP '.*[aeiou]$'

extra solution

# LIKE 사용 (AND) 
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'

# RIGHT 사용
SELECT DISTINCT city
FROM station
WHERE RIGHT(city,1) NOT IN ('a','e','i','o','u')

# SUBSTR 사용 
SELECT DISTINCT city
FROM station
WHERE SUBSTR(city,-1,1) NOT IN ('a','e','i','o','u')


Weather Observation Station 11

Problem

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.

solution

SELECT DISTINCT city
FROM station
WHERE city NOT REGEXP '^[aeiou].*'
OR city NOT REGEXP '.*[aeiou]$'

extra solution

# LIKE 사용
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%'
OR
city NOT LIKE '%a'
AND city NOT LIKE '%e'
AND city NOT LIKE '%i'
AND city NOT LIKE '%o'
AND city NOT LIKE '%u'

# LEFT,RIGHT 사용
SELECT DISTINCT city
FROM station
WHERE LEFT(city,1) NOT IN ('a','e','i','o','u')
OR RIGHT(city,1) NOT IN ('a','e','i','o','u')

# SUBSTR 사용 
SELECT DISTINCT city
FROM station
WHERE SUBSTR(city,1,1) NOT IN ('a','e','i','o','u')
OR SUBSTR(city,-1,1) NOT IN ('a','e','i','o','u')


Weather Observation Station 12

Problem

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.

solution

SELECT DISTINCT city
FROM station
WHERE city NOT REGEXP '^[aeiou]' AND city NOT REGEXP '[aeiou]$' 

extra solution

# LIKE 사용
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%'
AND
city NOT LIKE '%a'
AND city NOT LIKE '%e'
AND city NOT LIKE '%i'
AND city NOT LIKE '%o'
AND city NOT LIKE '%u'

# LEFT,RIGHT 사용
SELECT DISTINCT city
FROM station
WHERE LEFT(city,1) NOT IN ('a','e','i','o','u')
AND RIGHT(city,1) NOT IN
('a','e','i','o','u')

# SUBSTR 사용 
SELECT DISTINCT city
FROM station
WHERE SUBSTR(city,1,1) NOT IN ('a','e','i','o','u')
AND SUBSTR(city,-1,1) NOT IN ('a','e','i','o','u')

Higher Than 75 Marks

Problem

Query the Name of any student in STUDENTS who scored higher than Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.

Input Format

The STUDENTS table is described as follows: The Name column only contains uppercase (A-Z) and lowercase (a-z) letters.

Sample Input

solution

SELECT name
FROM students
WHERE marks >75 
ORDER BY RIGHT(name,3),id

extra solution

# SUBSTR 사용하기 
SELECT name
FROM students
WHERE marks >75 
ORDER BY SUBSTR(name,LENGTH(name)-2,3),ID

Employee Names

Problem

Write a query that prints a list of employee names (i.e.: the name attribute) from the Employee table in alphabetical order.

Input Format

The Employee table containing employee data for a company is described as follows:

where employee_id is an employee's ID number, name is their name, months is the total number of months they've been working for the company, and salary is their monthly salary.

Sample Input

solution

SELECT name
FROM employee
ORDER BY name   


Employee Salaries

Problem

Write a query that prints a list of employee names (i.e.: the name attribute) for employees in Employee having a salary greater than per month who have been employees for less than months. Sort your result by ascending employee_id.

solution

SELECT name
FROM employee
WHERE salary >2000 and months <10
ORDER BY employee_id  

0개의 댓글