Sobes.tech
Junior — Middle

How can data be sorted in an SQL query?

sobes.tech AI

Answer from AI

The keyword ORDER BY is used to organize data sorting in an SQL query. It allows you to specify which columns to sort by and in what order (ascending or descending).

Example of ascending order:

SELECT * FROM employees ORDER BY last_name ASC;

Sorting in descending order:

SELECT * FROM employees ORDER BY hire_date DESC;

You can sort by multiple columns, for example:

SELECT * FROM employees ORDER BY department_id ASC, salary DESC;

In Java, when working with a database through JDBC or ORM (e.g., JPA), sorting is usually specified directly in the SQL query or through ORM API methods.

How can data be sorted in an SQL query? — Java - sobes.tech