SlideShare a Scribd company logo
SOFTWARE TESTING Documents
SUB QUERIES
A sub query can be defined as a group of nested SELECT statements inside a
SELECT, INSERT, UPDATE or DELETE statement. Sub query can also be used inside
the WHERE or HAVING clauses of the outer SELECT, INSERT, UPDATE or DELETE
statements. SELECT statements containing one or more sub queries are called
nested queries.
The command syntax is:
A subquery must be enclosed within parentheses and cannot use ORDER BY,
COMPUTE BY or FOR BROWSE clauses. SQL Server does not implement any
restriction on level of nesting while using subqueries, SQL Server imposes
restrictions on the number of tables or views used in a subquery or a join.
SQL Server evaluates the inner query first and returns the result to the outer query
for the final result set.
The outer query always depends on the evaluation result of the subquery.
Subqueries can be divided into three catefories depending upon the values they
return;
 Sub queries that operate on lists: this type of query returns single-column-
multiple values results and are implemented using the IN clause. The syntax
is as follows:
 Subqueries that are introduced with an unmodified comparison o-erator: this
type of query returns single column-single value results for outer query
evaluation and is implemented using unmodified comparison
operators(operators without the ANY or ALL keywords) the syntax is as
follows:
 Subqueries that check for the existence of data: this type of query checks for
