Skip to content

Commit

Permalink
solve: Advanced-Select - #1,#4,#5 + Aggregation - #1-#8
Browse files Browse the repository at this point in the history
  • Loading branch information
faizanxmulla committed Feb 11, 2024
1 parent 8fbe434 commit 1763ce5
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 0 deletions.
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 interviewbit-solutions/02 - Advanced Select/4-role-counter.sql
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)

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.
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'
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'
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
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
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
25 changes: 25 additions & 0 deletions interviewbit-solutions/03 - Aggregation/06-big-salary.sql
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 interviewbit-solutions/03 - Aggregation/07-5th-highest-mark.sql
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 interviewbit-solutions/03 - Aggregation/08-famous-books.sql
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

0 comments on commit 1763ce5

Please sign in to comment.