SlideShare uma empresa Scribd logo
1 de 4
SQL Simple Queries – Worksheet 3


                              SQL Queries - Basics
                                     Worksheet - 3
                                      SUMMARY QUERIES
1     What is a summary query?
      For certain queries we are not interested in finer details of each record in a table. Rather we
      want aggregate or summary information. For example, consider the following queries:
      a) How many salespeople have exceeded their yearly quota?
      b) What is the sixe of the average order for an office?
      c) How many salespeople are assigned to each office?
      d) What is the average mark of students in an exam?

      SQL supports these requests for summary data through column functions and the GROUP
      BY and HAVING clauses of the SELECT statement.

2     What are the COLUMN functions in SQL?
      SQL lets you summarize data from the database through a set of column functions. A SQL
      column function takes an entire column of data as its argument and produces a single data
      item that summarizes the column.

      For example, the AVG() column function takes a column of data and computes its average.

      Here is a query that uses the AVG() column function to compute the average value of two
      columns from the SALESREPS table:

      What are the average quota and average sales of our salespeople?

      SELECT AVG(QUOTA), AVG(SALES)
      FROM SALESREPS
      AVG(QUOTA)   AVG(SALES)

      The first column function in the query takes values in the QUOTA column and computes
      their average; the second one averages the values in the SALES column. The query
      produces a single row of query results summarizing the data in the SALESREPS table.

      SQL offers 6 different column functions. These are:

      1.   SUM()             – computes the total of a column
      2.   AVG()             – computes the average value in a column
      3.   MIN()             – finds the minimum value in a column
      4.   MAX()             – finds the maximum value in a column
      5.   COUNT()           – counts the number of values in a column
      6.   COUNT(*)          – counts row of query results

      The argument to a column function can be a simple column name or an expression.

      Example 1:
Prof. Mukesh N. Tekwani                                                                  Page 1 of 4
SQL Simple Queries – Worksheet 3

     What is the average quota performance of our salespeople?

     SELECT AVG (100 * (SALES/QUOTA))
     FROM SALESREPS

     Computing a column total

     The SUM() column function computes the sum of a column of data values. The data in the
     column must have a numeric type (integer, decimal, floating point, or money). The result of
     the SUM() function has the same basic data type as the data in the column, but the result
     may have a higher precision. For example, if you apply the SUM() function to a column of
     16-bit integers, it may produce a 32-bit integer as its result.

     Example 2:

     What are the total quotas and sales for all salespeople?

     SELECT SUM(QUOTA), SUM(SALES)
     FROM SALESREPS

     What is the total of the orders taken by Bill Adams?

     SELECT SUM(AMOUNT)
     FROM ORDERS, SALESREPS
     WHERE NAME = 'Bill Adams'
     AND REP = EMPL_NUM

     Computing an Average

     The AVG() column function computes the average of a column of data values. The data in
     the column must have a numeric type.

     Because the AVG() function adds the values in the column and then divides by the number
     of values, its result may have a different data type than that of the values in the column. For
     example, if you apply the AVG() function to a column of integers, the result will be either
     a decimal or a floating point number.

     Calculate the average price of products from manufacturer ACI.

     SELECT AVG(PRICE)
     FROM PRODUCTS
     WHERE MFR_ID = 'ACI'

     Calculate the average size of an order placed by Acme Mfg. (customer number 2103).

     SELECT AVG(AMOUNT)
     FROM ORDERS
     WHERE CUST = 2103