the existence of records in a table that are used in the inner query, and
returns either a TRUE or a FALSE VALUE based on the existence of data. This
is implemented using the EXISTS keyword. The syntax is as follows:
Visit: www.gcreddy.com for QTP Documents
1
(SELECT [ALL|DISTINCT] suquery_select_list
[FROM {table_nmae | view_name}
[[,table_name2|view_name2}]
[..,{table_name16|view_name16{]]
[WHERE clause]
GROUP BY clause]
[HAVING clause])
WHERE expression [NOT] IN (subquery)
WHERE expression comparison_operator [ANY|ALL] (subquery)
SOFTWARE TESTING Documents
SubQueries With IN
A subquery introduced with IN returns zero or more values. Consider the
following example where all author ID’S, from the TITLEAUTHOR table, are displayed
whose books are sold:
SQL Server returns a list of all title IDs to the main query then lists all the authors,
whose books are sold, in the result set.
Consider the following example where the server returns a list of publisher IDs to the
main query, and then determines whether each publisher’s pub_id is in that list:
The inner query is evaluated first and then the result set is sent to the outer query.
Consider another subquery with the IN clause:
The inner query is evaluated first and then the result set is sent to the outer query.
The NOT IN clause is used in the same way as the IN clause. Consider the following
example.
Visit: www.gcreddy.com for QTP Documents
2
WHERE [NOT] EXISTS (subquery)
SELECT au_id
FROM titleauthor
WHERE title_id IN (SELECT title_id FROM sales)
SELECT publisher=pub_name
FROM publishers
WHERE pub_id IN ( SELECT pub_id FROM titles WHERE type=’business’)
SELECT type=type, Average=AVG(ytd_sales)
FROM titles
WHERE type IN (SELECT type FROM titles
WHERE title=” the busy executive’s database guide” or title=’Is Anger the Enemy?’)
GROUUP BY type
SELECT pub_id, pub_name
FROM publishers
WHERE pub_id NOT IN (SELECT pub_id FROM titles
WHERE type=’mod_cook’)
SOFTWARE TESTING Documents
Sub Queries with EXISTS
The subquery, when used with the EXISTS clause, always returns data in
terms of TRUE OR FALSE and passes the status to the outer query to produce the
results set. The subquery returns a TRUE value if the result set contains any rows.
The query introduced with the EXISTS keyword differs from other queries. The
EXISTS keyword is not preceded by any column name, constant or there expression
and it contains an asterisk (*) in the SELECT list.
Aggregate functions can also be used in subqueries. Consider the following example
which displays the titles of all those books for which the advance is more than the
average advance of business related books.
Subquery Restrictions:
The restrictions imposed are:
 The column list of the select statement of a subquery introduced with the
comparison operator can include only one column.
 The column used in the WHERE clause of the outer query should be
compatible with the column used in the select list of the inner query.
 The DISTINCT keyword cannot be used with the subqueries that include the
GROUP BY clause.
 The ORDER BY clause, GROUP BY clause and INTO keyword cannot be used in
a subquery, because a subquery cannot manipulate its result internally.
 Do not specify more than one column name in the subquery introduced with
the EXISTS keyword.
 A view created with a subquery cannot be updated.
Nested Sub Queries:
A sub query can contain one or more subqueries. There is no restriction in the
number of subqueries that you can include with SELECT, INSERRT, UPDATE or
DELETE statements.
Visit: www.gcreddy.com for QTP Documents
3
1. SELECT pub_name
FROM publishers
WHERE EXISTS (SELECT * FROM titles WHERE type=’business’)
2. SELECT pub_name
FROM publishers
WHERE EXISTS (SELECT * FROM publishers WHERE City=’Paris’)
SELECT Title=title
FROM titles
WHERE advance>(SELECT AVG (advance) from titles
WHERE type=’business’)
SOFTWARE TESTING Documents
Example:
Example Description
SELECT title_id=title_id, Title=title
FROM title
WHERE prie>ALL(SELECT price
FROM titles
WHERE pub_id=’0736’)
Lists all the titles along with their IDs
from the titles table where price is
greater than the maximum price of books
published by the publisher with publisher
ID 0736.
SELECT title_ID = title_id, Title = title
FROM titles
WHERE price >ANY (SELECT price
FROM titles
WHERE pub_id = `0736`)
Lists all the titles along with their titles
IDs from the titles table where price is
greater than the minimum price of books
published by the publisher with publisher
ID 0736.
SELECT publisher_ID = pub_id, Name =
pub_name
FROM publishers
WHERE city = ANY (SELECT city FROM
authors)
Lists all the publishers where city is sane
as of any author.
SELECT INTO Statement
A SELECT statement with an INTO clause is used to store the result set in a new
table without a data definition process. The SELECT INTO statement creates a new
table, if the table already exists then the operation fails.
The syntax of the SELECT INTO statement is:
The SELECT INTO clause creates a permanent table if the select into/bulk copy
database option is set. If the select into/bulkcopy database option is not set, then
only local temporary tables (prefixed by #), and global temporary tables (prefixed by
##) can be created.
Visit: www.gcreddy.com for QTP Documents
4
1. SELECT ‘Author Name’=SUBSTRING (au_fname, 1,1)+’.’+au_lastname
FROM authors WHERE au_id IN(SELECT au_id FROM titleauthor
WHERE title=’Net Etiquette’))
2. SELECT ‘Author ID’=au_id, Name=SUBSTRING (au_fnmae, 1,1) +
‘.’+au_lname FROM authors WHERE au_id IN (SELECT au_id FROM titleauthor
WHERE type=’business’))
SELECT columns_list
INTO new_table_name
FROM table_names
WHERE conditions
SOFTWARE TESTING Documents
The select into/bulkcopy database option can be set using the following command:
SELECT title_id, title
INTO newtitles
From titles
WHERE price >$15
Column names in the new table can be changed while specifying the columns in the
SELECT statement. Consider the following example with the new column names:
UNION
SQL Server provides a unique operator known as the UNION operator that is used
to combine the result set or more queries.
The syntax is:
By default, the result set of the UNION operator removes the duplicate rows from the
queries combined, until an ALL clause is specified with the UNION operator.
The queries combined with the UNION operator must contain an equal number of
columns or expressions, and they must be compatible with each other
There are two tables: region_east and region_west. The region_east table contains
the employee id, name and address of the employees of the eastern region. The
region_west table contains the employee id, name and the address of the employees
of the western region.
Visit: www.gcreddy.com for QTP Documents
5
sp_dboption ‘pubs’, ‘select into / bulkcopy ‘, true
SELECT Title _ID = title _id, Title_Name =title
INTO new titles 1
FROM titles
WHERE advance >$7000
SELECT column_list [INTO new_table_name]
[FROM clause]
[WHERE clause]
[GROUP BY clause]
[HAVING clause]….]
[ORDER BY clause]
[COMPUTE clause]
SOFTWARE TESTING Documents
The region_east table contains the following data:
emp_id emp_name emp_add
- - - - - - - - - - - - - - - - - - - - - - - - -- -- - - - - - - - - - - - - - - - - -
E001 George 323, Park Avenue Street
E002 Jack 475, Barbara Lines
E003 Nancy 68, Bank Street
The region_west table contains the following data:
Emp_id emp_name emp_add
- - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - -- - - - - - - - -
W001 Maria 45, Canara Street
W002 James 12, Blue Lines
W003 Jill 98, Civil Lines
The following statement displays employee ids and names of the both the regions:
east and the west.
SELECT emp_id emp_name FROM region_east
Union
SELECT emp_id, emp_name FROM region_west
SELECT ‘Employee ID’ =emp_id, ‘Employee Name’=emp_name FROM region_east
Union
SELECT emp_, emp_name FROM region_west
Rules Regarding the use of the UNION Operator
The restrictions imposed by SQL Server on the use of the UNION
operator are listed below:
• Corresponding columns in the individual queries of a UNION statement must
occur in the same order, because UNION compares the columns in the order
specified in the individual queries.
• The columns or expressions used in one query must be equal in number, and
should be compatible with the columns or expressions of other queries.
• The first query in the UNION statement can only contain the INTO clause to
store the final result set. The INTO clause cannot be used with any query
other than the first query while implementing the UNION operator.
• The ORDER BY and COMPUTE BY clauses cannot be used in an individual
query. These clauses can be used only at the end of the last query to
summarize and order the final result set.
• The GROUP BY and HAVING clauses can only be used with individual queries
and cannot be used for the final result to be set.
• The UNION operator can also be used with the INSERT statement.
Visit: www.gcreddy.com for QTP Documents
6
SOFTWARE TESTING Documents
Visit: www.gcreddy.com for QTP Documents
7

