Monday, 27 July 2026

SPARQL ORDER BY: Sorting Employee Data

  

In previous examples, we learned how to retrieve data from an RDF graph and how to perform calculations using aggregation functions such as COUNT(), MIN(), MAX(), and AVG().

 

However, query results are not always returned in a meaningful order. Sometimes we want to see the highest-paid employees first, the newest hires first, or employees sorted alphabetically by name.

 

This is where the ORDER BY clause becomes useful.

 

In this lesson, we'll use our employee dataset to learn how to sort query results using SPARQL.

 

1. Why Do We Need ORDER BY?

Consider the following query:

PREFIX : <http://example.org/company/> 

SELECT ?name ?salary 
WHERE { 
  ?employee a :Employee ; 
            :name ?name ; 
            :salary ?salary . 
}

   

The query returns all employees and their salaries. However, RDF stores do not guarantee the order of results. One execution might return:

 

name

salary

David Miller 

95000

Emma Wilson

90000

James Taylor 

87000

John Smith 

180000

Michael Brown

135000

Sarah Johnson

125000

Sophia Davis 

85000

 

Another execution could return them in a completely different order. To control the order of the results, we use ORDER BY.

 

2. Sorting Employees by Salary (Lowest to Highest)

Suppose we want to see employees ordered by salary in ascending order.

 

PREFIX : <http://example.org/company/> 

SELECT ?name ?salary 
WHERE { 
  ?employee a :Employee ; 
            :name ?name ; 
            :salary ?salary . 
}
ORDER BY ?salary

   

Output

name

salary

Sophia Davis 

85000

James Taylor 

87000

Emma Wilson

90000

David Miller 

95000

Sarah Johnson

125000

Michael Brown

135000

John Smith 

180000

 

The smallest salary appears first and the largest salary appears last.

 

Default sorting order is ascending, if you want, you can even explicitly specify using ASC keyword.

 

ORDER BY ASC(?salary)

 

3. Sorting Employees by Salary (Highest to Lowest)

Often we want the opposite order. SPARQL provides the DESC() function for descending sorting.

PREFIX : <http://example.org/company/> 

SELECT ?name ?salary 
WHERE { 
  ?employee a :Employee ; 
            :name ?name ; 
            :salary ?salary . 
}
ORDER BY DESC(?salary)

   

Output

name

salary

John Smith 

180000

Michael Brown

135000

Sarah Johnson

125000

David Miller 

95000

Emma Wilson

90000

James Taylor 

87000

Sophia Davis 

85000

 

Now the highest-paid employees appear first.

 

4. Sort by mutliple fields

 

PREFIX : <http://example.org/company/> 

SELECT ?cityName ?empName 
WHERE { 
  ?employee a :Employee ; 
            :name ?empName ;
            :worksInCity ?city ;
            :joinedOn ?joiningDate .
  
  ?city :cityName ?cityName
}
ORDER BY DESC(?cityName) ASC(?empName)

   

Above query sort the employees by their cityName in descending order, and employee name in ascending order.

 

Output

cityName

empName

Hyderabad

James Taylor

Hyderabad

Sophia Davis

Chennai

David Miller

Chennai

Emma Wilson

Chennai

Sarah Johnson

Bangalore

John Smith

Bangalore

Michael Brown

 

5. Other Examples

Sorting Employees Alphabetically

 

PREFIX : <http://example.org/company/> 

SELECT ?name 
WHERE { 
  ?employee a :Employee ; 
            :name ?name .
}
ORDER BY ?name

   

Sorting by Joining Date

 

PREFIX : <http://example.org/company/> 

SELECT ?name ?joiningDate
WHERE { 
  ?employee a :Employee ; 
            :name ?name ;
            :joinedOn ?joiningDate .
}
ORDER BY ?joiningDate

   

In summary, the ORDER BY clause allows us to control how SPARQL query results are returned.

 

·      Ascending Order

·      ORDER BY ?variable

·      Descending Order

·      ORDER BY DESC(?variable)

·      Multiple Sort Keys

·      ORDER BY ?cityName ?name

 

Common use cases include:

 

·      Employees sorted by salary

·      Employees sorted by joining date

·      Alphabetical employee listings

·      Top earners in the company

·      Recent hires

·      Ranking departments by average salary

 

ORDER BY is often used together with FILTER, GROUP BY, and aggregation functions to transform raw RDF data into meaningful business reports.


Previous                                                    Next                                                    Home

No comments:

Post a Comment