Aggregate Queries in SQL

Aggregate Queries in Oracle

Aggregate Queries in Oracle work on multiple rows unlike normal functions which work on single rows.

There are 5 aggregate functions in SQL. They are:-

1)      Max
Max works on all data types for which ordering is defined.For numerical types value based ordering is done, for character types dictionary ordering is use, for Date & Time the instance that later is considered to be greater.

2)      Min --- Same as above

3)      Sum Totals the data. Only works for numerical types.

4)      Avg finds the average. Only works for numerical types.

5)      Count .Counts the number of records. Works for all data types.

 

We create the following table to illustrate the concepts.

create table Cricket_Scores (batsman varchar(100),InningsNo int,MatchType varchar(10),score int)

Varanasi Software Junction:Aggregate Queries in SQL

Varanasi Software Junction:Aggregate Queries in SQL

Finally we have the table with the following records.

Varanasi Software Junction:Aggregate Queries in SQL
We try the Queries one by one:-

Varanasi Software Junction:Aggregate Queries in SQL
Varanasi Software Junction:Aggregate Queries in SQL




Varanasi Software Junction:Aggregate Queries in SQL

Varanasi Software Junction:Aggregate Queries in SQL

Varanasi Software Junction:Aggregate Queries in SQL

Using a aggregate query with other fields.
Let us try the following query:-

Varanasi Software Junction:Aggregate Queries in SQL


In such cases we need to specify the group by option


Varanasi Software Junction:Aggregate Queries in SQL


No Oracle creates groups based on batsman and calculates max based on those groups separately.


Varanasi Software Junction:Aggregate Queries in SQL

This gives the batsman wise statistics.
Multiple group by clauses can be used.

Varanasi Software Junction:Aggregate Queries in SQL

The order of group by clauses will not affect the output.


Varanasi Software Junction:Aggregate Queries in SQL

It does affect the processing time. So, we should specify the groups such that sizes are reduced in the beginning.


Using the having clause.

The having clause is used for providing conditions based on aggregate queries.

Suppose we wish to find the player with max score greater than equal to 100.

Varanasi Software Junction:Aggregate Queries in SQL


A query containing where, group by,and having clauses.

Varanasi Software Junction:Aggregate Queries in SQL

Some questions:-

Find the highest scores in Test Matches and One Days

Varanasi Software Junction:Aggregate Queries in SQL


Find the highest score in the 1st innings of a batsman:-

Varanasi Software Junction:Aggregate Queries in SQL
Removing the group by on batsman we get.

Varanasi Software Junction:Aggregate Queries in SQL


Find the batsman with highest average in tests.

Varanasi Software Junction:Aggregate Queries in SQL

Assignments

  1. Create the following table
    1. Marksheet
      (rollno primary key, name, physics, chemistry, maths)
    2. Add the following data
      1, A,60,59,75
      2, B,43,24,70
      3, C,55,67,49
      4, A,61,49,47
    3. Find the following data
      1. Highest scores in all subjects
      2. Student with highest marks in physics
      3. Student with highest total marks
      4. Student with highest difference between physics and chemistry marks
      5. List of students with common names
  2. Make the following table
    1. receiptno   qty      sale_date      share        price
      
      
          1        25          2022-04-15  Tata         500
      
          2        30          2022-04-25  Tata         450
          3         5          2022-04-15  Bata         300
      
          4         6          2022-04-25  Bata         200
      
          5        25          2022-04-15  BMW       400
    2. Find the total number of purchases
    3. Find total sales
    4. Find names of shares(unique only)
    5. Find shares with multiple sales
    6. Find max and min price of each share



Contact us for software training, education or development

Varanasi Software Junction:Aggregate Queries in SQL









 

Post a Comment

0 Comments

Me