SQL Hacker Rank
SQL Hacker Rank
Exclude duplicates
from the answer.
select distinct(id) from STATION where id % 2 = 0; // even
select distinct(id) from STATION where id % 2 != 0; // odd
-----------------------------------------------------------------------------------
------------------------
-Find the difference between the total number of CITY in the table and the number
of distinct CITY.
select (count(CITY) - count(distinct(CITY))) from STATION;
-----------------------------------------------------------------------------------
------------------------
-Query the list of CITY names starting with vowels from STATION. Your result cannot
contain duplicates.
SELECT DISTINCT(city) FROM station WHERE city REGEXP "^[aeiou].*$";
-----------------------------------------------------------------------------------
------------------------
-Query the list of CITY names NOT starting with vowels from STATION. Your result
cannot contain duplicates.
select distinct(city) from station where upper(substr(city, 1,1)) not in
('A','E','I','O','U');
-----------------------------------------------------------------------------------
------------------------
-Query the list of CITY names NOT Ending with vowels from STATION. Your result
cannot contain duplicates.
select distinct(city) from station where upper(substr(city, length(city),1)) not in
('A','E','I','O','U');
-----------------------------------------------------------------------------------
------------------------
-Query the list of CITY which have vowels as both their first and last characters.
no duplicates
SELECT DISTINCT(city) FROM station WHERE city REGEXP "^[aeiou].*[aeiou]$";
-----------------------------------------------------------------------------------
------------------------ OR
select distinct(city) from station where upper(substr(city, 1,1)) in
('A','E','I','O','U') and
upper(substr(city, length(city),1)) in ('A','E','I','O','U');
-----------------------------------------------------------------------------------
------------------------
Query the average population for all cities in CITY, rounded down to the nearest
integer.
-select round(avg(population)) from city;
-----------------------------------------------------------------------------------
------------------------
order by last 3 characters and if same, order by their ids:
select name from students where marks > 75 order by right(name, 3), id;
-----------------------------------------------------------------------------------
------------------------
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 A = C THEN 'Equilateral'
WHEN A = B or A = C or B = C THEN 'Isosceles'
WHEN A <> B and B <> C and A <> C THEN 'Scalene'
END tuple
FROM TRIANGLES;
-----------------------------------------------------------------------------------
------------------------
// decimal places
// can't use aggregate function after "where" so use sub query.
select round(LONG_W, 4) from station where LAT_N = (select max(LAT_N) from station
where LAT_N < 137.2345);
-----------------------------------------------------------------------------------
------------------------
-2 cities with the shortest and longest CITY names, If there is more than one
smallest or largest city, choose the one that comes first when ordered
alphabetically.
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;
-----------------------------------------------------------------------------------
------------------------
CEILING(3.46) => 4
FLOOR(3.46) => 3
ROUND(5.693893,2) => 5.69
-----------------------------------------------------------------------------------
------------------------
BLUNDER:
(select CEIL((avg(salary))-avg(replace(salary, '0', ''))) from employees)
-----------------------------------------------------------------------------------
------------------------
TOP EARNER or sub query to get one record:
select max(salary*months), count(salary*months) from employee where (salary*months)
= (select max(salary*months) from employee);
-----------------------------------------------------------------------------------
------------------------
EUCLEDIAN:
select round(sqrt((power(min(LAT_N)-max(LAT_N), 2) + power(min(LONG_W)-max(LONG_W),
2))), 4) from station;
-----------------------------------------------------------------------------------
------------------------
TRIANGLE:
set @i = 0;
select repeat('* ', @i := @i+1) from information_schema.tables where @i<20;
-----------------------------------------------------------------------------------
------------------------
Manhattan distance : |x1 - x2| + |y1 - y2|
select round(abs(min(LAT_N)-max(LAT_N)) + abs(min(LONG_W)-max(LONG_W)), 4) from
station;
-----------------------------------------------------------------------------------
------------------------
Error while table deletion or truncate due to foreign key:
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE table $table_name;
SET FOREIGN_KEY_CHECKS = 1;
-----------------------------------------------------------------------------------
------------------------
-----------------------------------------------------------------------------------
------------------------
-----------------------------------------------------------------------------------
------------------------
-----------------------------------------------------------------------------------
------------------------
-----------------------------------------------------------------------------------
------------------------
-----------------------------------------------------------------------------------
------------------------