Tuesday, July 21, 2020

HOW TO USE SUB-QUERY IN SQL SERVER

A subquery is a query that are used nested inside into the another statements i.e select,insert,update,delete sql statements. A subquery is also known as inner query or nested query.

A subquery can be used in FROM Clause,WHERE Clause and SELECT Clause.

So, here we will use a subquery into the WHERE Clause.

First, we will create a product table and customer table and insert records into it. Please see the created Product table and Customer table.

Product Table:



Customer Table:


Now, we will use a subquery into the Where clause. The syntax is:

       SELECT 
ID,
PRODUCT_NAME,
PRODUCT_CODE,
PRICE 
FROM 
Product 
WHERE 
CUSTOMER_ID = (SELECT 
                                                 ID 
                                         FROM 
                                                 CUSTOMER 
                                         WHERE 
                                                 ID = 2)

Also, you can see the screenshot for further clarifications.


 
                     - - - - - - Cheers, Happy to Help! - - - - - - 

No comments:

Post a Comment

DIFFERENCE BETWEEN CHAR AND VARCHAR IN SQL SERVER

CHAR:  The  CHAR datatype is a fixed length data type in sql server. It is used to store fixed length type of string data or character strin...