Page 2 of 4                                                           mukeshtekwani@hotmail.com
SQL Simple Queries – Worksheet 3

      Finding maximum and minimum values

      The MIN() and MAX() column functions find the smallest and largest values in a column,
      respectively. The data in the column can contain numeric, string, or date/time information.
      The result of the MIN() or MAX() function has exactly the same data type as the data in
      the column.


      What are the smallest and largest assigned quotas?
      SELECT MIN(QUOTA), MAX(QUOTA)
      FROM SALESREPS

      What is the earliest order date in the database?

      SELECT MIN(ORDER_DATE)
      FROM ORDERS

      What is the best sales performance of any salesperson?

      SELECT MAX(100 * (SALES/QUOTA))
      FROM SALESREPS
      MAX(100*(SALES/QUOTA))

      Counting Data Values
      The COUNT() column function counts the number of data values in a column. The data in
      the column can be of any type. The COUNT() function always returns an integer,
      regardless of the data type of the column.

      How many customers are there?
      SELECT COUNT(CUST_NUM)
      FROM CUSTOMERS


      How many salespeople are over quota?

      SELECT COUNT(NAME)
      FROM SALESREPS
      WHERE SALES > QUOTA

      Find the average order amount, total order amount, average order amount as a percentage
      of the customer's credit limit, and average order amount as a percentage of the
      salesperson's quota.

      SELECT AVG(AMOUNT), SUM(AMOUNT),
      (100 * AVG(AMOUNT/CREDIT_LIMIT)),
      (100 * AVG(AMOUNT/QUOTA))
      FROM ORDERS, CUSTOMERS, SALESREPS
      WHERE CUST = CUST_NUM
      AND REP = EMPL_NUM
Prof. Mukesh N. Tekwani                                                              Page 3 of 4
SQL Simple Queries – Worksheet 3


3    What are Subqueries?
     Subqueries are query statements inside of query statements. Like the order of operations in
     algebra, order of operations also come into play when you start to embed SQL commands
     inside of other SQL commands (subqueries).

     Example 1:
     Select only the most recent order(s) in the orders table.

     We use the MAX(). This function wraps around a table column and returns the current
     highest (max) value for the specified column. We use this function to return the current
     "highest", or most recent date value in the orders table.

     USE mydatabase;
     SELECT MAX(day_of_order)
     FROM orders

     Now we can throw this query into the WHERE clause of another SELECT query.

     USE mydatabase;
     SELECT *
     FROM orders
     WHERE day_of_order = ( SELECT MAX(day_of_order) FROM orders )

4    SELECT Case statement
     SQL CASE is a very unique conditional statement providing if/then/else logic for any
     ordinary SQL command, such as SELECT or UPDATE. It then provides when-then-else
     functionality (WHEN this condition is met THEN do_this).

     USE northwind

     SELECT productid,
           'Status' = CASE
             WHEN quantity > 0 THEN 'in stock'
             ELSE 'out of stock'
             END
     FROM "order details";

5    GROUP BY statement
     SQL GROUP BY aggregates (consolidates and calculates) column values into a single
     record value. GROUP BY requires a list of table columns on which to run the calculations.

     USE mydatabase
     SELECT customer
     FROM orders

     GROUP BY customer




Page 4 of 4                                                         mukeshtekwani@hotmail.com

Mais conteúdo relacionado

Mais procurados

SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbmsVignesh Saravanan
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answersMichael Belete
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive TechandMate
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptxSherinRappai
 
SQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question BankSQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question BankMd Mudassir
 
SQL Queries
SQL QueriesSQL Queries
SQL QueriesNilt1234
 
Spreadsheet text functions
Spreadsheet text functionsSpreadsheet text functions
Spreadsheet text functionsAnjan Mahanta
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sqlCharan Reddy
 
Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - DatabaseShahadat153031
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries InformationNishant Munjal
 
Sql database object
Sql database objectSql database object
Sql database objectHarry Potter
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsSalman Memon
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsAshwin Dinoriya
 

Mais procurados (20)

SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
Sql Tutorials
Sql TutorialsSql Tutorials
Sql Tutorials
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answers
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question BankSQL-RDBMS Queries and Question Bank
SQL-RDBMS Queries and Question Bank
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Spreadsheet text functions
Spreadsheet text functionsSpreadsheet text functions
Spreadsheet text functions
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - Database
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
Sql database object
Sql database objectSql database object
Sql database object
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
DBMS Notes: DDL DML DCL
DBMS Notes: DDL DML DCLDBMS Notes: DDL DML DCL
DBMS Notes: DDL DML DCL
 

