SlideShare une entreprise Scribd logo
1  sur  17
Sub queries
Vikas K. Gupta
Using a Subquery to Solve a Problem


To solve this problem, you need two
queries: one to find what Abel earns,
and a second query to find who
earns more than that amount.



You can solve this problem by
combining the two queries, placing
one query inside the other query.



The inner query or the subquery
returns a value that is used by the
outer query or the main query.



Using a subquery is equivalent to
performing two sequential queries
and using the result of the first query
as the search value in the second
query.
Subqueries


A subquery is a SELECT statement
that is embedded in a clause of
another SELECT statement.



They can be very useful when you need
to select rows from a table with a
condition that depends on the data in the
table itself.

You can place the subquery in a number
of SQL clauses, including:
- The WHERE clause
- The HAVING clause
- The FROM clause

You can build powerful statements out of
simple ones by using subqueries.





The subquery generally executes first,
and its output is used to complete the
query condition for the main or outer
query.

Note: Comparison conditions fall into two
classes: single-row operators (>, =, >=,
<, <>, <=) and multiple-row operators
(IN, ANY, ALL).



subqueries can be placed in the
CREATE VIEW statement, CREATE
TABLE statement, UPDATE statement,
INTO clause of an INSERT statement,
and SET clause of an UPDATE
statement.

The subquery is often referred to as a
nested SELECT, sub-SELECT, or inner
SELECT statement.



operator includes a comparison condition
such as >, =, or IN





Sub-query Syntax

• The subquery (inner query) executes once before the main query.
• The result of the subquery is used by the main query (outer query).
Example :


In the slide, the inner query determines the salary of employee Abel.



The outer query takes the result of the inner query and uses this result to display all
the employees who earn more than this amount.



Execute the subquery (inner query) on its own first to show the value that the
subquery returns.



Then execute the outer query using the result returned by the inner query.
Finally, execute the entire query (containing the subquery), and show that the
result is the same.
Guidelines for Using Subqueries


The ORDER BY clause in the subquery is not needed unless you are
performing Top-N analysis.



Use single-row operators with single-row subqueries and use multiple-row
operators with multiple-row subqueries.



Prior to release Oracle8i, subqueries could not contain an ORDER BY clause.



Only one ORDER BY clause can be used for a SELECT statement, and if
specified it must be the last clause in the main SELECT statement. Starting with
release Oracle8i, an ORDER BY clause can be used and is required in the
subquery to perform Top-N analysis.



The Oracle server imposes no limit on the number of subqueries; the limit is
related to the buffer size that the query uses.
Types of Subqueries


Single-row subqueries:
Queries that return only one row
from the inner SELECT statement



Multiple-row subqueries:
Queries that return more than one
row from the inner SELECT
statement



Note: There are also multiplecolumn subqueries: Queries that
return more than one column from
the inner SELECT statement.
Single-Row Subqueries


Return only one row



Use single-row comparison operators
Executing Single-Row Subqueries


A SELECT statement can be considered as a query block.



The example on the slide displays employees whose job ID is the same as that of employee 141
and whose salary is greater than that of employee 143.



The example consists of three query blocks: the outer query and two inner queries.



The inner query blocks are executed first, producing the query results ST_CLERK and
2600, respectively.



The outer query block is then processed and uses the values returned by the inner queries
to complete its search conditions.
Using Group Functions in a Subquery


The example on the slide displays the employee last name, job ID, and
salary of all employees whose salary is equal to the minimum salary.



The MIN group function returns a single value (2500) to the outer query.
The HAVING Clause with Subqueries


The Oracle server executes subqueries first.



The Oracle server returns results into the HAVING clause of the main query.



The SQL statement on the slide displays all the departments that have a minimum salary greater than that of
department 50.
Problems with Subqueries


A common problem with subqueries is no rows being returned by the inner query.



There is no employee named Haas. So the subquery returns no rows.



The outer query takes the results of the subquery (null) and uses these results in its
WHERE clause. The outer query finds no employee with a job ID equal to null, and so
returns no rows.



If a job existed with a value of null, the row is not returned because comparison of two null
values yields a null, therefore the WHERE condition is not true.
Multiple-Row Subqueries


Subqueries that return more than one row
are called multiple-row subqueries.



You use a multiple-row operator, instead of
a single-row operator, with a multiple-row
subquery.



The multiple-row operator expects one or
more values.
ANY Operator in Multiple-Row Subqueries


The ANY operator compares a value to each value returned by a subquery.



The slide example displays employees who are not IT programmers and whose
salary is less than that of any IT programmer. The maximum salary that a
programmer earns is $9,000.



<ANY means less than the maximum.
>ANY means greater than the minimum.
=ANY is equivalent to IN.









