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;
Success!
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;
Success!
Query all columns (attributes) for every row in the CITY table.
The CITY table is described as follows:
Solution:
SELECT *
FROM CITY;
Success!
Query all columns for a city in CITY with the ID 1661.
The CITY table is described as follows:
SELECT *
FROM CITY
WHERE ID = 1661;
Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.
The CITY table is described as follows:
SELECT *
FROM CITY
WHERE COUNTRYCODE = "JPN";
Success!
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:
SELECT Name
FROM CITY
WHERE CountryCode = "JPN";
Success!!
Query a list of CITY and STATE from the STATION table. The STATION table is described as follows:
Solution:
SELECT CITY,STATE
FROM STATION;
Success!
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:
SELECT DISTINCT CITY
FROM STATION
WHERE 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.
For example, if there are three records in the table with CITY values 'New York', 'New York', 'Bengalaru', there are 2 different city names: 'New York' and 'Bengalaru'. The query returns , because
Solution:
SELECT COUNT(CITY)-COUNT(DISTINCT CITY) "Difference"
FROM STATION;
Success!
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:
SELECT CITY,LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC,CITY
LIMIT 1;
SELECT CITY,LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY),CITY
LIMIT 1;
Success!!!
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:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP "^[aeiou]";
Success!!
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:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP "[aeiou]$";
Success!!
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.
Input Format
The STATION table is described as follows:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP "^[aeiou]"
AND CITY REGEXP "[aeiou]$";
Success!!
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:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT REGEXP "^[aeiou]";
Question:
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:
Solution:
SELECT DISTINCT CITY
FROM Station
WHERE CITY NOT REGEXP "[aeiou]$";
Success!!
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:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT REGEXP "^[aeiou]"
OR CITY NOT REGEXP "[aeiou]$";
OR
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');
Success!!
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:
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');
OR
SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT REGEXP "^[aeiou]"
AND CITY NOT REGEXP "[aeiou]$"
Success!!!
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:
SELECT Name
FROM STUDENTS
WHERE Marks > 75
ORDER BY RIGHT(Name,3), ID;
Success!!
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:
SELECT Name
FROM Employee
ORDER BY Name;
SUCCESS!!!\
Solution:
SELECT Name
FROM Employee
WHERE Salary > 2000 AND months < 10;
Solution:
SELECT
CASE
WHEN A+B <= C OR A+C <= B OR B+C <= A THEN "Not A Triangle"
WHEN A=B AND B=C AND C=A THEN "Equilateral"
WHEN A=B OR B=C OR C=A THEN "Isosceles"
ELSE "Scalene"
END AS "Triangle Type"
FROM TRIANGLES;
Link: https://www.hackerrank.com/challenges/the-pads/problem
Solution:
SELECT CONCAT(Name,"(",LEFT(Occupation,1),")")
FROM OCCUPATIONS
ORDER BY Name,LEFT(Occupation,1);
SELECT CONCAT("There are a total of ", COUNT(Occupation)," ",LOWER(Occupation),"s.")
FROM OCCUPATIONS
GROUP BY Occupation
ORDER BY COUNT(Occupation), Occupation;
Problem: https://www.hackerrank.com/challenges/the-company/problem
SELECT c.company_code,
c.founder,
COUNT(DISTINCT lm.lead_manager_code),
COUNT(DISTINCT sm.senior_manager_code),
COUNT(DISTINCT m.manager_code),
COUNT(DISTINCT e.employee_code)
FROM company c
JOIN lead_manager lm
ON lm.company_code = c.company_code
JOIN senior_manager sm
ON sm.lead_manager_code = lm.lead_manager_code
JOIN manager m
ON m.senior_manager_code = sm.senior_manager_code
JOIN employee e
ON e.manager_code = m.manager_code
GROUP BY c.company_code, c.founder
ORDER BY c.company_code;
Link: https://www.hackerrank.com/challenges/revising-aggregations-the-count-function
SELECT COUNT(ID) "Number of Cities"
FROM CITY
WHERE Population > 100000;
SELECT SUM(Population)
FROM CITY
WHERE District = "California";
Link: https://www.hackerrank.com/challenges/revising-aggregations-the-average-function/problem
SELECT AVG(Population)
FROM CITY
WHERE District = "California";
Success!