Query Languages
04.11.2026
Extracting data from databases, XML documents, and JSON
XPath is also the basis for XSLT, one of the transformation languages discussed in the next lecture.
After this lecture, you should be able to:
Three languages, three data models
| Aspect | SQL | XPath | jq |
|---|---|---|---|
| Data model | Relational tables | XML node tree | JSON |
| Standard | ISO/IEC 9075 | W3C XPath | not standardized |
| Core concept | Rows and columns | Nodes and paths | Filters and pipes |
| Filtering | WHERE clause |
Predicates [...] |
select(...) |
| Aggregation | COUNT, SUM, AVG, … |
count(), sum(), … |
length, add, … |
| Integration | Data extraction, ETL, mapping | XSLT, XQuery, SQL/XML | API, log, and ETL pipelines |
All three languages are declarative and focus on what data to retrieve.
Structured Query Language
The relational model and SQL remain the most widely used technologies for managing data today.
Four functional categories
| Category | Purpose | Key Statements |
|---|---|---|
| DDL — Data Definition Language | Define and modify database structure | CREATE, ALTER, DROP |
| DQL — Data Query Language | Retrieve and query data | SELECT |
| DML — Data Manipulation Language | Insert, update, and delete data | INSERT, UPDATE, DELETE |
| DCL — Data Control Language | Control access and permissions | GRANT, REVOKE |
The categories are often summarized as define, query, manipulate, and control.
Data Definition Language
CREATE — create tables, indexes, views, and databasesALTER — modify existing objects, e.g. add a column or change a data typeDROP — remove objects such as tables or indexesCreating a table
id is the primary key — uniquely identifies each productname stores the product nameprice uses a fixed-point type suitable for monetary valuesstock stores the available quantityDDL defines the schema; it does not insert any data.
Data Query Language
SELECT statementRetrieves the name and price of all products with a price greater than 1000.
| Clause | Purpose |
|---|---|
SELECT |
Defines which columns to retrieve |
FROM |
Specifies the table(s) from which data is pulled |
WHERE |
Applies conditions to filter rows |
ORDER BY |
Sorts the result |
JOIN |
Combines data from multiple tables |
GROUP BY |
Groups rows for aggregation |
SELECTandFROMare mandatory; all other clauses are optional.
Data Manipulation Language
Data Control Language
GRANT — grants specific privileges to a user or roleREVOKE — removes specific privileges from a user or roleA standard that keeps evolving
| Version | Year | Key Improvements |
|---|---|---|
| SQL-86 | 1986 | First formal standard; basic querying and manipulation |
| SQL-92 | 1992 | Major expansion: data types, joins, set operations, transactions |
| SQL:2003 | 2003 | XML support, identity columns, MERGE |
| SQL:2016 | 2016 | JSON support, row pattern recognition, polymorphic table functions |
We discuss the versions highlighted in the lecture in more detail on the following slides.
Why SQL keeps evolving
SQL-1 — 1986
SELECT, INSERT, UPDATE, DELETEWHERE, FROMSQL-86 provided a good foundation, but lacked advanced features needed in enterprise systems.
SQL-2 — 1992
DATE, TIME, TIMESTAMPLEFT, RIGHT, FULL)UNION, EXCEPT, INTERSECTCOMMIT and ROLLBACKSUBSTRING, POSITION, TRIMSQL-92 was widely adopted and set the stage for more advanced SQL functionality.
XMLELEMENT (SQL:2003); XMLQUERY, XMLTABLE (SQL/XML:2006)GENERATED AS IDENTITYMERGE statement — combines INSERT, UPDATE, and DELETE
SQL:2003 brought XML into relational databases and simplified common enterprise tasks.
JSON_VALUEJSON_QUERYJSON_TABLEMATCH_RECOGNIZE for sequence matchingLISTAGG — new formatting and aggregation featuresSQL:2016 brought SQL into the modern age of semi-structured data.
XML Path Language
XPath is the basis for XSLT, which we discuss in the next lecture.
The XML document used throughout this section
<products>
<product id="1">
<name>Mountain Bike</name>
<price currency="USD">1200.00</price>
<stock>10</stock>
</product>
<product id="2">
<name>Road Bike</name>
<price currency="USD">900.00</price>
<stock>5</stock>
</product>
<product id="3">
<name>Helmet</name>
<price currency="USD">50.00</price>
<stock>100</stock>
</product>
<product id="4">
<name>Gloves</name>
<price currency="EUR">25.00</price>
<stock>50</stock>
</product>
</products>The XML document as a node tree
| Node Type | Example | Description |
|---|---|---|
| Root node | the document itself | topmost node of the XML document |
| Element nodes | <product> |
represent XML elements |
| Attribute nodes | id="1", currency="USD" |
represent attributes within elements |
| Text nodes | Mountain Bike |
text content inside elements |
Navigating the document
/// selects nodes anywhere in the document (all descendants)| Expression | Meaning |
|---|---|
/products/product |
all product elements that are children of products |
product/name |
name elements relative to the current product |
//name |
all name elements anywhere in the document |
/products//price |
all price descendants of products |
Filtering nodes
[...]| Predicate | Filters by | Result |
|---|---|---|
/products/product[1] |
position | first product element |
/products/product[@id='2'] |
attribute value | product with id="2" |
/products/product[price > 500] |
element content | products with price above 500 |
/products/product[price/@currency='EUR'] |
attribute value | products priced in EUR |
/products/product[stock < 10] |
element content | products with stock below 10 |
Relationships between nodes
child is the default axis and can be omitted| Axis | Selects | Example |
|---|---|---|
child |
all child nodes | /products/product/child::name ≡ /products/product/name |
parent |
the parent node | /products/product/name/parent::product |
descendant |
all descendants | /products/descendant::price |
attribute |
attributes of the current node | /products/product/price/attribute::currency |
Manipulating and evaluating data
| Function | Purpose | Example | Result |
|---|---|---|---|
text() |
returns the text content of a node | /products/product[1]/name/text() |
Mountain Bike |
contains() |
checks whether a string contains a substring | /products/product[contains(name, 'Bike')]/name |
Mountain Bike, Road Bike |
count() |
counts the nodes in a node set | count(/products/product) |
4 |
XPath also provides functions for string manipulation, arithmetic, and boolean expressions.
XPath:
Result: all four product elements
id |
name |
price |
stock |
|---|---|---|---|
| 1 | Mountain Bike | 1200.00 USD | 10 |
| 2 | Road Bike | 900.00 USD | 5 |
| 3 | Helmet | 50.00 USD | 100 |
| 4 | Gloves | 25.00 EUR | 50 |
XPath returns the selected nodes; the table shows their content for readability.
XPath:
Result:
[1] selects the first product element/name then selects its name childXPath:
Result: all price elements anywhere in the document
currency |
price |
|---|---|
| USD | 1200.00 |
| USD | 900.00 |
| USD | 50.00 |
| EUR | 25.00 |
// searches through all descendants of the context nodeXPath:
Result: all four product elements
id |
name |
price |
|---|---|---|
| 1 | Mountain Bike | 1200.00 USD |
| 2 | Road Bike | 900.00 USD |
| 3 | Helmet | 50.00 USD |
| 4 | Gloves | 25.00 EUR |
The predicate checks only whether the
currencyattribute exists — not its value. If apriceelement lackedcurrency, that product would be omitted.
XPath:
Result: the currency attribute of all price elements
//: //price/@currencyXPath:
Result: products 1 and 2
id |
name |
price |
stock |
|---|---|---|---|
| 1 | Mountain Bike | 1200.00 USD | 10 |
| 2 | Road Bike | 900.00 USD | 5 |
price elementXPath:
Result: product 4
id |
name |
price |
stock |
|---|---|---|---|
| 4 | Gloves | 25.00 EUR | 50 |
currency attributeXPath:
Result:
count() function returns the number of nodes in a node setXPath:
Result: product 2
id |
name |
price |
stock |
|---|---|---|---|
| 2 | Road Bike | 900.00 USD | 5 |
XPath:
Result:
contains() checks whether a string contains a substringproduct elements before /name selects their namesWriting XPath Expressions
In this exercise, you will write XPath expressions to query an XML document representing a list of products.
Work in pairs and compare your expressions.
product elements that are featuredStart with simple path expressions and add predicates where needed.
featured element exists
true or falsestock is less than 10Question 6 is about presence, not about the value of the element.
/ selects from the root; // searches anywhere in the documenttext(), contains(), and count() extend queriesQuerying and transforming JSON
jq is the most widely used command-line tool for JSON processing.
Same products, JSON data model
{
"products": [
{
"id": 1,
"name": "Mountain Bike",
"price": { "amount": 1200.0, "currency": "USD" },
"stock": 10
},
{
"id": 2,
"name": "Road Bike",
"price": { "amount": 900.0, "currency": "USD" },
"stock": 5
},
{
"id": 3,
"name": "Helmet",
"price": { "amount": 50.0, "currency": "USD" },
"stock": 100
},
{
"id": 4,
"name": "Gloves",
"price": { "amount": 25.0, "currency": "EUR" },
"stock": 50
}
]
}The nested
priceobject mirrors the XPathprice/@currencyattribute.
Filters, fields, and pipes
. is the identity filter.products — the products array.products[0] — first product.products[] — each product separately.products[].price.amount — chained field access.products[] | .name? suppresses errors, e.g. .products[].tags[]?Filtering, construction, functions, and output
select(condition) keeps inputs where the condition is true[...] collects a stream into an array{...} builds an object; {name} is shorthand for {name: .name}length, map, sort_by, group_by, add, contains, has-c compact, -r raw strings, @csv and @tsvTypical filters:
.products[] | select(.price.amount > 500) — products above 500[.products[] | select(...)] — matches collected into an array{name, price: .price.amount} — a new object.products | length — number of productsExamples 1–3
| # | Filter | Result |
|---|---|---|
| 1 | .products |
all four product objects |
| 2 | .products[0].name |
"Mountain Bike" |
| 3 | .products[].price.amount |
1200.00, 900.00, 50.00, 25.00 |
.[] emits each element separately[...] to collect values into an arrayResults are shown as produced by
jq -c
Examples 4–7
.products[] | select(.price | has("currency")) | .name.products[].price.currency"USD", "USD", "USD", "EUR"[.products[] | select(.price.amount > 500)].products[] | select(.price.currency == "EUR")
has("currency")checks presence, not the value. This mirrors XPathprice/@currency.
Examples 8–10
.products | length4.products[] | select(.stock < 10).products[] | select(.name | contains("Bike")) | .name
contains("Bike")is a case-sensitive substring test
Writing jq Filters
In this exercise, you will write jq filters to query a JSON document representing a list of products.
jq '<filter>' exercise.jsonThe JSON document mirrors the XML document from the XPath exercise.
{
"store": {
"products": [
{
"id": 1,
"name": "Mountain Bike",
"category": "Outdoor",
"price": { "amount": 1200.0, "currency": "USD" },
"stock": 10,
"featured": true
},
{
"id": 2,
"name": "Road Bike",
"category": "Outdoor",
"price": { "amount": 900.0, "currency": "USD" },
"stock": 5,
"featured": false
},
{
"id": 3,
"name": "Helmet",
"category": "Accessories",
"price": { "amount": 50.0, "currency": "USD" },
"stock": 100
},
{
"id": 4,
"name": "Gloves",
"category": "Accessories",
"price": { "amount": 25.0, "currency": "EUR" },
"stock": 50
},
{
"id": 5,
"name": "Electric Scooter",
"category": "Urban Mobility",
"price": { "amount": 850.0, "currency": "USD" },
"stock": 20
}
]
}
}featured key exists (regardless of whether its value is true or false)stock is less than 10select() filters values[...] and {...} reshape data into arrays and objectslength, sort_by, group_by, add, and contains cover common tasksselect(), and built-ins such as length and sort_byWhy are query languages important for systems integration?
When would you use SQL, XPath, or jq?
Systems Integration | Query Languages | Prof. C. Drumm