<ALL means less than the minimum.
>ALL means greater than the maximum.
When using SOME or ANY, you often use the DISTINCT keyword to prevent
rows from being selected several times.
Example :

The NOT operator can be used with IN, ANY, and ALL operators.
Null Values in a Subquery


One of the values returned by the inner query is a null value, and hence the
entire query returns no rows.



The reason is that all conditions that compare a null value result in a
null.



So whenever null values are likely to be part of the results set of a subquery,
do not use the NOT IN operator.



The NOT IN operator is equivalent to <> ALL.
Summary


In this lesson, you should have learned how to use subqueries.



A subquery is a SELECT statement that is embedded in a clause of another SQL
statement.



Subqueries are useful when a query is based on a search criteria with unknown
intermediate values.

Subqueries have the following characteristics:


Can pass one row of data to a main statement that contains a single-row operator, such as
=, <>, >, >=, <, or <=



Can pass multiple rows of data to a main statement that contains a multiple-row operator,
such as IN



Are processed first by the Oracle server, and the WHERE or HAVING clause uses the
results



Can contain group functions

Contenu connexe

Tendances

SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statement
Syed Zaid Irshad
 

Tendances (20)

Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Sql Constraints
Sql ConstraintsSql Constraints
Sql Constraints
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
Sql joins
Sql joinsSql joins
Sql joins
 
Sub query_SQL
Sub query_SQLSub query_SQL
Sub query_SQL
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
joins in database
 joins in database joins in database
joins in database
 
MySql Triggers Tutorial - The Webs Academy
MySql Triggers Tutorial - The Webs AcademyMySql Triggers Tutorial - The Webs Academy
MySql Triggers Tutorial - The Webs Academy
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 
Sql join
Sql  joinSql  join
Sql join
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
MySQL JOINS
MySQL JOINSMySQL JOINS
MySQL JOINS
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statement
 

En vedette

Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
vijaybusu
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
ecomputernotes
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
Kaing Menglieng
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
Sidney Chen
 

En vedette (20)

Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
Oracle Advanced SQL
Oracle Advanced SQLOracle Advanced SQL
Oracle Advanced SQL
 
Oracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurOracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |Thrissur
 
Subqueries
SubqueriesSubqueries
Subqueries
 
Pivot Unpivot
Pivot UnpivotPivot Unpivot
Pivot Unpivot
 
Technical stream presentation
Technical stream presentationTechnical stream presentation
Technical stream presentation
 
Using T-SQL
Using T-SQL Using T-SQL
Using T-SQL
 
MS SQL SERVER: Creating Views
MS SQL SERVER: Creating ViewsMS SQL SERVER: Creating Views
MS SQL SERVER: Creating Views
 
Sql db optimization
Sql db optimizationSql db optimization
Sql db optimization
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
 
Joins SQL Server
Joins SQL ServerJoins SQL Server
Joins SQL Server
 
Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012
 
Sql xp 04
Sql xp 04Sql xp 04
Sql xp 04
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
 

Similaire à SQL subquery

Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
maxpane
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 

Similaire à SQL subquery (20)

7. Using Sub Queries
7. Using Sub Queries7. Using Sub Queries
7. Using Sub Queries
 
Les06
Les06Les06
Les06
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
Les06
Les06Les06
Les06
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
 
How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
 
Sql scripting sorcerypaper
Sql scripting sorcerypaperSql scripting sorcerypaper
Sql scripting sorcerypaper
 
Les18
Les18Les18
Les18
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
Att160 f.tmp
Att160 f.tmpAtt160 f.tmp
Att160 f.tmp
 
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
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
Les06
Les06Les06
Les06
 
SUBQUERIES.pptx
SUBQUERIES.pptxSUBQUERIES.pptx
SUBQUERIES.pptx
 
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 

Dernier

Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 

Dernier (20)

Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهله
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 

