Ordering the Display of Tuples



Rating - 4/5
489 views

SQL offers the user some control over the order in which tuples in a relation
are displayed. The order by clause causes the tuples in the result of a query to
appear in sorted order. To list in alphabetic order all instructors in the Physics
department, we write:

select name
from instructor
where dept_name = ’Physics
order by name;

By default, the order by clause lists items in ascending order. To specify the
sort order, we may specify desc for descending order or asc for ascending order.
Furthermore, ordering can be performed on multiple attributes. Suppose that we
wish to list the entire instructor relation in descending order of salary. If several instructors have the same salary, we order them in ascending order by name. We express this query in SQL as follows:

select *
from instructor
order by salary desc, name asc;


Rating - 4/5
516 views

Advertisements