Question: i have the fowling code in my project:

```
 $title = "In this title we have the word GUN"
 $needed_words = array('War', 'Gun', 'Shooting');
 foreach($needed_words as $needed_word) {
     if (preg_match("/\b$needed_word\b/", $title)) {
     $the_word = "ECHO THE WORD THATS FIND INSIDE TITLE";
     }
 }

```

I want to check if `$title` contains one of 15 predefined words, 
for example lets say:
if `$title` contains words **"War, Gun, Shooting"** then i want to assign the word that is find to `$the_word`

Thanks in advance for your time!

Answer: 
[SQL Fiddle](http://sqlfiddle.com/#!4/07aad/10)

**Query 1**:

```
WITH password_counts AS (
  SELECT DISTINCT
         UserName,
         Password,
         COUNT( DISTINCT Password ) OVER ( PARTITION BY UserName ) AS number_of_passwords,
         COUNT( Password ) OVER ( PARTITION BY UserName, Password ) AS count_per_password
  FROM   SecurityHole
)
SELECT UserName,
       Password,
       count_per_password
FROM   password_counts
WHERE  number_of_passwords > 1
ORDER BY UserName, Password

```

**[Results](http://sqlfiddle.com/#!4/07aad/10/0)**:

```
| USERNAME | PASSWORD | COUNT_PER_PASSWORD |
|----------|----------|--------------------|
|     John |      cat |                  2 |
|     John |      dog |                  2 |
|     Rick |     boat |                  2 |
|     Rick |      mat |                  1 |

```
this should work: 

```
SELECT username,password,COUNT(*)
FROM t
WHERE username IN (SELECT username FROM t GROUP BY username HAVING COUNT(DISTINCT password>1))
GROUP by username,password

```

you can optimize it if you'll use with clause to prevent the table from being read twice.