Sobes.tech
Senior

-- There are data with fields user_id, status, and created_at. -- Write an SQL query that counts successful requests of each user over the last 7 days and sorts the result by count in descending order. -- Successful status - OK CREATE TABLE user_requests ( id BIGSERIAL PRIMARY KEY, user_id BIGINT NOT NULL, status VARCHAR(50) NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL ); SELECT user_id, COUNT(*) AS successful_count FROM user_requests WHERE status = 'OK' AND created_at >= CURRENT_TIMESTAMP - INTERVAL '7 days' GROUP BY user_id ORDER BY successful_count DESC;