Sql Hackerrank Flashcards
Write function RemoveExclamationMarks which removes all exclamation marks from a given string.
SELECT s, REPLACE(s, ‘!’, ‘’) AS res
FROM removeexclamationmarks
REPLACE(s, ‘!’, ‘’): This function replaces all occurrences of the exclamation mark ‘!’ in the string s with an empty string ‘’.
REPLACE is a built-in SQL function that takes three arguments:
The original string (s).
The substring you want to replace (‘!’).
The replacement substring (in this case, an empty string ‘’).
In your application, there is a section for adults only. You need to get a list of names and ages of users from the users table, who are 18 years old or older.
users table schema
name
age
SELECT name, age
FROM users
WHERE age >= 18
Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.
ID num
Name varchar2 (17)
countrycode varchar2
district varchar2
pop num
select *
from City
where CountryCode=’USA’ and Population>100000
Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA.
ID num
Name varchar2 (17)
countrycode varchar2
district varchar2
pop num
select NAME
from CITY
where countrycode=’USA’ and Population >120000
Query all columns (attributes) for every row in the CITY table.
ID num
Name varchar2 (17)
countrycode varchar2
district varchar2
pop num
select *
from city
Query all columns for a city in CITY with the ID 1661.
select *
from city
where Id=1661
Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.
select *
from city
where countrycode=”JPN”
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.Please append a semicolon “;” at the end of the query and enter your query in a single line to avoid error.
Select CITY, STATE from STATION ORDER BY CITY, STATE;
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;
or
SELECT DISTINCT city
FROM STATION
WHERE RIGHT(id, 1) IN (‘0’, ‘2’, ‘4’, ‘6’, ‘8’);
Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.
Select count(CITY) - count(DISTINCT CITY)
from station
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.
SELECT City, LENGTH(City)
FROM (SELECT City
FROM Station
ORDER BY LENGTH(City), City)
WHERE ROWNUM = 1;
SELECT City, LENGTH(City)
FROM (SELECT City
FROM Station
ORDER BY LENGTH(City) DESC, City)
WHERE ROWNUM = 1;
Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.
SELECT DISTINCT City FROM Station WHERE UPPER(SUBSTR(City, 1, 1)) IN (‘A’, ‘E’, ‘I’, ‘O’, ‘U’);
Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.
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’;
What are the most common aggregations?
MIN() - returns the smallest value within the selected column
MAX() - returns the largest value within the selected column
COUNT() - returns the number of rows in a set
SUM() - returns the total sum of a numerical column
AVG() - returns the average value of a numerical column