Destaque

Applied 20S January 23, 2009
Applied 20S January 23, 2009Applied 20S January 23, 2009
Applied 20S January 23, 2009Darren Kuropatwa
 
Insediarsi in AREA Science Park
Insediarsi in AREA Science ParkInsediarsi in AREA Science Park
Insediarsi in AREA Science ParkAREA Science Park
 
Gallatin Thesis '15
Gallatin Thesis '15Gallatin Thesis '15
Gallatin Thesis '15Emma Behnke
 
The Future of Learning and Learning for the Future
The Future of Learning and Learning for the FutureThe Future of Learning and Learning for the Future
The Future of Learning and Learning for the FutureKim Flintoff
 
Huong dan dang ky
Huong dan dang kyHuong dan dang ky
Huong dan dang kyHavy Tran
 
Personal branding(with speaking notes)
Personal branding(with speaking notes)Personal branding(with speaking notes)
Personal branding(with speaking notes)Mani Nutulapati
 
03 424 private club
03 424 private club03 424 private club
03 424 private clubLin, Tony
 
Entorno gráfico word
Entorno gráfico wordEntorno gráfico word
Entorno gráfico wordzanethpineda
 
CETWorld- Mobile MediaCrossMedia
CETWorld- Mobile MediaCrossMediaCETWorld- Mobile MediaCrossMedia
CETWorld- Mobile MediaCrossMediaMatthew Snyder
 

Destaque (15)

From hear to there
From hear to thereFrom hear to there
From hear to there
 
Applied 20S January 23, 2009
Applied 20S January 23, 2009Applied 20S January 23, 2009
Applied 20S January 23, 2009
 
Insediarsi in AREA Science Park
Insediarsi in AREA Science ParkInsediarsi in AREA Science Park
Insediarsi in AREA Science Park
 
Gallatin Thesis '15
Gallatin Thesis '15Gallatin Thesis '15
Gallatin Thesis '15
 
Escuelas economicas
Escuelas economicasEscuelas economicas
Escuelas economicas
 
1
11
1
 
The Future of Learning and Learning for the Future
The Future of Learning and Learning for the FutureThe Future of Learning and Learning for the Future
The Future of Learning and Learning for the Future
 
Huong dan dang ky
Huong dan dang kyHuong dan dang ky
Huong dan dang ky
 
Personal branding(with speaking notes)
Personal branding(with speaking notes)Personal branding(with speaking notes)
Personal branding(with speaking notes)
 
03 424 private club
03 424 private club03 424 private club
03 424 private club
 
All about you
All about youAll about you
All about you
 
Entorno gráfico word
Entorno gráfico wordEntorno gráfico word
Entorno gráfico word
 
CETWorld- Mobile MediaCrossMedia
CETWorld- Mobile MediaCrossMediaCETWorld- Mobile MediaCrossMedia
CETWorld- Mobile MediaCrossMedia
 
Niña hechizera
Niña hechizeraNiña hechizera
Niña hechizera
 
Sosial læring 141118
Sosial læring 141118Sosial læring 141118
Sosial læring 141118
 

Semelhante a Sql wksht-3

Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdfKalyankumarVenkat1
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Finalmukesh24pandey
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptxSabrinaShanta2
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxArjayBalberan1
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence PortfolioChris Seebacher
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)Huda Alameen
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsZohar Elkayam
 
Data Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfData Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfhowto4ucontact
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQueryAbhishek590097
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functionsNitesh Singh
 
Simplifying SQL with CTE's and windowing functions
Simplifying SQL with CTE's and windowing functionsSimplifying SQL with CTE's and windowing functions
Simplifying SQL with CTE's and windowing functionsClayton Groom
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sqlpgolhar
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planMaria Colgan
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sqlMcNamaraChiwaye
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfDraguClaudiu
 