More Related Content

Recently uploaded

How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 

Recently uploaded (20)

How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Sql quaries

  • 1. SOFTWARE TESTING Documents SUB QUERIES A sub query can be defined as a group of nested SELECT statements inside a SELECT, INSERT, UPDATE or DELETE statement. Sub query can also be used inside the WHERE or HAVING clauses of the outer SELECT, INSERT, UPDATE or DELETE statements. SELECT statements containing one or more sub queries are called nested queries. The command syntax is: A subquery must be enclosed within parentheses and cannot use ORDER BY, COMPUTE BY or FOR BROWSE clauses. SQL Server does not implement any restriction on level of nesting while using subqueries, SQL Server imposes restrictions on the number of tables or views used in a subquery or a join. SQL Server evaluates the inner query first and returns the result to the outer query for the final result set. The outer query always depends on the evaluation result of the subquery. Subqueries can be divided into three catefories depending upon the values they return;  Sub queries that operate on lists: this type of query returns single-column- multiple values results and are implemented using the IN clause. The syntax is as follows:  Subqueries that are introduced with an unmodified comparison o-erator: this type of query returns single column-single value results for outer query evaluation and is implemented using unmodified comparison operators(operators without the ANY or ALL keywords) the syntax is as follows:  Subqueries that check for the existence of data: this type of query checks for the existence of records in a table that are used in the inner query, and returns either a TRUE or a FALSE VALUE based on the existence of data. This is implemented using the EXISTS keyword. The syntax is as follows: Visit: www.gcreddy.com for QTP Documents 1 (SELECT [ALL|DISTINCT] suquery_select_list [FROM {table_nmae | view_name} [[,table_name2|view_name2}] [..,{table_name16|view_name16{]] [WHERE clause] GROUP BY clause] [HAVING clause]) WHERE expression [NOT] IN (subquery) WHERE expression comparison_operator [ANY|ALL] (subquery)
  • 2. SOFTWARE TESTING Documents SubQueries With IN A subquery introduced with IN returns zero or more values. Consider the following example where all author ID’S, from the TITLEAUTHOR table, are displayed whose books are sold: SQL Server returns a list of all title IDs to the main query then lists all the authors, whose books are sold, in the result set. Consider the following example where the server returns a list of publisher IDs to the main query, and then determines whether each publisher’s pub_id is in that list: The inner query is evaluated first and then the result set is sent to the outer query. Consider another subquery with the IN clause: The inner query is evaluated first and then the result set is sent to the outer query. The NOT IN clause is used in the same way as the IN clause. Consider the following example. Visit: www.gcreddy.com for QTP Documents 2 WHERE [NOT] EXISTS (subquery) SELECT au_id FROM titleauthor WHERE title_id IN (SELECT title_id FROM sales) SELECT publisher=pub_name FROM publishers WHERE pub_id IN ( SELECT pub_id FROM titles WHERE type=’business’) SELECT type=type, Average=AVG(ytd_sales) FROM titles WHERE type IN (SELECT type FROM titles WHERE title=” the busy executive’s database guide” or title=’Is Anger the Enemy?’) GROUUP BY type SELECT pub_id, pub_name FROM publishers WHERE pub_id NOT IN (SELECT pub_id FROM titles WHERE type=’mod_cook’)
  • 3. SOFTWARE TESTING Documents Sub Queries with EXISTS The subquery, when used with the EXISTS clause, always returns data in terms of TRUE OR FALSE and passes the status to the outer query to produce the results set. The subquery returns a TRUE value if the result set contains any rows. The query introduced with the EXISTS keyword differs from other queries. The EXISTS keyword is not preceded by any column name, constant or there expression and it contains an asterisk (*) in the SELECT list. Aggregate functions can also be used in subqueries. Consider the following example which displays the titles of all those books for which the advance is more than the average advance of business related books. Subquery Restrictions: The restrictions imposed are:  The column list of the select statement of a subquery introduced with the comparison operator can include only one column.  The column used in the WHERE clause of the outer query should be compatible with the column used in the select list of the inner query.  The DISTINCT keyword cannot be used with the subqueries that include the GROUP BY clause.  The ORDER BY clause, GROUP BY clause and INTO keyword cannot be used in a subquery, because a subquery cannot manipulate its result internally.  Do not specify more than one column name in the subquery introduced with the EXISTS keyword.  A view created with a subquery cannot be updated. Nested Sub Queries: A sub query can contain one or more subqueries. There is no restriction in the number of subqueries that you can include with SELECT, INSERRT, UPDATE or DELETE statements. Visit: www.gcreddy.com for QTP Documents 3 1. SELECT pub_name FROM publishers WHERE EXISTS (SELECT * FROM titles WHERE type=’business’) 2. SELECT pub_name FROM publishers WHERE EXISTS (SELECT * FROM publishers WHERE City=’Paris’) SELECT Title=title FROM titles WHERE advance>(SELECT AVG (advance) from titles WHERE type=’business’)
  • 4. SOFTWARE TESTING Documents Example: Example Description SELECT title_id=title_id, Title=title FROM title WHERE prie>ALL(SELECT price FROM titles WHERE pub_id=’0736’) Lists all the titles along with their IDs from the titles table where price is greater than the maximum price of books published by the publisher with publisher ID 0736. SELECT title_ID = title_id, Title = title FROM titles WHERE price >ANY (SELECT price FROM titles WHERE pub_id = `0736`) Lists all the titles along with their titles IDs from the titles table where price is greater than the minimum price of books published by the publisher with publisher ID 0736. SELECT publisher_ID = pub_id, Name = pub_name FROM publishers WHERE city = ANY (SELECT city FROM authors) Lists all the publishers where city is sane as of any author. SELECT INTO Statement A SELECT statement with an INTO clause is used to store the result set in a new table without a data definition process. The SELECT INTO statement creates a new table, if the table already exists then the operation fails. The syntax of the SELECT INTO statement is: The SELECT INTO clause creates a permanent table if the select into/bulk copy database option is set. If the select into/bulkcopy database option is not set, then only local temporary tables (prefixed by #), and global temporary tables (prefixed by ##) can be created. Visit: www.gcreddy.com for QTP Documents 4 1. SELECT ‘Author Name’=SUBSTRING (au_fname, 1,1)+’.’+au_lastname FROM authors WHERE au_id IN(SELECT au_id FROM titleauthor WHERE title=’Net Etiquette’)) 2. SELECT ‘Author ID’=au_id, Name=SUBSTRING (au_fnmae, 1,1) + ‘.’+au_lname FROM authors WHERE au_id IN (SELECT au_id FROM titleauthor WHERE type=’business’)) SELECT columns_list INTO new_table_name FROM table_names WHERE conditions
  • 5. SOFTWARE TESTING Documents The select into/bulkcopy database option can be set using the following command: SELECT title_id, title INTO newtitles From titles WHERE price >$15 Column names in the new table can be changed while specifying the columns in the SELECT statement. Consider the following example with the new column names: UNION SQL Server provides a unique operator known as the UNION operator that is used to combine the result set or more queries. The syntax is: By default, the result set of the UNION operator removes the duplicate rows from the queries combined, until an ALL clause is specified with the UNION operator. The queries combined with the UNION operator must contain an equal number of columns or expressions, and they must be compatible with each other There are two tables: region_east and region_west. The region_east table contains the employee id, name and address of the employees of the eastern region. The region_west table contains the employee id, name and the address of the employees of the western region. Visit: www.gcreddy.com for QTP Documents 5 sp_dboption ‘pubs’, ‘select into / bulkcopy ‘, true SELECT Title _ID = title _id, Title_Name =title INTO new titles 1 FROM titles WHERE advance >$7000 SELECT column_list [INTO new_table_name] [FROM clause] [WHERE clause] [GROUP BY clause] [HAVING clause]….] [ORDER BY clause] [COMPUTE clause]
  • 6. SOFTWARE TESTING Documents The region_east table contains the following data: emp_id emp_name emp_add - - - - - - - - - - - - - - - - - - - - - - - - -- -- - - - - - - - - - - - - - - - - - E001 George 323, Park Avenue Street E002 Jack 475, Barbara Lines E003 Nancy 68, Bank Street The region_west table contains the following data: Emp_id emp_name emp_add - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - -- - - - - - - - - W001 Maria 45, Canara Street W002 James 12, Blue Lines W003 Jill 98, Civil Lines The following statement displays employee ids and names of the both the regions: east and the west. SELECT emp_id emp_name FROM region_east Union SELECT emp_id, emp_name FROM region_west SELECT ‘Employee ID’ =emp_id, ‘Employee Name’=emp_name FROM region_east Union SELECT emp_, emp_name FROM region_west Rules Regarding the use of the UNION Operator The restrictions imposed by SQL Server on the use of the UNION operator are listed below: • Corresponding columns in the individual queries of a UNION statement must occur in the same order, because UNION compares the columns in the order specified in the individual queries. • The columns or expressions used in one query must be equal in number, and should be compatible with the columns or expressions of other queries. • The first query in the UNION statement can only contain the INTO clause to store the final result set. The INTO clause cannot be used with any query other than the first query while implementing the UNION operator. • The ORDER BY and COMPUTE BY clauses cannot be used in an individual query. These clauses can be used only at the end of the last query to summarize and order the final result set. • The GROUP BY and HAVING clauses can only be used with individual queries and cannot be used for the final result to be set. • The UNION operator can also be used with the INSERT statement. Visit: www.gcreddy.com for QTP Documents 6
  • 7. SOFTWARE TESTING Documents Visit: www.gcreddy.com for QTP Documents 7