Giter Club home page Giter Club logo

hackerrank-sql's Introduction

Hackerrank-SQL

▶️ 01. Revising the Select Query I

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:

image

Solution:

SELECT *
FROM CITY
WHERE CountryCode = "USA" AND Population > 100000; 

Success!

▶️ 02. Revising the Select Query II

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:

image

Solution:

SELECT NAME
FROM CITY
WHERE CountryCode = "USA" AND Population > 120000; 

Success!

▶️ 03. Select All

Query all columns (attributes) for every row in the CITY table.

The CITY table is described as follows:

image

Solution:

SELECT *
FROM CITY; 

Success!

▶️ 04. Select By ID

Query all columns for a city in CITY with the ID 1661.

The CITY table is described as follows:

image

SELECT *
FROM CITY 
WHERE ID = 1661;

▶️ 05. Japanese Cities' Attributes

Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.

The CITY table is described as follows:

image

SELECT *
FROM CITY
WHERE COUNTRYCODE = "JPN"; 

Success!

▶️ 06. Japanese Cities' Names

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:

image

SELECT Name
FROM CITY
WHERE CountryCode = "JPN";

Success!!

▶️ 07. Weather Observation Station 1

Query a list of CITY and STATE from the STATION table. The STATION table is described as follows:

image

Solution:

SELECT CITY,STATE
FROM STATION; 

Success!

▶️ 08. Weather Observation Station 3

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:

image

SELECT DISTINCT CITY
FROM STATION
WHERE ID%2=0;

▶️ 09. Weather Observation Station 4

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:

image

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!

▶️ 10. Weather Observation Station 5

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:

image

image

🔙 Faced Difficulties During Solving

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!!!

▶️ 11. Weather Observation Station 6

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:

image

SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP "^[aeiou]";

Success!!

▶️ 12. Weather Observation Station 7

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:

image

SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP "[aeiou]$";

Success!!

▶️ 13. Weather Observation Station 8

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:

image

SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP "^[aeiou]"
      AND CITY REGEXP "[aeiou]$";

Success!!

▶️ 14. Weather Observation Station 9

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:

image

SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT REGEXP "^[aeiou]";

▶️ 15. Weather Observation Station 10

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:

image

Solution:

SELECT DISTINCT CITY
FROM Station
WHERE CITY NOT REGEXP "[aeiou]$";

Success!!

▶️ 15. Weather Observation Station 11

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:

image

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!!

▶️ 16. Weather Observation Station 12

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:

image

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!!!

▶️ 17. Higher Than 75 Marks

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:

image

image

SELECT Name
FROM STUDENTS
WHERE Marks > 75
ORDER BY RIGHT(Name,3), ID;

Success!!

▶️ 18. Employee Names

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:

image

SELECT Name
FROM Employee
ORDER BY Name;

SUCCESS!!!\

▶️ 19. Employee Salaries

image

Solution:

SELECT Name
FROM Employee
WHERE Salary > 2000 AND months < 10; 

▶️ 💡 20. Type of Triangle

image

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; 

▶️ 💡 21. The PADS

image

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; 

▶️ 💡 22. New Companies

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;

▶️ 23. Revising Aggregations - The Count Function

Link: https://www.hackerrank.com/challenges/revising-aggregations-the-count-function

SELECT COUNT(ID) "Number of Cities"
FROM CITY
WHERE Population > 100000;

▶️ 24. Revising Aggregations - The Sum Function

SELECT SUM(Population)
FROM CITY
WHERE District = "California";

▶️ 25. Revising Aggregations - Averages

Link: https://www.hackerrank.com/challenges/revising-aggregations-the-average-function/problem

SELECT AVG(Population)
FROM CITY
WHERE District = "California";

Success!

hackerrank-sql's People

Contributors

zizanayub avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.