How can we use Sub Queries in SQL?

Right now, will find out about the sub inquiries and how they every now and again utilized with the SELECT articulation?

This is helpful. Think about a circumstance when we need result from a table having a field which should be coordinated to a field a record from an alternate table.

This can be exceptionally muddled as first we will the primary table and afterwards, we will require all the information from the second table at that point repeat through each record and check if the record matches and if the record matches store it in the outcome and do likewise for the second record.

This can be exceptionally simple in the event that we utilize a sub-question. We would simply to choose information from the principal information and utilize a contingent where catchphrase for the field we have to check whenever coordinated with the field in the subsequent table.

For example, consider two table classifications and subcategories and we just need records from the classification which have subcategories, this should be possible by the idea clarified previously.

Like this,

SELECT * FROM category 
WHERE cid in (SELECT * FROM subcategories);

Sham information:

How can we use Sub Queries in SQL?

Syntax 1:

SELECT c, c1 FROM   t1, t2 WHERE  c OPERATOR
(SELECT c, c1
FROM  t1, t2 [WHERE]);

Syntax 2:

UPDATE  t
SET c = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT c FROM t)
[ WHERE) ]

Example 1:

SELECT * FROM EMPLOYEE WHERE ID IN 
(SELECT ID FROM EMPLOYEE WHERE SALARY > 20000) ;
How can we use Sub Queries in SQL?

Example 2:

UPDATE EMPLOYEE SET SALARY = SALARY * 2 WHERE AGE IN 
(SELECT AGE FROM EMPLOYEE WHERE AGE >= 27 );
How can we use Sub Queries in SQL?

Leave a Comment

error: Alert: Content is protected!!