Intro to tsql unit 3
Intro to tsql   unit 3Intro to tsql   unit 3
Intro to tsql unit 3Syed Asrarali
 
Intro To TSQL - Unit 3
Intro To TSQL - Unit 3Intro To TSQL - Unit 3
Intro To TSQL - Unit 3iccma
 

Semelhante a Sql wksht-3 (20)

Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptx
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Data Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfData Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdf
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Simplifying SQL with CTE's and windowing functions
Simplifying SQL with CTE's and windowing functionsSimplifying SQL with CTE's and windowing functions
Simplifying SQL with CTE's and windowing functions
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sql
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_plan
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sql
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 
Intro to tsql unit 3
Intro to tsql   unit 3Intro to tsql   unit 3
Intro to tsql unit 3
 
Intro To TSQL - Unit 3
Intro To TSQL - Unit 3Intro To TSQL - Unit 3
Intro To TSQL - Unit 3
 

Mais de Mukesh Tekwani

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelMukesh Tekwani
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfMukesh Tekwani
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - PhysicsMukesh Tekwani
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion Mukesh Tekwani
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Mukesh Tekwani
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversionMukesh Tekwani
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion Mukesh Tekwani
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversionMukesh Tekwani
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Mukesh Tekwani
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prismMukesh Tekwani
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surfaceMukesh Tekwani
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomMukesh Tekwani
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesMukesh Tekwani
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEMukesh Tekwani
 

Mais de Mukesh Tekwani (20)

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube Channel
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdf
 
Circular motion
Circular motionCircular motion
Circular motion
 
Gravitation
GravitationGravitation
Gravitation
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - Physics
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversion
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion
 
What is Gray Code?
What is Gray Code? What is Gray Code?
What is Gray Code?
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversion
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prism
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surface
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrors
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atom
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lenses
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
 
Cyber Laws
Cyber LawsCyber Laws
Cyber Laws
 
