Question: I have two tables:

Customer table:

* id (Primary Key)
* email\_id
* role\_id
* individual\_id (Foreign key).

Individual table:

* individual\_id
* dob

I want to find all duplicate records whose `email_id`, `role_id` and `dob`are same.

I have created a query using simple join and group by clause as below: 

```
SELECT c.email_id, c.role_id, i.dob 
FROM CUSTOMER c   
JOIN INDIVIDUAL i on c.individual_id = i.individual_id
GROUP BY c.email_id, c.role_id, i.dob
Having count(*) >= 2

```

It is working fine on test database (SQL Server 2012), but I would like to know that does my query would have performance issue on production database(around 5 million) or is there a better way to do this?

Answer: 
Issue is, when you define variables without `var`, they become part of global scope.

### Your Code

```js
function score(string, pattern) {
  counter = 0
  for (i = 0; i < string.length; i++) {
    if (string.toUpperCase().substr(i, pattern.length) == pattern.toUpperCase())
      counter++
  }
  return counter
}

result = []

for (i = 0; i < 4; i++) {
  console.log("Before call: ", i)
  result[result.length] = score("Coooool!", "OO")
  console.log("After call: ", i)
}

console.log(result)
```

### Resolved Code

```js
function score(string, pattern) {
  var counter = 0
  for (var i = 0; i < string.length; i++) {
    if (string.toUpperCase().substr(i, pattern.length) == pattern.toUpperCase())
      counter++
  }
  return counter
}

var result = []

for (var i = 0; i < 4; i++) {
  result[result.length] = score("Coooool!", "OO")
}

console.log(result)
```

Also as rightly pointed out by [kevin kloet](https://stackoverflow.com/users/5547818/kevin-kloet), you should use `.push` instead of `result[result.length]`
You are indexing your array based on the length rather than the counter. So change it to `result[i]=...` like below. 

```
result=[]

for(i=0;i<web.length;i++){
    result[i]=score("Coooool!","OO")
}

```

You could also `push` to the array:

```
result=[]

for(i=0;i<web.length;i++){
    result.push(score("Coooool!","OO"))
}

```