SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
How to work with Subquery in Data Mining?
Data Miningis the only process of sorting the large data sets in a quick manner. Also, it
identifies each record with its relationship to solve through the data analysisskills.
Data miningincludes such wonderful methods for data analysis like Machine Learning,
Statistics, and Database Systems. The Main purpose of data mining is to extract the information
from large data set and transform it into a useful data structure.
In this article, we will learn about how subquery helps to find the better result with the help of data
mining but before going ahead we need to learn what is subquery and its working relationship with
data mining.
What is Subquery in Data Mining
A subquery is a query, which comes inside another query in SQL, this is also known as a Nested
query or Inner query. This returns data that is used in the main query on behalf of condition inside
parentheses. Therefore, A subquery is embedded within WHERE clause, FROM and SELECT
clause. You can see the exact syntax of a subquery as follows.
•A subquery always works within the WHERE Clause of other SQL SELECT command.
•It is always enclosed within parentheses.
•Also, you can use these operators, much like as >, <, or =.
•The nested query runs first before its main (Parent) query. So, the results of a nested
query pass to the outer query.
•You can not use ORDER BY command within nested query but you can use ORDER
BY command in the main query.
•An inner query always consists of one selected column in the SELECT clause until the
multiple columns compare to its selected columns.
•The BETWEEN clause cannot be used in the subquery, However, you can use
BETWEEn clause in the main query.
You can use all common commands like SELECT, INSERT, UPDATE and DELETE in
the subquery for getting the result better through the help of Data Mining Company process.
Subqueries with the SELECT Command
There is a common syntax for writing a subquery as given below,
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE]
)
So, let’s take a real example, we assume a table like as given below;
So, let’s start with a Select command for the following output,
SELECT * FROM CUSTOMERS
WHERE ID IN
(
SELECT ID FROM CUSTOMERS
WHERE SALARY > 4500
)
Finally, the output will look like this.
You have seen the simple example of insert the data in the table. Therefore, we take another
example for more clarification. We need to create two table tblProducts and tblProductSales and
insert sample data for the same.
Script to create table tblProducts
Create Table tblProducts
(
[Id] int identity primary key,
[Name] nvarchar(50),
[Description] nvarchar(250)
)
Create Table tblProductSales
(
Id int primary key identity,
ProductId int foreign key references tblProducts(Id),
UnitPrice int,
QuantitySold int
)
statement 1
Insert into tblProducts values ('TV', '52 inch black color LCD
TV')
statement 2
Insert into tblProducts values ('Laptop', 'Very thin black color
acer laptop')
statement 3
Insert into tblProducts values ('Desktop', 'HP high performance
desktop')
statement 4
Insert into tblProductSales values(3, 450, 5)
statement 5
Insert into tblProductSales values(2, 250, 7)
statement 6
Insert into tblProductSales values(3, 450, 4)
statement 7
Insert into tblProductSales values(3, 450, 9)
Finally, the output for the following table.
Now, I want to retrieve products that are not at all sold,
Select [Id], [Name], [Description]
from tblProducts
where Id not in (Select Distinct ProductId from tblProductSales)
Finally, the output as follows.
How Subquery works with Joins
By usinHow Subquery workss you the same results. So, Let’s create the query for the same as
follows,
Select tblProducts.[Id], [Name], [Description]
from tblProducts
left join tblProductSales
on tblProducts.Id = tblProductSales.ProductId
where tblProductSales.ProductId IS NULL
When you execute the code, you get the same output that we achieved above.
Hence, you have seen how to use a subquery with WHERE clause.
In addition, let’s have a look, a subquery in the SELECT clause. So, I want to retrieve the NAME
and TOTALQUANTITY sold by using a subquery.
Select [Name],
(Select SUM(QuantitySold) from tblProductSales where ProductId =
tblProducts.Id) as TotalQuantity
from tblProducts
order by Name
Hence, you get the same output.
If you use joins, then you can get the same out as above,
Select [Name], SUM(QuantitySold) as TotalQuantity
from tblProducts
left join tblProductSales
on tblProducts.Id = tblProductSales.ProductId
group by [Name]
order by Name
Finally, you get the Output:
From above examples, it seems clear now that, a subquery simply use a select statement, that gives
a single value in return and also inner query inside a SELECT, UPDATE, INSERT, or DELETE
command.
What to choose SubQueries or Joins in Data Mining
This is something very confusing because some of the developers get confused which one should be
used for sorting the data in data mining. But remember both the function gives the same result but
they have their own capabilities so let’s understand through a table.
we assume data in both the tables tblProducts and tblProductSales.
400,00 data in the tblProducts table
600,00 data in tblProductSales tables
When I execute the command, then, you get 306,199 rows in 6 seconds
Select Id, Name, Description
from tblProducts
where ID IN
(
Select ProductId from tblProductSales
)
Finally, you get the output as follows,
For this reason, please swipe the command and execute plan cache by using the following T-SQL
statement.
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS; -- Clears query cache
Go
DBCC FREEPROCCACHE; -- Clears execution plan cache
GO
Now, execute the command by using joins. Note that we get the exact same 306,199 rows in 6
seconds.
Select distinct tblProducts.Id, Name, Description
from tblProducts
inner join tblProductSales
on tblProducts.Id = tblProductSales.ProductId
Please Note: We are using automated SQL script to insert large amount of random data.
According to MSDN, a join gives better performance. Else, the subquery must be passed for each
result of the main query. As a result, a joins gives the better results instead of a subquery.
The above query command gives the products result that we have not sold for once. When I run the
query I get 93,801 rows in 3 seconds
Select Id, Name, [Description]
from tblProducts
where Not Exists(Select * from tblProductSales where ProductId =
tblProducts.Id)
Finally, you get the output as follows,
When I run the query by using joins, I get the same result 93,801 rows in 3 seconds.
Select tblProducts.Id, Name, [Description]
from tblProducts
left join tblProductSales
on tblProducts.Id = tblProductSales.ProductId
where tblProductSales.ProductId IS NULL
In conclusion, joins work faster than subqueries, but if we see in reality, it all depends on the plan of
execution that is made by SQL Server. Hence, if you want to get the output in a quick manner or
you do an extraction of data then go with joins for data mining.
Conclusion
In this article, you may find both results set with subquery and use of joins instead of using the
subquery while working in mining the data with any analytic tool like SQL, Hadoop, and Big Data.
A subquery verifies two identities with proper format for the IN clause. But, if we talk about a large
number of data in the database server, use joins queries rather than a subquery because joins takes
less time to get the output in data mining.
Connect With Source Url:-
https://www.loginworks.com/blogs/work-subquery-
data-mining/

Weitere ähnliche Inhalte

Was ist angesagt?

Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
MYXPLAIN
 
Sql having clause
Sql having clauseSql having clause
Sql having clause
Vivek Singh
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 

Was ist angesagt? (18)

TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
 
Tutorial - Learn SQL with Live Online Database
Tutorial - Learn SQL with Live Online DatabaseTutorial - Learn SQL with Live Online Database
Tutorial - Learn SQL with Live Online Database
 
Sql having clause
Sql having clauseSql having clause
Sql having clause
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Les11
Les11Les11
Les11
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
My Sql concepts
My Sql conceptsMy Sql concepts
My Sql concepts
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Correlated update vs merge
Correlated update vs mergeCorrelated update vs merge
Correlated update vs merge
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Not in vs not exists
Not in vs not existsNot in vs not exists
Not in vs not exists
 

Ähnlich wie How to work with Subquery in Data Mining?

Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Avoiding cursors with sql server 2005 tech republic
Avoiding cursors with sql server 2005   tech republicAvoiding cursors with sql server 2005   tech republic
Avoiding cursors with sql server 2005 tech republic
Kaing Menglieng
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
maxpane
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
Tony Wong
 
Understand when to use user defined functions in sql server tech-republic
Understand when to use user defined functions in sql server   tech-republicUnderstand when to use user defined functions in sql server   tech-republic
Understand when to use user defined functions in sql server tech-republic
Kaing Menglieng
 
See sql server graphical execution plans in action tech republic
See sql server graphical execution plans in action   tech republicSee sql server graphical execution plans in action   tech republic
See sql server graphical execution plans in action tech republic
Kaing Menglieng
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 

Ähnlich wie How to work with Subquery in Data Mining? (20)

Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
SQL interview questions jeetendra mandal - part 5
SQL interview questions jeetendra mandal - part 5SQL interview questions jeetendra mandal - part 5
SQL interview questions jeetendra mandal - part 5
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSFOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
 
SQL Database Performance Tuning for Developers
SQL Database Performance Tuning for DevelopersSQL Database Performance Tuning for Developers
SQL Database Performance Tuning for Developers
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
 
Avoiding cursors with sql server 2005 tech republic
Avoiding cursors with sql server 2005   tech republicAvoiding cursors with sql server 2005   tech republic
Avoiding cursors with sql server 2005 tech republic
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sql
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
Understand when to use user defined functions in sql server tech-republic
Understand when to use user defined functions in sql server   tech-republicUnderstand when to use user defined functions in sql server   tech-republic
Understand when to use user defined functions in sql server tech-republic
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
 
See sql server graphical execution plans in action tech republic
See sql server graphical execution plans in action   tech republicSee sql server graphical execution plans in action   tech republic
See sql server graphical execution plans in action tech republic
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Chapter5.ppt
Chapter5.pptChapter5.ppt
Chapter5.ppt
 
Oracle NOLOGGING
Oracle NOLOGGINGOracle NOLOGGING
Oracle NOLOGGING
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
Beg sql
Beg sqlBeg sql
Beg sql
 

Kürzlich hochgeladen

CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
AroojKhan71
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 

Kürzlich hochgeladen (20)

CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 

How to work with Subquery in Data Mining?

  • 1. How to work with Subquery in Data Mining? Data Miningis the only process of sorting the large data sets in a quick manner. Also, it identifies each record with its relationship to solve through the data analysisskills. Data miningincludes such wonderful methods for data analysis like Machine Learning, Statistics, and Database Systems. The Main purpose of data mining is to extract the information from large data set and transform it into a useful data structure. In this article, we will learn about how subquery helps to find the better result with the help of data mining but before going ahead we need to learn what is subquery and its working relationship with data mining. What is Subquery in Data Mining A subquery is a query, which comes inside another query in SQL, this is also known as a Nested query or Inner query. This returns data that is used in the main query on behalf of condition inside parentheses. Therefore, A subquery is embedded within WHERE clause, FROM and SELECT clause. You can see the exact syntax of a subquery as follows.
  • 2. •A subquery always works within the WHERE Clause of other SQL SELECT command. •It is always enclosed within parentheses. •Also, you can use these operators, much like as >, <, or =. •The nested query runs first before its main (Parent) query. So, the results of a nested query pass to the outer query. •You can not use ORDER BY command within nested query but you can use ORDER BY command in the main query. •An inner query always consists of one selected column in the SELECT clause until the multiple columns compare to its selected columns. •The BETWEEN clause cannot be used in the subquery, However, you can use BETWEEn clause in the main query. You can use all common commands like SELECT, INSERT, UPDATE and DELETE in the subquery for getting the result better through the help of Data Mining Company process. Subqueries with the SELECT Command There is a common syntax for writing a subquery as given below, SELECT column_name [, column_name ]
  • 3. FROM table1 [, table2 ] WHERE column_name OPERATOR (SELECT column_name [, column_name ] FROM table1 [, table2 ] [WHERE] ) So, let’s take a real example, we assume a table like as given below; So, let’s start with a Select command for the following output, SELECT * FROM CUSTOMERS WHERE ID IN ( SELECT ID FROM CUSTOMERS WHERE SALARY > 4500 ) Finally, the output will look like this.
  • 4. You have seen the simple example of insert the data in the table. Therefore, we take another example for more clarification. We need to create two table tblProducts and tblProductSales and insert sample data for the same. Script to create table tblProducts Create Table tblProducts ( [Id] int identity primary key, [Name] nvarchar(50), [Description] nvarchar(250) ) Create Table tblProductSales ( Id int primary key identity, ProductId int foreign key references tblProducts(Id), UnitPrice int, QuantitySold int ) statement 1
  • 5. Insert into tblProducts values ('TV', '52 inch black color LCD TV') statement 2 Insert into tblProducts values ('Laptop', 'Very thin black color acer laptop') statement 3 Insert into tblProducts values ('Desktop', 'HP high performance desktop') statement 4 Insert into tblProductSales values(3, 450, 5) statement 5 Insert into tblProductSales values(2, 250, 7) statement 6 Insert into tblProductSales values(3, 450, 4) statement 7 Insert into tblProductSales values(3, 450, 9) Finally, the output for the following table.
  • 6. Now, I want to retrieve products that are not at all sold, Select [Id], [Name], [Description] from tblProducts where Id not in (Select Distinct ProductId from tblProductSales) Finally, the output as follows. How Subquery works with Joins By usinHow Subquery workss you the same results. So, Let’s create the query for the same as follows, Select tblProducts.[Id], [Name], [Description] from tblProducts left join tblProductSales on tblProducts.Id = tblProductSales.ProductId where tblProductSales.ProductId IS NULL When you execute the code, you get the same output that we achieved above.
  • 7. Hence, you have seen how to use a subquery with WHERE clause. In addition, let’s have a look, a subquery in the SELECT clause. So, I want to retrieve the NAME and TOTALQUANTITY sold by using a subquery. Select [Name], (Select SUM(QuantitySold) from tblProductSales where ProductId = tblProducts.Id) as TotalQuantity from tblProducts order by Name Hence, you get the same output. If you use joins, then you can get the same out as above, Select [Name], SUM(QuantitySold) as TotalQuantity from tblProducts left join tblProductSales on tblProducts.Id = tblProductSales.ProductId group by [Name] order by Name Finally, you get the Output:
  • 8. From above examples, it seems clear now that, a subquery simply use a select statement, that gives a single value in return and also inner query inside a SELECT, UPDATE, INSERT, or DELETE command. What to choose SubQueries or Joins in Data Mining This is something very confusing because some of the developers get confused which one should be used for sorting the data in data mining. But remember both the function gives the same result but they have their own capabilities so let’s understand through a table. we assume data in both the tables tblProducts and tblProductSales. 400,00 data in the tblProducts table 600,00 data in tblProductSales tables When I execute the command, then, you get 306,199 rows in 6 seconds Select Id, Name, Description from tblProducts where ID IN ( Select ProductId from tblProductSales ) Finally, you get the output as follows,
  • 9. For this reason, please swipe the command and execute plan cache by using the following T-SQL statement. CHECKPOINT; GO DBCC DROPCLEANBUFFERS; -- Clears query cache Go DBCC FREEPROCCACHE; -- Clears execution plan cache GO Now, execute the command by using joins. Note that we get the exact same 306,199 rows in 6 seconds. Select distinct tblProducts.Id, Name, Description from tblProducts
  • 10. inner join tblProductSales on tblProducts.Id = tblProductSales.ProductId Please Note: We are using automated SQL script to insert large amount of random data. According to MSDN, a join gives better performance. Else, the subquery must be passed for each result of the main query. As a result, a joins gives the better results instead of a subquery. The above query command gives the products result that we have not sold for once. When I run the query I get 93,801 rows in 3 seconds Select Id, Name, [Description] from tblProducts where Not Exists(Select * from tblProductSales where ProductId = tblProducts.Id) Finally, you get the output as follows,
  • 11. When I run the query by using joins, I get the same result 93,801 rows in 3 seconds. Select tblProducts.Id, Name, [Description] from tblProducts left join tblProductSales on tblProducts.Id = tblProductSales.ProductId where tblProductSales.ProductId IS NULL
  • 12. In conclusion, joins work faster than subqueries, but if we see in reality, it all depends on the plan of execution that is made by SQL Server. Hence, if you want to get the output in a quick manner or you do an extraction of data then go with joins for data mining. Conclusion In this article, you may find both results set with subquery and use of joins instead of using the subquery while working in mining the data with any analytic tool like SQL, Hadoop, and Big Data. A subquery verifies two identities with proper format for the IN clause. But, if we talk about a large number of data in the database server, use joins queries rather than a subquery because joins takes less time to get the output in data mining. Connect With Source Url:- https://www.loginworks.com/blogs/work-subquery- data-mining/