Hacker Rank
Hacker Rank
FROM table_name;
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
COLUMNS = ATTRIBUTE
The MOD() function returns the remainder of a number divided by another number.
MOD(x, y) x MOD y x %y
LENGTH(string)
SQL Server / MS Access Syntax:
MySQL Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
Oracle 12 Syntax:
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY; -- FETCH FIRST ROW ONLY;
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
Wildcard Characters
Symbol Description
SUBSTR()
REVERSE(string)
1) select * from city;
2) SELECT * FROM CITY WHERE POPULATION > 100000 AND COUNTRYCODE = 'USA';
3) SELECT NAME FROM CITY WHERE POPULATION > 120000 AND COUNTRYCODE =
'USA';
4) SELECT * FROM CITY WHERE ID = 1661;
5) SELECT * FROM CITY WHERE COUNTRYCODE = 'JPN';
6) SELECT NAME FROM CITY WHERE COUNTRYCODE = 'JPN';
7) SELECT CITY, STATE FROM STATION ;
8) SELECT DISTINCT CITY FROM STATION WHERE MOD(ID,2) = 0 ;
9) SELECT COUNT(CITY) - COUNT(DISTINCT CITY) FROM STATION ;
10) select city, length(city) from station order by length(city) DESC,city ASC LIMIT 1;
select city, length(city) from station order by length(city) asc ,city asc fetch first row
only;
11) 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%';
12) 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';
13) 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%') AND (CITY LIKE '%a' OR CITY LIKE
'%e' OR CITY LIKE '%i' OR CITY LIKE '%o' OR CITY LIKE '%u') order by city;
14) SELECT DISTINCT CITY FROM STATION WHERE upper(SUBSTR(CITY,1,1)) NOT IN
('A','E','I','O','U') AND lower(SUBSTR(CITY,1,1)) NOT IN ('a','e','i','o','u');
15) SELECT DISTINCT CITY FROM STATION WHERE UPPER(SUBSTR(CITY, LENGTH(CITY), 1))
NOT IN ('A','E','I','O','U') AND LOWER(SUBSTR(CITY, LENGTH(CITY),1)) NOT IN
('a','e','i','o','u');
16) SELECT DISTINCT CITY FROM STATION WHERE LOWER(SUBSTR(CITY,1,1)) NOT IN
('a','e','i','o','u') OR LOWER(SUBSTR(CITY, LENGTH(CITY),1)) NOT IN ('a','e','i','o','u');
17) SELECT DISTINCT CITY FROM STATION WHERE LOWER(SUBSTR(CITY,1,1)) NOT IN
('a','e','i','o','u') AND LOWER(SUBSTR(CITY,LENGTH(CITY),1)) NOT IN ('a','e','i','o','u');
18) SELECT NAME FROM STUDENTS WHERE MARKS > 75 ORDER BY SUBSTR(NAME,
LENGTH(NAME)-2, 3), ID;
19) SELECT NAME FROM EMPLOYEE ORDER BY NAME;
20) SELECT NAME FROM EMPLOYEE WHERE SALARY > 2000 AND MONTHS < 10 ORDER
BY EMPLOYEE_ID;
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
PIVOT rotates a table-valued expression by turning the unique values from one
column in the expression into multiple columns in the output. PIVOT also runs
aggregations where they're required on any remaining column values that are
wanted in the final output.
UNPIVOT carries out the opposite operation to PIVOT, by rotating columns of a table-
valued expression into column values.
ROW_NUMBER ( )
OVER ( [ PARTITION BY value_expression , ... [ n ] ] order_by_clause )
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either
left or right table
ELSE 'Scalene'
END
FROM TRIANGLES;
3) SELECT
FROM
SELECT
Name,
Occupation
FROM OCCUPATIONS
) AS SOURCE
GROUP BY RowNum;
4) select N,
if(P is null, 'Root', if((select count(*) from BST where P = B.N)> 0, 'Inner', 'Leaf'))
from BST as B
order by N;
ROUND(number, decimals)
Parameter Values
Parameter Description
The CEIL() function returns the smallest integer value that is bigger than or equal to a
number.
Parameter
The FLOOR() function returns the largest integer value that is smaller than or equal to
a number.
1) select count(name) from city where population > 100000;
2) select sum(population) from city where district = 'California';
3) select round(avg(population)) from city;
4) select sum(population) from city where countrycode = 'JPN';
5) SELECT MAX(POPULATION) - MIN(POPULATION) FROM CITY;
6) SELECT CEIL(AVG(salary) - AVG(REPLACE(salary, '0', ''))) FROM employees;
7) SELECT months*salary, COUNT(*) FROM employee GROUP BY months*salary ORDER
BY months*salary DESC LIMIT 1;
8) SELECT ROUND(SUM(LAT_N),2), ROUND(SUM(LONG_W),2) FROM STATION;
9) SELECT truncate(SUM(LAT_N), 4) FROM STATION WHERE LAT_N > 38.7880 AND
LAT_N < 137.2345 ;
10) SELECT ROUND(MAX(lat_n), 4) FROM station WHERE lat_n < 137.2345;
11) select round(long_w, 4) from station where (select max(lat_n) from station where
lat_n < 137.2345) = lat_n;
12) select round(min(lat_n), 4) from station where lat_n > 38.7780;
13) select round(long_w, 4) from station where (select min(lat_n) from station where
lat_n > 38.7780) = lat_n;
14) select round(max(LAT_N)-min(LAT_N) + max(LONG_W) - min(LONG_W), 4) from
station;
15) select round(sqrt(power(max(LAT_N)-min(LAT_N),2) + power(max(LONG_W) -
min(LONG_W),2)), 4) from station;
16) SELECT ROUND(AVG(lat_n), 4) AS median FROM ( SELECT lat_n, ROW_NUMBER()
OVER (ORDER BY lat_n) AS rn FROM station ) AS subq WHERE rn = (SELECT
CEIL((COUNT(rn)+1)/2) FROM station) OR rn = (SELECT FLOOR((COUNT(rn)+1)/2)
FROM station) ;
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
1) select sum(ci.population) from city ci join country co on co.code = ci.countrycode
where co.continent = 'Asia';
2) select (ci.name) from city ci join country co on co.code = ci.countrycode where
co.continent = 'Africa';
3) select (co.Continent), floor(avg(ci.population)) from city ci join country co on co.code
= ci.countrycode group by co.continent;
4) SELECT CASE WHEN Grades.Grade < 8 THEN 'NULL' ELSE Students.Name END,
Grades.Grade, Students.Marks FROM Students, Grades WHERE Students.Marks >=
Grades.Min_mark AND Students.Marks <= Grades.Max_mark ORDER BY
Grades.Grade DESC, Students.Name;
6) WITH a AS(
SELECT h.hacker_id, name, count(c.challenge_id) AS nums
FROM hackers h join challenges c ON h.hacker_id = c.hacker_id
GROUP BY h.hacker_id,h.name)
SELECT hacker_id, name, nums
FROM a
WHERE nums NOT IN (
SELECT nums FROM a WHERE nums < (
SELECT max(nums) FROM a
)
GROUP BY nums
HAVING count(nums) > 1 )
ORDER BY nums desc, hacker_id;
Parameter Description
interval Required. The time/date interval to add.
Can be one of the following values:
year, yyyy, yy = Year
quarter, qq, q = Quarter
month, mm, m = month
dayofyear, dy, y = Day of the year
day, dd, d = Day
week, ww, wk = Week
weekday, dw, w = Weekday
hour, hh = hour
minute, mi, n = Minute
second, ss, s = Second
millisecond, ms = Millisecond
Parameter Description
interval Required. The part to return. Can be one of
the following values:
year, yyyy, yy = Year
quarter, qq, q = Quarter
month, mm, m = month
dayofyear = Day of the year
day, dy, y = Day
week, ww, wk = Week
weekday, dw, w = Weekday
hour, hh = hour
minute, mi, n = Minute
second, ss, s = Second
millisecond, ms = Millisecond
date1, date2 Required. The two dates to calculate the
difference between
Pads the string on the right with the given characters until the total string length = length.
Starts at 1
Increments by 1 each time Oracle goes one step deeper in the hierarchy
https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/Hierarchical-
Queries.html#GUID-0118DF1D-B9A9-41EB-8556-C6E7D6A5A84E
1) SELECT RPAD('*', (21-LEVEL)*2, ' *') FROM DUAL CONNECT BY LEVEL <= 20;
2) SELECT RPAD('*', 2 * LEVEL - 1, ' *') FROM DUAL CONNECT BY LEVEL <= 20;
3) WITH RECURSIVE Numbers AS ( SELECT 2 AS L UNION SELECT L + 1 FROM Numbers
WHERE L < 1000 ) SELECT GROUP_CONCAT(L SEPARATOR '&') AS PrimeNumbers
FROM Numbers WHERE NOT EXISTS ( SELECT 1 FROM Numbers AS N2 WHERE N2.L >
1 AND N2.L < Numbers.L AND Numbers.L % N2.L = 0 ) AND L <= 1000;