SQL subquery

  • 2. Using a Subquery to Solve a Problem  To solve this problem, you need two queries: one to find what Abel earns, and a second query to find who earns more than that amount.  You can solve this problem by combining the two queries, placing one query inside the other query.  The inner query or the subquery returns a value that is used by the outer query or the main query.  Using a subquery is equivalent to performing two sequential queries and using the result of the first query as the search value in the second query.
  • 3. Subqueries  A subquery is a SELECT statement that is embedded in a clause of another SELECT statement.  They can be very useful when you need to select rows from a table with a condition that depends on the data in the table itself. You can place the subquery in a number of SQL clauses, including: - The WHERE clause - The HAVING clause - The FROM clause You can build powerful statements out of simple ones by using subqueries.   The subquery generally executes first, and its output is used to complete the query condition for the main or outer query. Note: Comparison conditions fall into two classes: single-row operators (>, =, >=, <, <>, <=) and multiple-row operators (IN, ANY, ALL).  subqueries can be placed in the CREATE VIEW statement, CREATE TABLE statement, UPDATE statement, INTO clause of an INSERT statement, and SET clause of an UPDATE statement. The subquery is often referred to as a nested SELECT, sub-SELECT, or inner SELECT statement.  operator includes a comparison condition such as >, =, or IN   
  • 4. Sub-query Syntax • The subquery (inner query) executes once before the main query. • The result of the subquery is used by the main query (outer query).
  • 5. Example :  In the slide, the inner query determines the salary of employee Abel.  The outer query takes the result of the inner query and uses this result to display all the employees who earn more than this amount.  Execute the subquery (inner query) on its own first to show the value that the subquery returns.  Then execute the outer query using the result returned by the inner query. Finally, execute the entire query (containing the subquery), and show that the result is the same.
  • 6. Guidelines for Using Subqueries  The ORDER BY clause in the subquery is not needed unless you are performing Top-N analysis.  Use single-row operators with single-row subqueries and use multiple-row operators with multiple-row subqueries.  Prior to release Oracle8i, subqueries could not contain an ORDER BY clause.  Only one ORDER BY clause can be used for a SELECT statement, and if specified it must be the last clause in the main SELECT statement. Starting with release Oracle8i, an ORDER BY clause can be used and is required in the subquery to perform Top-N analysis.  The Oracle server imposes no limit on the number of subqueries; the limit is related to the buffer size that the query uses.
  • 7. Types of Subqueries  Single-row subqueries: Queries that return only one row from the inner SELECT statement  Multiple-row subqueries: Queries that return more than one row from the inner SELECT statement  Note: There are also multiplecolumn subqueries: Queries that return more than one column from the inner SELECT statement.
  • 8. Single-Row Subqueries  Return only one row  Use single-row comparison operators
  • 9. Executing Single-Row Subqueries  A SELECT statement can be considered as a query block.  The example on the slide displays employees whose job ID is the same as that of employee 141 and whose salary is greater than that of employee 143.  The example consists of three query blocks: the outer query and two inner queries.  The inner query blocks are executed first, producing the query results ST_CLERK and 2600, respectively.  The outer query block is then processed and uses the values returned by the inner queries to complete its search conditions.
  • 10. Using Group Functions in a Subquery  The example on the slide displays the employee last name, job ID, and salary of all employees whose salary is equal to the minimum salary.  The MIN group function returns a single value (2500) to the outer query.
  • 11. The HAVING Clause with Subqueries  The Oracle server executes subqueries first.  The Oracle server returns results into the HAVING clause of the main query.  The SQL statement on the slide displays all the departments that have a minimum salary greater than that of department 50.
  • 12. Problems with Subqueries  A common problem with subqueries is no rows being returned by the inner query.  There is no employee named Haas. So the subquery returns no rows.  The outer query takes the results of the subquery (null) and uses these results in its WHERE clause. The outer query finds no employee with a job ID equal to null, and so returns no rows.  If a job existed with a value of null, the row is not returned because comparison of two null values yields a null, therefore the WHERE condition is not true.
  • 13. Multiple-Row Subqueries  Subqueries that return more than one row are called multiple-row subqueries.  You use a multiple-row operator, instead of a single-row operator, with a multiple-row subquery.  The multiple-row operator expects one or more values.
  • 14. ANY Operator in Multiple-Row Subqueries  The ANY operator compares a value to each value returned by a subquery.  The slide example displays employees who are not IT programmers and whose salary is less than that of any IT programmer. The maximum salary that a programmer earns is $9,000.  <ANY means less than the maximum. >ANY means greater than the minimum. =ANY is equivalent to IN.      <ALL means less than the minimum. >ALL means greater than the maximum. When using SOME or ANY, you often use the DISTINCT keyword to prevent rows from being selected several times.
  • 15. Example : The NOT operator can be used with IN, ANY, and ALL operators.
  • 16. Null Values in a Subquery  One of the values returned by the inner query is a null value, and hence the entire query returns no rows.  The reason is that all conditions that compare a null value result in a null.  So whenever null values are likely to be part of the results set of a subquery, do not use the NOT IN operator.  The NOT IN operator is equivalent to <> ALL.
  • 17. Summary  In this lesson, you should have learned how to use subqueries.  A subquery is a SELECT statement that is embedded in a clause of another SQL statement.  Subqueries are useful when a query is based on a search criteria with unknown intermediate values. Subqueries have the following characteristics:  Can pass one row of data to a main statement that contains a single-row operator, such as =, <>, >, >=, <, or <=  Can pass multiple rows of data to a main statement that contains a multiple-row operator, such as IN  Are processed first by the Oracle server, and the WHERE or HAVING clause uses the results  Can contain group functions