FAANG SQL INTERVIEW Question | Return IDs who has the highest rating

 DROP TABLE IF EXISTS Friends


CREATE TABLE Friends (

    id INT,

    friend_id INT 

);



DROP TABLE IF EXISTS Ratings


CREATE TABLE Ratings (

    id INT PRIMARY KEY,

    rating INT

);




INSERT INTO Friends (id, friend_id)

VALUES

(1, 2),

(1, 3),

(2, 3),

(3, 4),

(4, 1),

(4, 2),

(5,NULL),

(6,NULL);



INSERT INTO Ratings (id, rating)

VALUES

(1, 85),

(2, 90),

(3, 75),

(4, 88),

(5, 82),

(6, 91)



SELECT * FROM Friends


SELECT * FROM Ratings


-- Retrieve all Ids of a person whose rating is greater than friend's id

-- If person does not have any friend, retrieve only their id only if rating greater than 85

Comments

Popular posts from this blog

50 Essential SQL Questions to Land Your Dream Job

How to find all the customers who placed orders on three consecutive days | SQL Scenario questions

ACCENTURE SQL INTERVIEW QUESTION | Change the ProductIDs