XQuery versus SQL/XML, Part Deux
July 25, 2008
Some of you have been asking for more information about XQuery versus SQL/XML. In particular, it appears that you are interested in understanding the levels of support for common operations. I’ll take a few moments to compare both XQuery and SQL/XML in IBM DB2 9. However, please note that not all vendors provide the same levels of support. For instance, all vendors do not support sub-document update, and those that do support it may not implement the XQuery standard. So please, before making any decisions, verify the levels of support provided by your vendor.
| Operation | XQuery | SQL/XML | Comments |
| Inserting an XML document | No | Yes | You use SQL to insert an entire XML document. |
| Retrieving an XML document | Yes | Yes | |
| Retrieving part of an XML document | Yes | Yes | |
| Using predicates with relational data | Yes | Yes | XQuery does not support relational predicates. However, IBM DB2 supports SQL in XQuery, allowing predicates with relational data. |
| Using predicates with XML data | Yes | Yes | |
| Deleting an XML document | No | Yes | You use SQL to delete an entire XML document. |
| Updating an XML document | Yes | Yes | |
| Updating part of an XML document | Yes | Yes | |
| Joining XML data | Yes | Yes | Using XQuery is the easier approach. Using SQL/XML is typically difficult to code. |
| Joining XML wth relational data | Yes | Yes | XQuery does not support joins to relational data. However, IBM DB2 supports SQL in XQuery, allowing joins to relational data. |
| Transforming XML | Yes | Yes | Using XQuery is the easier approach. Using SQL/XML is typically difficult to code. |
| Aggregating XML data | Yes | Yes | Using SQL/XML is the easier approach. Using XQuery is possible with embedded SQL, but is typically difficult to code. |
| Calling external functions | No | Yes | |
| Passing parameter markers | No | Yes |
At first glance, it may appear that SQL/XML has more extensive support. However, this is in part because logically-speaking certain tasks do not belong in XQuery. Also note that some tasks are easier to code with XQuery. This ease of coding can make a significant difference in some environments.

