Question: I have below XML and my table name is table1

```
<Details >
  <RelatedDetails>   
    <Name>name 1</Name>   
    <Position>User</Position>    
    <Relationship>User</Relationship>
    <Salary>5000</Salary>
    <Type>Company</Type>
  </RelatedDetails>
  <RelatedDetails>   
    <Name>name 2</Name>   
    <Position>User</Position>    
    <Relationship>Owner</Relationship>
    <Salary>7000</Salary>
    <Type>Company</Type>
  </RelatedDetails>
  <RelatedDetails>   
    <Name>name 3</Name>   
    <Position>User</Position>    
    <Relationship>Director</Relationship>
    <Salary>2000</Salary>
    <Type>Company</Type>
  </RelatedDetails>
</Details>

```

I would like to get the value of 'name' where the Salary > 6000 in SQL Server.

Please suggest how this can be achieved?

Ideally i should get 'name 2' as output.

Answer: 
```
select x.n.value('(Name)[1]', 'nvarchar(50)')
from @xml.nodes('/Details/RelatedDetails[Salary[text() > 6000]]') as x(n);

```
Assuming you have your XML in a SQL Server variable:

```
DECLARE @Input XML = '<Details>
  <RelatedDetails>   
    <Name>name 1</Name>   
    <Position>User</Position>    
    <Relationship>User</Relationship>
    <Salary>5000</Salary>
    <Type>Company</Type>
  </RelatedDetails>
  <RelatedDetails>   
    <Name>name 2</Name>   
    <Position>User</Position>    
    <Relationship>Owner</Relationship>
    <Salary>7000</Salary>
    <Type>Company</Type>
  </RelatedDetails>
  <RelatedDetails>   
    <Name>name 3</Name>   
    <Position>User</Position>    
    <Relationship>Director</Relationship>
    <Salary>2000</Salary>
    <Type>Company</Type>
  </RelatedDetails>
</Details>'

```

You can easily use the built-in XQuery features to do this quite easily:

```
SELECT 
    XCol.value('(Name)[1]', 'varchar(25)')
FROM 
    @Input.nodes('/Details/RelatedDetails') AS XTbl(XCol)
WHERE
    XCol.value('(Salary)[1]', 'int') > 6000

```