TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2950 6.49K
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ: 
You have 2 minutes to solve this SQL query. 
Find the employee(s) who have worked on the highest number of distinct projects. 

Assume the table structure: employee_projects(employee_id, project_id)

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
    employee_id,
    total_projects
FROM (
    SELECT
        employee_id,
        COUNT(DISTINCT project_id) AS total_projects,
        DENSE_RANK() OVER (
            ORDER BY COUNT(DISTINCT project_id) DESC
        ) AS rnk
    FROM employee_projects
    GROUP BY employee_id
) ranked
WHERE rnk = 1;


๐Ÿ’ก Explanation: 
This query counts the number of unique projects each employee has worked on and identifies those with the highest count.

โ€ข COUNT(DISTINCT project_id) counts unique projects for each employee
โ€ข GROUP BY employee_id creates one record per employee
โ€ข DENSE_RANK() ranks employees based on the number of projects
โ€ข The outer query returns all employees tied for the highest number of projects

This question tests your understanding of: 
โœ… COUNT(DISTINCT) 
โœ… GROUP BY 
โœ… Window Functions DENSE_RANK 
โœ… Ranking Aggregated Results 

๐ŸŽฏ Expected Output Example 
Employee ID | Total Projects 
101 | 12 
205 | 12 

Both employees have worked on the highest number of distinct projects.

๐Ÿš€ Alternative Without Window Functions

SELECT
    employee_id,
    COUNT(DISTINCT project_id) AS total_projects
FROM employee_projects
GROUP BY employee_id
HAVING COUNT(DISTINCT project_id) = (
    SELECT MAX(project_count)
    FROM (
        SELECT
            COUNT(DISTINCT project_id) AS project_count
        FROM employee_projects
        GROUP BY employee_id
    ) t
);


This solution uses nested subqueries and MAX() instead of window functions.

๐Ÿš€ Tip for SQL Job Seekers: 
Many interview questions involve ranking aggregated results, such as: 
Highest number of projects, Most orders, Maximum sales, Highest attendance, Most logins 

Practice combining GROUP BY with window functions like DENSE_RANK() to solve these efficiently.

โค๏ธ React with โค๏ธ for more interview challenges!
  • โค 12
  • ๐Ÿ‘ 2
More from @sqlspecialist
  1. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  2. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
  3. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 5 Guys, let's continue our Data Analyst Interviewโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026๐Ÿ”Ÿ What is the difference between UNION and JOIN? Sample Answer: "JOIN combines columns frโ€ฆ
  6. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 4 Guys, let's continue our Data Analyst Interviewโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’