XML
XMLXML
XML
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Último (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Sql wksht-3

  • 1. SQL Simple Queries – Worksheet 3 SQL Queries - Basics Worksheet - 3 SUMMARY QUERIES 1 What is a summary query? For certain queries we are not interested in finer details of each record in a table. Rather we want aggregate or summary information. For example, consider the following queries: a) How many salespeople have exceeded their yearly quota? b) What is the sixe of the average order for an office? c) How many salespeople are assigned to each office? d) What is the average mark of students in an exam? SQL supports these requests for summary data through column functions and the GROUP BY and HAVING clauses of the SELECT statement. 2 What are the COLUMN functions in SQL? SQL lets you summarize data from the database through a set of column functions. A SQL column function takes an entire column of data as its argument and produces a single data item that summarizes the column. For example, the AVG() column function takes a column of data and computes its average. Here is a query that uses the AVG() column function to compute the average value of two columns from the SALESREPS table: What are the average quota and average sales of our salespeople? SELECT AVG(QUOTA), AVG(SALES) FROM SALESREPS AVG(QUOTA) AVG(SALES) The first column function in the query takes values in the QUOTA column and computes their average; the second one averages the values in the SALES column. The query produces a single row of query results summarizing the data in the SALESREPS table. SQL offers 6 different column functions. These are: 1. SUM() – computes the total of a column 2. AVG() – computes the average value in a column 3. MIN() – finds the minimum value in a column 4. MAX() – finds the maximum value in a column 5. COUNT() – counts the number of values in a column 6. COUNT(*) – counts row of query results The argument to a column function can be a simple column name or an expression. Example 1: Prof. Mukesh N. Tekwani Page 1 of 4
  • 2. SQL Simple Queries – Worksheet 3 What is the average quota performance of our salespeople? SELECT AVG (100 * (SALES/QUOTA)) FROM SALESREPS Computing a column total The SUM() column function computes the sum of a column of data values. The data in the column must have a numeric type (integer, decimal, floating point, or money). The result of the SUM() function has the same basic data type as the data in the column, but the result may have a higher precision. For example, if you apply the SUM() function to a column of 16-bit integers, it may produce a 32-bit integer as its result. Example 2: What are the total quotas and sales for all salespeople? SELECT SUM(QUOTA), SUM(SALES) FROM SALESREPS What is the total of the orders taken by Bill Adams? SELECT SUM(AMOUNT) FROM ORDERS, SALESREPS WHERE NAME = 'Bill Adams' AND REP = EMPL_NUM Computing an Average The AVG() column function computes the average of a column of data values. The data in the column must have a numeric type. Because the AVG() function adds the values in the column and then divides by the number of values, its result may have a different data type than that of the values in the column. For example, if you apply the AVG() function to a column of integers, the result will be either a decimal or a floating point number. Calculate the average price of products from manufacturer ACI. SELECT AVG(PRICE) FROM PRODUCTS WHERE MFR_ID = 'ACI' Calculate the average size of an order placed by Acme Mfg. (customer number 2103). SELECT AVG(AMOUNT) FROM ORDERS WHERE CUST = 2103 Page 2 of 4 mukeshtekwani@hotmail.com
  • 3. SQL Simple Queries – Worksheet 3 Finding maximum and minimum values The MIN() and MAX() column functions find the smallest and largest values in a column, respectively. The data in the column can contain numeric, string, or date/time information. The result of the MIN() or MAX() function has exactly the same data type as the data in the column. What are the smallest and largest assigned quotas? SELECT MIN(QUOTA), MAX(QUOTA) FROM SALESREPS What is the earliest order date in the database? SELECT MIN(ORDER_DATE) FROM ORDERS What is the best sales performance of any salesperson? SELECT MAX(100 * (SALES/QUOTA)) FROM SALESREPS MAX(100*(SALES/QUOTA)) Counting Data Values The COUNT() column function counts the number of data values in a column. The data in the column can be of any type. The COUNT() function always returns an integer, regardless of the data type of the column. How many customers are there? SELECT COUNT(CUST_NUM) FROM CUSTOMERS How many salespeople are over quota? SELECT COUNT(NAME) FROM SALESREPS WHERE SALES > QUOTA Find the average order amount, total order amount, average order amount as a percentage of the customer's credit limit, and average order amount as a percentage of the salesperson's quota. SELECT AVG(AMOUNT), SUM(AMOUNT), (100 * AVG(AMOUNT/CREDIT_LIMIT)), (100 * AVG(AMOUNT/QUOTA)) FROM ORDERS, CUSTOMERS, SALESREPS WHERE CUST = CUST_NUM AND REP = EMPL_NUM Prof. Mukesh N. Tekwani Page 3 of 4
  • 4. SQL Simple Queries – Worksheet 3 3 What are Subqueries? Subqueries are query statements inside of query statements. Like the order of operations in algebra, order of operations also come into play when you start to embed SQL commands inside of other SQL commands (subqueries). Example 1: Select only the most recent order(s) in the orders table. We use the MAX(). This function wraps around a table column and returns the current highest (max) value for the specified column. We use this function to return the current "highest", or most recent date value in the orders table. USE mydatabase; SELECT MAX(day_of_order) FROM orders Now we can throw this query into the WHERE clause of another SELECT query. USE mydatabase; SELECT * FROM orders WHERE day_of_order = ( SELECT MAX(day_of_order) FROM orders ) 4 SELECT Case statement SQL CASE is a very unique conditional statement providing if/then/else logic for any ordinary SQL command, such as SELECT or UPDATE. It then provides when-then-else functionality (WHEN this condition is met THEN do_this). USE northwind SELECT productid, 'Status' = CASE WHEN quantity > 0 THEN 'in stock' ELSE 'out of stock' END FROM "order details"; 5 GROUP BY statement SQL GROUP BY aggregates (consolidates and calculates) column values into a single record value. GROUP BY requires a list of table columns on which to run the calculations. USE mydatabase SELECT customer FROM orders GROUP BY customer Page 4 of 4 mukeshtekwani@hotmail.com