HackerRank SQL Practice -Basic Select
— Nikhitha Perapola
- Revising the Select Query I
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 countrycode = 'USA' and population>100000;
2. Revising the Select Query II
Query the NAME field for all American cities in the CITY table with populations larger than 120000
. The CountryCode for America is USA
.
The CITY table is described as follows:
Solution:
select name from city where countrycode = 'USA' and population > 120000;
3. Select All
Query all columns (attributes) for every row in the CITY table.
The CITY table is described as follows:
Query all columns (attributes) for every row in the CITY table.
The CITY table is described as follows:
Solution:
select * from city;
4. Select by ID:
Query all columns for a city in CITY with the ID 1661
.
The CITY table is described as follows:
Solution:
select * from city where id = 1661;
5. Japanese Cities’ Attributes
Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN
.
The CITY table is described as follows:
Solution:
select * from city where countrycode = 'JPN';
6. Japanese Cities’ Names
Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN
.
The CITY table is described as follows:
Solution:
select name from city where countrycode = 'JPN';
7. Weather Observation Station 1
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.
Solution:
select city,state from station;
8. Weather Observation Station 3
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.
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Solutions:
select distinct(city) from station where mod(id,2)=0 order by 1;
9. Weather Observation Station 4
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.
Solution:
select count(city) - count(distinct(city)) from station;
10. 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.
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Sample Input
For example, CITY has four entries: DEF, ABC, PQRS, and WXY.
Sample Output
ABC 3
PQRS 4
Explanation
When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY, with lengths 3,3,4, and 3. The longest name is PQRS, but there are 3 options for the shortest-named city. Choose ABC, because it comes first alphabetically.
Solution:
select CITY,LENGTH(CITY) from STATION order by Length(CITY) asc, CITY limit 1;
select CITY,LENGTH(CITY) from STATION order by Length(CITY) desc, CITY limit 1;
11. 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.
Input Format
The STATION table is described as follows:
Solution:
SELECT CITY FROM STATION where SUBSTR(CITY,1,1) IN('A','E','I','O','U');
12. 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.
Input Format
The STATION table is described as follows:
Solution:
SELECT distinct(CITY) FROM STATION where city like '%[aeiou]' order by 1;
13. Weather Observation Station 8
Query the list of CITY names from STATION that have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Solution:
SELECT CITY FROM STATION where city like '[aeiou]%[aeiou]';
14. Weather Observation Station 9
Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Solution:
SELECT distinct(CITY) FROM STATION where city not like '[aeiou]%';
15. Weather Observation Station 10
Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Solution:
SELECT distinct(CITY) FROM STATION where city not like '%[aeiou]';
16. 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.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Solution:
SELECT distinct(CITY) FROM STATION where city not like '[aeiou]%[aeiou]';
17. 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.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Solution:
select distinct(city) from station where city not like '[aeiou]%' and city not like '%[aeiou]'
18. Higher Than 75 Marks
Query the Name of any student in STUDENTS who scored higher than 75 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
Sample Output
Ashley
Julia
Belvet
Explanation
Only Ashley, Julia, and Belvet have Marks > 75. If you look at the last three characters of each of their names, there are no duplicates and ‘ley’ < ‘lia’ < ‘vet’.
Solution:
select name from students where marks>75 order by substr(name,-3,3);
19. Employee Names
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, the 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
Sample Output
Angela
Bonnie
Frank
Joe
Kimberly
Lisa
Michael
Patrick
Rose
Todd
Solution:
select name from employee order by 1;
20. Employee Salaries
Write a query that prints a list of employee names (i.e.: the name attribute) for employees in Employee having a salary greater than $2000 per month who have been employees for less than 10 months. Sort your result by ascending employee_id.
Input Format
The Employee table containing employee data for a company is described as follows:
where employee_id is an employee’s ID number, the 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
Sample Output
Angela
Michael
Todd
Joe
Explanation
Angela has been an employee for months and earns $3443 per month.
Michael has been an employee for months and earns $2017 per month.
Todd has been an employee for months and earns $3396 per month.
Joe has been an employee for months and earns $3573 per month.
We order our output by ascending employee_id.
Solution:
select name from employee where salary>2000 and months<10 order by employee_id;
Hope this was helpful!
Do let me know if any code could be optimized or written better. Let’s learn and grow together.