0% found this document useful (0 votes)
80 views4 pages

SQL Hacker Rank Questions

The document contains examples of SQL queries with explanations for selecting data from databases tables by attributes like city name, population size, IDs and more. It also includes examples of joining tables and using functions like COUNT, DISTINCT, CASE and REGEXP.

Uploaded by

Nguyễn An Nam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views4 pages

SQL Hacker Rank Questions

The document contains examples of SQL queries with explanations for selecting data from databases tables by attributes like city name, population size, IDs and more. It also includes examples of joining tables and using functions like COUNT, DISTINCT, CASE and REGEXP.

Uploaded by

Nguyễn An Nam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Basic select

Revising the Select Query I

select * from city where population>100000 and countrycode='USA'

Revising the Select Query II

select name from city where population >120000 and countrycode='USA'

Select All

select*from city

Select By ID

select * from city where id=1661

Japanese Cities' Attributes

select * from city where countrycode='JPN'

Japanese Cities' Names

select name from city where countrycode='JPN'

Weather Observation Station 1

select city, state from station

Weather Observation Station 3

select distinct city from station where id % 2=0

Weather Observation Station 4

SELECT COUNT(city) - COUNT(DISTINCT city)

FROM station;

Weather Observation Station 5

(select city,max(length(city)) from station group by 1 order by 2 desc limit 1)

union

(select city,min(length(city)) from station group by 1 order by 2,1 asc limit 1)

Weather Observation Station 6

select distinct city

from station

where city regexp '^[AEIOU]'

or
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%'

Weather Observation Station 7

SELECT DISTINCT CITY

FROM STATION

WHERE CITY REGEXP '[AEIOU]$'

Or

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';

Weather Observation Station 8

SELECT DISTINCT CITY

FROM STATION

WHERE CITY REGEXP '^[AEIOU].*[aeiou]$'

Weather Observation Station 9

select distinct city

from station

where city not regexp '^[AEIOU]'

Weather Observation Station 10

select distinct city

from station

where city not regexp '[AEIOU]$'

Weather Observation Station 11

SELECT DISTINCT CITY

FROM STATION

WHERE CITY not REGEXP '^[AEIOU].*[aeiou]$'

Weather Observation Station 12

SELECT DISTINCT CITY


FROM STATION

WHERE CITY NOT REGEXP '^[AEIOU]' AND CITY NOT REGEXP '[aeiou]$';

Higher Than 75 Marks

select name from students where marks>75 order by right(name,3),id asc

Employee Names

select name from employee order by 1

Employee Salaries

select name from employee where salary>2000 and months <10 order by employee_id

Basic Join

African Cities

select c.name from city c join country s on c.countrycode=s.code where s.continent='Africa'

Average Population of Each Continent

select s.continent,floor(avg(c.population)) as avg from city c join country s

on c.countrycode=s.code group by 1 order by 1 desc

The Report

select case when G.Grade>=8 then S.name else 'NULL' end as Name,

G.Grade, S.Marks from students s join grades g on s.marks between g.min_mark and g.max_mark order
by g.grade desc, case when G.Grade>=8 then S.name else s.marks end;

Top Competitors

SELECT HACKER_ID, NAME

FROM (SELECT H.HACKER_ID, H.NAME, COUNT(S.HACKER_ID) AS count_hacker_id FROM

HACKERS H JOIN SUBMISSIONS S ON H.HACKER_ID = S.HACKER_ID

JOIN CHALLENGES C ON S.CHALLENGE_ID = C.CHALLENGE_ID

JOIN DIFFICULTY D ON C.DIFFICULTY_LEVEL = D.DIFFICULTY_LEVEL

WHERE S.SCORE = D.SCORE GROUP BY H.HACKER_ID, H.NAME)s

WHERE count_hacker_id > 1

ORDER BY count_hacker_id DESC, HACKER_ID ASC;

Ollivander's Inventory

SELECT ID, AGE, COINS_NEEDED, POWER FROM (SELECT W.ID, WP.AGE, W.COINS_NEEDED, W.POWER
FROM WANDS W JOIN WANDS_PROPERTY WP ON W.CODE = WP.CODE WHERE WP.IS_EVIL = 0)S

WHERE COINS_NEEDED = (SELECT MIN(COINS_NEEDED) FROM WANDS A JOIN WANDS_PROPERTY B

ON A.CODE=B.CODE WHERE A.POWER = S.POWER AND B.AGE=S.AGE)

ORDER BY POWER DESC, AGE DESC;

You might also like