-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
8fbe434
commit 1763ce5
Showing
11 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
interviewbit-solutions/02 - Advanced Select/1-conditional-work.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- Given a table NUMBERS, determine for each row, if the sum of the numbers are ‘Positive’, ‘Negative’, or ‘Zero’. | ||
-- Note: Output should be printed with column name A. | ||
|
||
SELECT CASE WHEN A+B+C > 0 THEN 'Positive' WHEN A+B+C < 0 THEN 'Negative' ELSE 'Zero' END AS A | ||
FROM NUMBERS | ||
|
||
|
||
-- REMARKS: | ||
-- didnt see input table properly. |
10 changes: 10 additions & 0 deletions
10
interviewbit-solutions/02 - Advanced Select/4-role-counter.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- Given a table GAMERS, query for the count of each Role of the Players in the given table. Sort the result based on ascending order of the count and print the result in the following format: | ||
|
||
-- [Role] Count is [Role_Count] | ||
|
||
|
||
SELECT CONCAT(Role, ' Count is ', COUNT(Role)) as COUNT | ||
FROM GAMERS | ||
GROUP BY Role | ||
ORDER BY COUNT(Role) | ||
|
8 changes: 8 additions & 0 deletions
8
interviewbit-solutions/02 - Advanced Select/5-vowel-country.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- Given a table PLACES, count the number of Countries which end with a vowel. | ||
|
||
SELECT COUNT(Country) | ||
FROM PLACES | ||
WHERE Country REGEXP '[aeiou]$' | ||
|
||
|
||
-- similar to hackerrank --> Basic select --> last 7-8 problems, all based on REGEX. |
6 changes: 6 additions & 0 deletions
6
interviewbit-solutions/03 - Aggregation/01-firefighters-average.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- Given a table FIREFIGHTERS, query the average of all the people saved by the Firefighters whose CountryCode is PM. | ||
|
||
|
||
SELECT AVG(PeopleSaved) | ||
FROM FIREFIGHTERS | ||
WHERE CountryCode='PM' |
5 changes: 5 additions & 0 deletions
5
interviewbit-solutions/03 - Aggregation/02-firefighters-sum.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Given a table FIREFIGHTERS, query the sum of all the people saved by the Firefighters whose CountryCode is PG. | ||
|
||
SELECT SUM(PeopleSaved) | ||
FROM FIREFIGHTERS | ||
WHERE CountryCode='PG' |
5 changes: 5 additions & 0 deletions
5
interviewbit-solutions/03 - Aggregation/03-largest-difference.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Given a table FIREFIGHTERS, find the largest difference between the number of people saved by 2 firefighters. | ||
|
||
|
||
SELECT MAX(a.PeopleSaved - b.PeopleSaved) as A | ||
FROM FIREFIGHTERS a, FIREFIGHTERS b |
9 changes: 9 additions & 0 deletions
9
interviewbit-solutions/03 - Aggregation/04-distance-maximizer.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- Given a table HOUSES, find the manhattan distance of the house which is the farthest from John’s house which lies at coordinates (20, 4). | ||
|
||
-- Basically you have to find the manhattan distance of some house which is the maximum amongst all the other houses from John’s house. | ||
|
||
-- Manhattan Distance between 2 points P(x1, y1), Q(x2, y2) is given as |x1 - x2| + |y1 - y2|. | ||
|
||
|
||
SELECT MAX(ABS(XCoordinate - 20) + ABS(YCoordinate - 4)) as A | ||
FROM HOUSES |
7 changes: 7 additions & 0 deletions
7
interviewbit-solutions/03 - Aggregation/05-euclids-distance.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- Given a table HOUSES, find the euclidean distance between points with the largest X and Y coordinate, and the points with the smallest X and Y coordinate. | ||
|
||
-- The Euclidean Distance between 2 points P(x1, y1) and Q(x2, y2) is defined as: sqrt((x1 - x2)2 + (y1 - y2)2). | ||
|
||
|
||
SELECT SQRT(POW(MAX(XCoordinate) - MIN(XCoordinate), 2) + POW(MAX(YCoordinate) - MIN(YCoordinate), 2)) as A | ||
FROM HOUSES |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- Given a table WORKERS, find how many workers have the maximum total earnings among all the WORKERS. | ||
|
||
-- The total earnings of a worker is calculated as Daily Wage * Number of Days Worked. | ||
|
||
|
||
-- Solution : | ||
|
||
SELECT Count(*) AS A | ||
FROM WORKERS | ||
WHERE (DailyWage * DaysWorked) = (SELECT Max(DailyWage * DaysWorked) | ||
FROM WORKERS) | ||
|
||
|
||
-- my approaches : | ||
|
||
SELECT MAX(DailyWage * DaysWorked) | ||
FROM ( | ||
SELECT COUNT(*) as A | ||
FROM WORKERS | ||
) x | ||
|
||
SELECT COUNT(*) as A | ||
FROM WORKERS | ||
ORDER BY (DailyWage*DaysWorked) DESC | ||
LIMIT 1 |
22 changes: 22 additions & 0 deletions
22
interviewbit-solutions/03 - Aggregation/07-5th-highest-mark.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- Given the ‘STUDENTS’ table. Write an SQL query to find the 5’th highest marks in the students table. | ||
|
||
SELECT MARKS | ||
FROM STUDENTS | ||
ORDER BY 1 DESC | ||
LIMIT 1 | ||
OFFSET 4 | ||
|
||
|
||
-- other approaches : | ||
|
||
SELECT MARKS | ||
FROM ( | ||
SELECT * , RANK() OVER(ORDER BY MARKS DESC) AS rank | ||
FROM STUDENTS | ||
) AS x | ||
WHERE rank=5 | ||
|
||
|
||
-- remarks : to remember --> asked many times in interview. | ||
|
||
|
16 changes: 16 additions & 0 deletions
16
interviewbit-solutions/03 - Aggregation/08-famous-books.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- You are given 2 tables, One containing the available Books and the other containing the Books that have been bought by a customer. You have to find the Id’s of all the ‘Famous’ Books. A book is called ‘Famous’ if it is bought by at least 3 customers. | ||
|
||
-- my approach : | ||
|
||
SELECT b.Id as Id | ||
FROM Books b JOIN BoughtBooks bb ON b.Id=bb.BooksId | ||
GROUP BY bb.BooksId | ||
HAVING COUNT(bb.Id) >= 3 | ||
|
||
|
||
-- more easy solution: no need for the first table. | ||
|
||
SELECT BooksId as Id | ||
FROM BoughtBooks | ||
GROUP BY 1 | ||
HAVING count(BooksId)>=3 |