week-4b aggregate functions

Upload: kenan

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Week-4b Aggregate Functions

    1/18

    SELECT MAX(SAL) FROM EMP;

    SELECT MIN(SAL) FROM EMP;

    SELECT AVG(SAL) FROM EMP;

    SELECT SUM(SAL) FROM EMP;

    SELECT COUNT(SAL) FROM EMP;

    AGGREGATE FUNCTIONS (SUM, AVG, MIN, MAX, COUNT)

    5000

    800

    2073.21

    29025

    14

    WEEK-4B

  • 8/9/2019 Week-4b Aggregate Functions

    2/18

    QUESTIONS

    1-Who has the lowest salary ?

    SELECT MIN(SAL) FROM EMP ; It gives the lowest salary, not owner

    SELECT ENAME,MIN(SAL) FROM EMP ; NOT CORRECT

    SELECT ENAME,SAL FROM EMP WHERE SAL=(SELECT MIN(SAL) FROM EMP)

    2-Who has the highest salary ?

    SELECT MAX(SAL) FROM EMP ; It gives the lowest salary, not owner

    SELECT ENAME,MAX(SAL) FROM EMP ; NOT CORRECTSELECT ENAME,SAL FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM EMP)

    3-How many employees have commissions?

    SELECT COUNT(COMM) FROM EMP;

    4-Calculate the total salary.

    SELECT SUM(SAL) FROM EMP;

  • 8/9/2019 Week-4b Aggregate Functions

    3/18

    5-Calculate the average of employers salary

    SELECT AVG(SAL) FROM EP;

    6- List employee whose salary is greater than the average salary.

    SELECT ENAME,SAL FROM EMP

    WHERE SAL>(SELECT AVG(SAL) FROM EMP);

    7-List employees whose salary is smaller than the average salary.

    SELECT ENAME,SAL FROM EMP

    WHERE SAL

  • 8/9/2019 Week-4b Aggregate Functions

    4/18

    Oftentimes, we're interested in summarizing our data to determine trends or produce

    top-level reports.

    For example, the manager may not be interested in a listing of all widget sales, but

    may simply want to know the number of widgets sold this month.Fortunately, SQL provides aggregate functions to assist with the summarization of

    large volumes of data.

    We'll look at functions that allow us to add and average data, count records meeting

    specific criteria and find the largest and smallest values in a table.

    AGGREGATE FUNCTIONS

    TABLE : ORDERS

    SUM AVG COUNT MIN MAX

    Table ORDERS contains records of widget sales.

  • 8/9/2019 Week-4b Aggregate Functions

    5/18

    SUM

    It is used within a SELECT statement and, returns the summation of a series of values.

    8-If you wanted to know the total number of widget sold, we could use the

    following query:

    SELECT SUM(QUANTITY) AS TOTAL FROM ORDERS ;

    Our result would appear as :

    TABLE : ORDERS

  • 8/9/2019 Week-4b Aggregate Functions

    6/18

    AVG

    The AVG (average) function works in a similar manner to provide the mathematical

    average of a series of values. Let's try a slightly more complicated task this time.

    9-We'd like to find out the average dollar amount of all orders placed on the North

    American continent.

    Note that we'll have to multiply the Quantity column by the UnitPrice column to compute

    the dollar amount of each order.

    Here's what our query will look like:

    SQL> SELECT AVG(UnitPrice*Quantity) AS AveragePrice FROM ORDERS ;

    And the Results :

    TABLE : ORDERS

    SELECT

    FirstName,Quantity*UnitPrice

    FROM orders ;

    AVERAGE

  • 8/9/2019 Week-4b Aggregate Functions

    7/18

    COUNT

    SQL provides the COUNT function to retrieve the number of records in a table that meet given

    criteria. We can use the COUNT(*) syntax alone to retrieve the number of rows in a table.

    Alternatively, a WHERE clause can be included to restrict the counting to specific records.

    10-For example, suppose You would like to know how many orders our company processed

    that requested over 100 widgets.

    Here's the SQL query:

    SELECT COUNT(*) AS Number of Large Orders FROM Orders WHERE Quantity > 100 ;

    TABLE : ORDERS

  • 8/9/2019 Week-4b Aggregate Functions

    8/18

    The COUNT function also allows for the use of the DISTINCT keyword and an expression to count

    the number of times a unique value for the expression appears in the target data.

    Similarly, the ALL keyword returns the total number of times the expression is satisfied, without

    worrying about unique values.

    11-For example, You would like a simple query that returned the number of unique continents

    in our orders database.

    First, let's take a look at the use of the ALL keyword:

    SELECT COUNT(ALL Continent) As Number of Continents FROM Orders ;

    And the result set:

    TABLE : ORDERS

  • 8/9/2019 Week-4b Aggregate Functions

    9/18

    Obviously, this is not the desired results. If you recall the contents of the ORDERS table from

    previous slide, all of our orders came from North America, Africa and Europe. Let's try the

    DISTINCT keyword instead:

    SQL> SELECT COUNT(DISTINCT Continent) As Number of Continents FROM Orders ;

    And the output:

    TABLE : ORDERS

  • 8/9/2019 Week-4b Aggregate Functions

    10/18

    MAX FUNCTION

    We'll look at the functionality SQL provides to locate the records containing the smallest and largest

    values for a given expression.

    The MAX() function returns the largest value in a given data series. We can provide the functionwith a field name to return the largest value for a given field in a table.

    12-Suppose you wanted to find the order in our database that produced the most

    revenue(incoming) for the company. We could use the following query to find the order with

    the largest total dollar value:

    SELECT MAX(Quantity * UnitPrice)As Largest Order FROM Orders ;

    Our results would look like this:

    select firstname,quantity*unitprice from orders ;

  • 8/9/2019 Week-4b Aggregate Functions

    11/18

    MIN FUNCTION

    The MINX() function returns the smallest value in a given data series. We can provide the function

    with a field name to return the smallest value for a given field in a table.

    13-Suppose you wanted to find the order in our database that produced the leastrevenue(incoming) for the company. We could use the following query to find the order with

    the smallest total dollar value:

    SELECT MIN(Quantity * UnitPrice)As Smallest Order FROM Orders ;

    Our results would look like this:

    SQL > SELECT FirstName,Quantity*UnitPrice FROM orders ;

  • 8/9/2019 Week-4b Aggregate Functions

    12/18

    ROWNUM

    ROWNUM returns a number indicating the order in which a row was selected from a table. The first

    row selected has a ROWNUM of 1, the second row has a ROWNUM of 2, and so on. If a SELECT

    statement includes an ORDER BY clause, ROWNUMs are assigned to the retrieved rows before

    the sort is done.

    You can use ROWNUM in the WHERE clause of a SELECT statement to limit the number of rows

    retrieved.

    SELECT A

    FROM

    NUMBERS

    SELECT A

    FROM

    NUMBERS

    ORDER BY A

    SELECT A

    FROM

    NUMBERS

    WHERE

    ROWNUM

  • 8/9/2019 Week-4b Aggregate Functions

    13/18

    CORRECT

    ROWNUM=1 (ONLY FOR 1) ROWNUM BETWEEN 1 AND 5 (ONYLY STARTS WITH 1)

    ROWNUM

  • 8/9/2019 Week-4b Aggregate Functions

    14/18

    SELECT

    NAME,SCORE

    FROM ENT;

    SELECT RANK()

    OVER(ORDER BY SCORE DESC)

    AS NO,

    NAME,SCORE

    FROM ENT;

    SELECT DENSE_RANK()

    OVER(ORDER BY SCORE DESC)

    AS NO ,

    NAME,SCORE

    FROM ENT;

    SELECT RANK()

    OVER(PARTITION BY CITY ORDER BY SCORE DESC) AS NO ,

    CITY,NAME,SCORE

    FROM ENT;

    RANK, DENSE_RANK: If you want to assign a sequential order or rank to any table, you may use RANK function.

  • 8/9/2019 Week-4b Aggregate Functions

    15/18

    SELECT RANK()OVER(PARTITION BY DEPTNO ORDER BY EMPNO)

    AS NO,

    DEPTNO,EMPNO,ENAME,SAL

    FROM EMP;

    SELECT RANK()

    OVER(ORDER BY EMPNO) AS NO,

    DEPTNO,EMPNO,ENAME,SAL

    FROM EMP;

    PARTITION BY

  • 8/9/2019 Week-4b Aggregate Functions

    16/18

    14-Find the most successful student (First on the ranking list by Score)

    select name,score from ent

    where score=(select max(score) from ent) ;

    15-Find the second successful student .

    SELECT * FROM

    (select rank() over(order by score desc) AS NO,NAME,SCORE from ENT)WHERE NO=2 ;

    ENT

  • 8/9/2019 Week-4b Aggregate Functions

    17/18

    TASKS

    1-Who has the lowest salary in EMP?

    2-Who has the highest salary in EMP ?

    3-How many employees has commission in EMP?4-How many employees have no commission in EMP?

    5-Calculate the total salary in EMP.

    6-Make a list with a column which ranked by line_numbers in AIRPLANES.(use Rank)

    7-Make a list with a column which ranked by line_numbers in AIRPLANES.(use dense_rank)

    8-Use rank function with partition to list table in AIRPLANES.

    9- List the first 3 flights on customer_id='DAL' , ordered by line_number.

    10-List the last flight if list is ordered by order_date.11-Make a list which ranked by city in ENT

    12-Make a list which ranked by score in each city.

    13-How many different departments are there in EMP table.

    14-How many different jobs are there in EMP table.

    15-Find the name of 8th successful student from the ENT table.

    16-Find the Name of Third successful student in Almaty from ENT table;

  • 8/9/2019 Week-4b Aggregate Functions

    18/18

    1)SELECT ENAME FROM EMP WHERE SAL=(SELECT MIN(SAL) FROM EMP);

    2)SELECT ENAME FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM EMP);

    3)SELECT COUNT(*) FROM EMP wHERE COMM IS NOT NULL AND COMM!=0;

    4)SELECT COUNT(*) FROM EMP wHERE COMM IS NULL OR COMM=0;

    5)SELECT SUM(SAL) FROM EMP;

    6)SELECT RANK() OVER(ORDER BY LINE_NUMBER) AS NO FROM AIRPLANES;

    7)SELECT DENSE_RANK() OVER(ORDER BY LINE_NUMBER) AS NO FROM AIRPLANES;

    8)SELECT RANK() OVER(PARTITION BY CUSTOMER_ID ORDER BY LINE_NUMBER) AS NO,CUSTOMER_ID FROM

    AIRPLANES;

    9)SELECT * FROM(SELECT RANK() OVER(ORDER BY LINE_NUMBER) AS NO,ORDER_DATE FROM AIRPLANES) WHEREROWNUM