Sybase Clauses

Sybase Clauses: Clauses are used to grouping and sorting the data. Sybase clauses are group by, order by, having and compute.

Group By Clause:

  • Group by clause divides the output of the given query into groups.
  • We can group by one or more column names.
  • When group by is used with aggregate functions, group by clause retrieves the calculations in each subgroup and returns multiple rows.
  • We can use maximum 16 columns or expressions in a group by clause.
  • We can use where clause and group by clause in query.

Syntax:

select column1, column2, …. columnn
from table
group by column1

Example:

select account_type, account_id
from accounts
group by account_id
go

select pub_id, type, sum(total_sales)
from titles
group by pub_id, type
go

Having Clause:

  • Having clause is used to display or filter the rows defined by the group clause.
  • Having clause is used to set condition for group by clause.

Syntax:

select column1, cloumn2 from table_name
group by column1
having condition

Examples:

select deptno from emp
group by deptno
having count(*)>1
go

Order by clause:

  • Order by clause is used to sort the output.
  • Sort is either in ascending or descending order.

Syntax:

select column1, column2 from table_name
order by column1 [asc/desc]

Example:

select account_id, account_name, account_type from accounts
orderby account_id asc
go

Compute Clause: Compute clause is used with row aggregates to produce reports for subtotals and summaries.

Syntax:

compute row_aggregate(column_name1)
[, row_aggregate(column_nam1e)]…
[by column_name1 [, column_name1]…]

Powered by k2schools