Difference between revisions of "SUB QUERIES"

From rbachwiki
Jump to navigation Jump to search
(Created page with "A SUB QUERY is a query within a query which can return one or more rows SELECT * FROM SALES WHERE CUSTOMER_ID = (SELECT CUSTOMER_ID FROM CUSTOMER WHERE LAST_NAME = 'JOSEPH'...")
 
Line 1: Line 1:
A SUB QUERY is a query within a query which can return one or more rows
A SUB QUERY is a query within a query which can return one or more rows
  SELECT * FROM SALES
  SELECT * FROM SALES
  WHERE CUSTOMER_ID =
  WHERE CUSTOMER_ID =
  (SELECT CUSTOMER_ID FROM CUSTOMER WHERE LAST_NAME = 'JOSEPH'
  (SELECT CUSTOMER_ID FROM CUSTOMER WHERE LAST_NAME = 'JOSEPH'


A SUBQUERY executes first before the main query
A SUBQUERY executes first before the main query, SO the customer id was returned from the the subquery then passed to the original query
 
When the subquery returns more than one value use the IN instead of the =


  SELECT * FROM SALES
  SELECT * FROM SALES
  WHERE CUSTOMER_ID IN   
  WHERE CUSTOMER_ID IN   
  (SELECT CUSTOMER_ID FROM CUSTOMER WHERE REGION ='SOUTH')
  (SELECT CUSTOMER_ID FROM CUSTOMER WHERE REGION ='SOUTH')
SELECT * FROM SALES
WHERE CUSTOMER_ID IN
(SELECT CUSTOMER_ID FROM CUSTOMER WHERE LAST_NAME = 'JOSEPH' OR last_name = 'mann'


[[#Rules for Character Strings Command|Back To Top]]-[[Main_Page| Home]] - [[Oracle_SQL|Category]]
[[#Rules for Character Strings Command|Back To Top]]-[[Main_Page| Home]] - [[Oracle_SQL|Category]]

Revision as of 20:20, 14 November 2017

A SUB QUERY is a query within a query which can return one or more rows

SELECT * FROM SALES
WHERE CUSTOMER_ID =
(SELECT CUSTOMER_ID FROM CUSTOMER WHERE LAST_NAME = 'JOSEPH'

A SUBQUERY executes first before the main query, SO the customer id was returned from the the subquery then passed to the original query

When the subquery returns more than one value use the IN instead of the =

SELECT * FROM SALES
WHERE CUSTOMER_ID IN  
(SELECT CUSTOMER_ID FROM CUSTOMER WHERE REGION ='SOUTH')
SELECT * FROM SALES
WHERE CUSTOMER_ID IN
(SELECT CUSTOMER_ID FROM CUSTOMER WHERE LAST_NAME = 'JOSEPH' OR last_name = 'mann'

Back To Top- Home - Category