Hackerrank SQL
Hackerrank SQL
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:
2. Generate the following two result sets:
1.Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each
profession as a parenthetical (i.e.: enclosed in parentheses). For
example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
2.Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and
output them in the following format:
There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is
the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be
ordered alphabetically.
Note: There will be at least two entries in the table for each type of occupation.
Input Format
The OCCUPATIONS table is described as follows:
Explanation
The results of the first query are formatted to the problem description's specifications.
The results of the second query are ascendingly ordered first by number of names corresponding to each profession
(2<=2<=3<=3), and then alphabetically by profession (doctor<= singer, and actor<=professor).
3. Employees With Missing Information
Table: Employees
+-------------------------+-----------+
| Column Name | Type |
+-------------------------+-----------+
| employee_id | int |
| name | varchar |
+-------------------------+-----------+
employee_id is the primary key for this table.
Each row of this table indicates the name of the employee whose ID is employee_id.
Table: Salaries
+---------------------+---------+
| Column Name | Type |
+---------------------+---------+
| employee_id | int |
| salary | int |
+---------------------+---------+
employee_id is the primary key for this table.
Each row of this table indicates the salary of the employee whose ID is employee_id.
Write an SQL query to report the IDs of all the employees with missing information. The information of an employee is missing
if:
The employee's name is missing, or
The employee's salary is missing.
Return the result table ordered by employee_id in ascending order.
The query result format is in the following example.
Example 1:
Input:
Employees table:
+-------------------+----------+
Output:
| employee_id | name |
+-------------------+
+------------------+-----------+
| employee_id |
|2 | Crew |
+------------------+
|4 | Haven |
|1 |
|5 | Kristian |
|2 |
+------------------+-----------+
+------------------+
Salaries table:
+-------------------+---------+
Explanation:
| employee_id | salary |
Employees 1, 2, 4, and 5 are working at this company.
+-------------------+----------+
The name of employee 1 is missing.
|5 | 76071 |
The salary of employee 2 is missing.
|1 | 22517 |
|4 | 63539 |
+-------------------+----------+
4. Table: Logins
+-----------------------+-------------+
| Column Name | Type |
+-----------------------+-------------+
| user_id | int |
| time_stamp | datetime |
+-----------------------+-------------+
(user_id, time_stamp) is the primary key for this table.
Each row contains information about the login time for the user with ID user_id.
Write an SQL query to report the latest login for all users in the year 2020. Do not include the users who did not login
in 2020.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input:
Logins table:
+-----------+------------------------------+
| user_id | time_stamp |
+-----------+------------------------------+ Explanation:
| 6 | 2020-06-30 15:06:07 | User 6 logged into their account 3 times but only once in 2020, so
| 6 | 2021-04-21 14:06:06 | we include this login in the result table.
| 6 | 2019-03-07 00:18:15 | User 8 logged into their account 2 times in 2020, once in
| 8 | 2020-02-01 05:10:53 | February and once in December. We include only the latest one
| 8 | 2020-12-30 00:46:50 | (December) in the result table.
| 2 | 2020-01-16 02:49:50 | User 2 logged into their account 2 times but only once in 2020, so
| 2 | 2019-08-25 07:59:08 | we include this login in the result table.
| 14 | 2019-07-14 09:00:00 | User 14 did not login in 2020, so we do not include them in the
| 14 | 2021-01-06 11:59:59 | result table.
+-----------+------------------------------+
Output:
+----------------+---------------------+
| user_id | last_stamp |
+----------------+---------------------+
|6 | 2020-06-30 15:06:07 |
|8 | 2020-12-30 00:46:50 |
|2 | 2020-01-16 02:49:50 |
+-------+------------------------------+
5. Customer Who Visited but Did Not Make Any
Transactions
Table: Visits
+--------------------+-----------+
| Column Name | Type |
+--------------------+-----------+
| visit_id | int |
| customer_id | int |
+--------------------+-----------+
visit_id is the primary key for this table.
This table contains information about the customers who visited the mall.
Table: Transactions
+-----------------------+---------+
| Column Name | Type |
+-----------------------+---------+
| transaction_id | int |
| visit_id | int |
| amount | int |
+-----------------------+---------+
transaction_id is the primary key for this table.This table contains information about the transactions made during the visit_id.
Write a SQL query to find the IDs of the users who visited without making any transactions and the number of times they made
these types of visits.
Return the result table sorted in any order. The query result format is in the following example.
Example 1:
Input:
Visits
Output:
+----------+-------------+
+-------------+----------------+
| visit_id | customer_id |
| customer_id | count_no_trans |
+----------+-------------+
+-------------+----------------+
|1 | 23 |
| 54 |2 |
|2 |9 |
| 30 |1 |
|4 | 30 |
| 96 |1 |
|5 | 54 |
+-------------+----------------+
|6 | 96 |
|7 | 54 |
|8 | 54 | Explanation:
+----------+-------------+ Customer with id = 23 visited the mall once and made one
Transactions transaction during the visit with id = 12.
+----------------+----------+--------+ Customer with id = 9 visited the mall once and made one
| transaction_id | visit_id | amount | transaction during the visit with id = 13.
+----------------+----------+--------+ Customer with id = 30 visited the mall once and did not make
|2 |5 | 310 | any transactions.
|3 |5 | 300 | Customer with id = 54 visited the mall three times. During 2
|9 |5 | 200 | visits they did not make any transactions, and during one visit
| 12 |1 | 910 | they made 3 transactions.
| 13 |2 | 970 | Customer with id = 96 visited the mall once and did not make
+----------------+----------+--------+ any transactions.
As we can see, users with IDs 30 and 96 visited the mall one
time without making any transactions. Also, user 54 visited the
mall twice and did not make any transactions.
6. Daily Leads and Partners
Table: DailySales
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| date_id | date |
| make_name | varchar |
| lead_id | int |
| partner_id | int |
+-------------+---------+
This table does not have a primary key.
This table contains the date and the name of the product sold and the IDs of the lead and partner it was sold to.
The name consists of only lowercase English letters.
Write an SQL query that will, for each date_id and make_name, return the number of distinct lead_id's and distinct
partner_id's.
Input:
DailySales table:
+-----------+-----------+---------+------------+
| date_id | make_name | lead_id | partner_id |
+-----------+-----------+---------+------------+
| 2020-12-8 | toyota | 0 |1 |
| 2020-12-8 | toyota | 1 |0 |
| 2020-12-8 | toyota | 1 |2 |
| 2020-12-7 | toyota | 0 |2 |
| 2020-12-7 | toyota | 0 |1 |
| 2020-12-8 | honda | 1 |2 | Explanation:
| 2020-12-8 | honda | 2 |1 | For 2020-12-8, toyota gets leads = [0, 1] and partners = [0, 1, 2]
| 2020-12-7 | honda | 0 |1 | while honda gets leads = [1, 2] and partners = [1, 2].
| 2020-12-7 | honda | 1 |2 | For 2020-12-7, toyota gets leads = [0] and partners = [1, 2]
| 2020-12-7 | honda | 2 |1 | while honda gets leads = [0, 1, 2] and partners = [1, 2].
+-----------+-----------+---------+------------+
Output:
+-----------+-----------+--------------+-----------------+
| date_id | make_name | unique_leads | unique_partners |
+-----------+-----------+--------------+-----------------+
| 2020-12-8 | toyota | 2 |3 |
| 2020-12-7 | toyota | 1 |2 |
| 2020-12-8 | honda | 2 |2 |
| 2020-12-7 | honda | 3 |2 |
+-----------+-----------+--------------+-----------------+
7. Patients With a Condition
Table: Patients
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| patient_id | int |
| patient_name | varchar |
| conditions | varchar |
+--------------+---------+
patient_id is the primary key for this table.
'conditions' contains 0 or more code separated by spaces.
This table contains information of the patients in the hospital.
Write an SQL query to report the patient_id, patient_name and conditions of the patients who have Type
I Diabetes. Type I Diabetes always starts with DIAB1 prefix.
Input:
Patients table:
+------------+--------------+--------------+
| patient_id | patient_name | conditions |
+------------+--------------+--------------+
|1 | Daniel | YFEV COUGH |
|2 | Alice | |
|3 | Bob | DIAB100 MYOP |
|4 | George | ACNE DIAB100 |
|5 | Alain | DIAB201 |
+------------+--------------+--------------+
Output:
+------------+--------------+--------------+
| patient_id | patient_name | conditions |
+------------+--------------+--------------+
|3 | Bob | DIAB100 MYOP |
|4 | George | ACNE DIAB100 |
+------------+--------------+--------------+
Explanation: Bob and George both have a condition that starts with DIAB1.
8. Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output
one of the following statements for each record in the table:
•Equilateral: It's a triangle with sides of equal length.
•Isosceles: It's a triangle with sides of equal length.
•Scalene: It's a triangle with sides of differing lengths.
•Not A Triangle: The given values of A, B, and C don't form a triangle.
Input Format
The TRIANGLES table is described as follows:
Each row in the table denotes the lengths of each of a triangle's three sides.
Sample Input
Sample Output
Isosceles
Equilateral
Scalene
Not A Triangle
9. Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to
print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your
output in descending order by the total number of challenges in which the hacker earned a full score. If more than one
hacker received full scores in same number of challenges, then sort them by ascending hacker_id.
Input Format
The following tables contain contest data:
•Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
Difficulty: The difficult_level is the level of difficulty of the challenge, and score is the score of the
challenge for the difficulty level.
Difficulty Table:
Sample Input
Hackers Table:
Challenges Table:
Submissions Table:
Sample Output
90411 Joe
10. You are given two tables: Students and Grades. Students contains three
columns ID, Name and Marks.
Sample Output
Maria 10 99
Jane 9 81
Julia 9 88 So, the following students
Scarlet 8 78 got 8, 9 or 10 grades:
NULL 7 63 •Maria (grade 10)
NULL 7 68 •Jane (grade 9)
•Julia (grade 9)
Note •Scarlet (grade 8)
Print "NULL" as the name if the grade is less than 8.