Вы находитесь на странице: 1из 5

Practical No.

= 9

Aim – To retrieve data from the tables of the database using order by
and group by commands.

Order By : Ordering result table traversal


The rows in a table in the relational model have no order because
result of a select is itself a table, query results also have no order
never rely on the DBMS to produce results in any particular order, even
if it appears to work although a table has no order, SQL will allow us to
control the order in which we access the rows of the table using
ORDER BY .to use ORDER BY we specify the sort keys(s) that SQL
should use to order the result output.

Syntax
ORDER BY<sort specification> [{,<sort specification>}…]
<sort specification> - <key expression>[asc|desc][nulls first |nulls
last]

Example
SELECT name, price
From items
ORDER BY price asc;
Now we will apply this order by command on our database firstly we
apply this order by clause on the first table of our project that is the
account _master table in this we arrange our data in ascending order in
context to the balance attribute of the table.

Snapshot
Similarly we will use the command on the second table of the project
Which is the branch _master in this we have used order by clause on
the
Branch _city it will arrange the data in descending order
Group Aggregation Using Group By
Aggregate functions return a single piece of summary information
about an entire set of rows .what if we wanted the aggregates to all
groups in one query .SQL uses the GROUP BY clause to specify the
attributes that determine the grouping .

Example
SELECT storied, SUM (price)
FROM orders
GROUP BY storied;

Now we will use the group by command on our database to retrieve


the data. We will use the GROUP BY clause on the remaining tables of
our databases in costumer _master table we have applied the group by
clause on the fields costumers _name, street.
It has grouped the to attributes which we have specified with it.
Similarly we apply the group by clause on the loan_ master table of our
database .the grouping is done according to the columns loan_ number
as well as the amount.

Вам также может понравиться