Which operator performs pattern matching?

138    Asked by DavidEDWARDS in SQL Server , Asked on Jun 26, 2024

I am currently working as a database administrator for a particular company that stores a large number of customer records in an SQL database. Recently, the new company launched a new campaign that targeted customers whose e-mail addresses end with specific domains such as @example.com or @sample.org. I need to retrieve a list of these customers quickly and effectively. How can I construct an SQL query for finding all the customers whose e-mail addresses end with those specific domains? 

Answered by Colin Payne

To retrieve a list of customers whose e-mail address ends with @example.com or @sample.org by using SQL, you can use the LIKE operator for pattern matching. The LIKE operator would help you by allowing you to specify a pattern to match against a column value.

Here is the SQL query:-

SELECT *
FROM customers
WHERE email LIKE ‘%@example.com’ OR email LIKE ‘%@sample.org’;

Here is also a Java-based example given which would demonstrate a scenario where you need to filter a list of customers based on their email addresses by using pattern matching similar to the SQL LIKE operator. In this particular example we would use the regular expression to achieve the pattern-matching functionality:-

Import java.util.ArrayList;
Import java.util.List;
Import java.util.regex.Matcher;
Import java.util.regex.Pattern;
Public class CustomerEmailFilter {
    Public static void main(String[] args) {
        // Sample list of customers with email addresses
        List customers = new ArrayList<>();
        Customers.add(new Customer(“John Doe”, john.doe@example.com));
        Customers.add(new Customer(“Alice Smith”, alice.smith@sample.org));
        Customers.add(new Customer(“Bob Brown”, bob.brown@gmail.com));
        Customers.add(new Customer(“Jane Lee”, jane.lee@example.com));
        // Pattern to match email addresses ending with “@example.com”
        String pattern1 = .*@example.com;
        // Pattern to match email addresses ending with “@sample.org”
        String pattern2 = .*@sample.org;
        // Filter customers based on the patterns
        List filteredCustomers = filterCustomersByPattern(customers, pattern1, pattern2);
        // Display filtered customers
        System.out.println(“Customers with emails ending with ‘@example.com’ or ‘@sample.org’:”);
        For (Customer customer : filteredCustomers) {
            System.out.println(customer.getName() + “ – “ + customer.getEmail());
        }
    }
    /**
     * Filters a list of customers based on email patterns using regular expressions.
     *
     * @param customers The list of customers to filter
     * @param patterns The regular expression patterns to match email addresses
     * @return A filtered list of customers matching the patterns
     */
    Private static List filterCustomersByPattern(List customers, String… patterns) {
        List filteredCustomers = new ArrayList<>();
        For (Customer customer : customers) {
            For (String patternStr : patterns) {
                Pattern pattern = Pattern.compile(patternStr);
                Matcher matcher = pattern.matcher(customer.getEmail());
                If (matcher.matches()) {
                    filteredCustomers.add(customer);
                    break; // Move to the next customer once matched
                }
            }
        }
        Return filteredCustomers;
    }
    /**
     * Customer class representing a customer with name and email.
     */
    Static class Customer {
        Private String name;
        Private String email;
        Public Customer(String name, String email) {
            This.name = name;
            This.email = email;
        }
        Public String getName() {
            Return name;
        }
        Public String getEmail() {
            Return email;
        }
    }
}

Your Answer

Interviews

Parent Categories