Ask Claude about this

User Email Domain Analysis

Business Context

At Google, understanding the distribution of email providers among our users helps us optimize our authentication systems, email deliverability, and user communication strategies. This analysis provides valuable insights into which email domains are most prevalent in our user base, which can inform partnerships with email providers and help us detect potential security patterns or anomalies in user registrations.

Technical Requirements

The primary objective is to develop a PostgreSQL query for the following:

Task: Count the number of users registered with each email domain, sorted by count in descending order.

Key Analysis Parameters:

  • Extract the domain portion from each user's email address (e.g., for 'user@example.com', the domain is 'example.com').
  • Group users by their email domain.
  • Count the number of unique users for each domain.
  • Sort the results in descending order by the user count, then alphabetically by domain name for ties.
  • The output should include the email domain and the count of users for that domain.

Database Schema

-- Table: Users
CREATE TABLE Users (
user_id INT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
registration_date DATE
);

Sample Data for Users Table:

user_id username email registration_date
1 john_doe john.doe@gmail.com 2022-01-15
2 jane_smith jane.smith@yahoo.com 2022-02-20
3 bob_johnson bob.johnson@gmail.com 2022-03-10
4 alice_w alice.w@hotmail.com 2022-04-05
5 charlie_b charlie.brown@gmail.com 2022-05-12
6 david_lee david.lee@yahoo.com 2022-06-18
7 emily_c emily.c@outlook.com 2022-07-22
8 frank_g frank.g@gmail.com 2022-08-30
9 grace_h grace.h@aol.com 2022-09-02
10 henry_i henry.i@yahoo.com 2022-10-11
11 user_eleven contact@corporate-domain.co.uk 2022-11-15
12 another_user info@corporate-domain.co.uk 2022-12-01

Expected Output Format (based on sample data):

email_domain (VARCHAR) user_count (BIGINT)
gmail.com 4
yahoo.com 3
corporate-domain.co.uk 2
aol.com 1
hotmail.com 1
outlook.com 1

User Email Domain Distribution

EASY

Write a PostgreSQL query to count the number of users for each email domain. The results should list the email domain and the corresponding user count, sorted in descending order by count, then alphabetically by domain for ties.

Your Turn! What are your thoughts on this problem or alternative ways to solve it? Share your own SQL query attempts or insights in the comments below!

Nerchuko Academy · Free DS Interview Prep