Quantcast
Channel: Planet MySQL
Viewing all articles
Browse latest Browse all 18800

SQL Subqueries Example | Subqueries In SQL Tutorial

$
0
0
SQL Subqueries Example | Subqueries In SQL Tutorial

SQL Subqueries Example | Subqueries In SQL Tutorial is today’s topic. A subquery is known as a nested query within another SQL query and is embedded within the where clause. It is usually added within the where clause of another SQL SELECT statement. It is used to return data, which will further be used in the main query as a condition to restrict the data also to be retrieved. It can be used with the SELECT, INSERT, UPDATE and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.

IMPORTANT RULES:

  1. SQL Subqueries must be enclosed within parentheses.
  2. We can place the Subquery in the number of SQL clauses such as Where, Having, and From Clause.
  3.  A subquery has only one column in the SELECT clause if and only if multiple columns are in the main query for a subquery to compare its selected columns.
  4. The ORDER BY command cannot be used with a subquery, and a GROUP BY command can perform the same operation that can be performed by ORDER BY clause in the particular subquery.
  5.  The List generated by a SELECT statement cannot include any references to values that evaluate to BLOB, ARRAY, CLOB, or NCLOB.
  6. They cannot be immediately enclosed in a set function.
  7. BETWEEN operator can be used within subquery but not with the subquery.

Let’s clear the above statements with proper queries and examples.

#SUBQUERY WITH SELECT STATEMENT:

Subqueries are most frequently used with Select statement.

SYNTAX:

SELECT columns FROM table 
where column OPERATOR 
(SELECT columns from table where);

EXAMPLE:

Let’s consider a table CUSTOMERS:

ID NAME AGE ADDRESS SALARY 
1 Tom 21 Kolkata 500
2 Karan 22 Allahabad 600
3 Hardik 23 Dhanbad 700
4 Komal 24 Mumbai 800

 

QUERY:

Select * from Customers 
where ID 
IN 
(Select ID from Customers 
where salary >=600);

OUTPUT:

ID NAME AGE ADDRESS SALARY
2 Karan 22 Allahabad 600
3 Hardik 23 Dhanbad 700
4 Komal 24 Mumbai 800

 

In the above query, the details of customer have been displayed whose salary was greater than 600 or equal to it. Here IN operator is used which checks whether there is any value returned by the inner query.

#SUBQUERY WITH INSERT STATEMENT:

INSERT statement is used to insert the values in a table which are returned by the subquery.

The selected data in the subquery can also be modified with any character, date, or number functions.

SYNTAX:

INSERT INTO new_table(columns) 
Select columns from old_table 
[where value operator]

QUERY:

INSERT INTO New_Customers 
SELECT * FROM CUSTOMERS 
WHERE ID 
IN 
(SELECT ID FROM CUSTOMERS);

So, here in the above query New_Customers table will be created which will be having the same details of the customer as that of Customers table.

#SUBQUERY WITH UPDATE STATEMENT:

We can also update subquery using update statement as conjunction. We can either update single or multiple columns in a table.

SYNTAX:

UPDATE TABLE 
SET column_name = new_value 
[Where operator [VALUE] 
(SELECT COLUMN_NAME 
FROM TABLE_NAME 
[where]);

QUERY:

UPDATE CUSTOMERS 
SET SALARY = SALARY +500 
WHERE AGE 
IN 
(SELECT AGE FROM New_Customers 
WHERE AGE>=23);

OUTPUT:

ID NAME AGE ADDRESS SALARY
3 Hardik 23 Dhanbad 1200
4 Komal 24 Mumbai 1300

 

So, here in the above query salary of the customers were incremented by 500 whose age was checked as per the given condition and the details of those customers was fetched from New_customers were previously we entered values using INSERT statement. Here IN operator was used to check whether there were any values returned by the inner subquery.

#SUBQUERY WITH DELETE STATEMENT:

The subquery can be used as a conjunction with a DELETE statement to delete some of the values from the database table.

SYNTAX:

DELETE FROM TABLE_NAME 
[ WHERE OPERATOR [ VALUE] 
(SELECT COLUMN_NAME 
FROM TABLE_NAME 
[WHERE]);

QUERY:

DELETE FROM CUSTOMERS 
WHERE AGE 
IN 
(SELECT AGE FROM New_Customers 
WHERE AGE>=23);

OUTPUT:

ID NAME AGE ADDRESS SALARY 
1 Tom 21 Kolkata 500
2 Karan 22 Allahabad 600

 

So, here in the above query, the details of the customers were deleted by CUSTOMERS table whose age was checked as per the given condition. The details of those customers were fetched from New_Customers table, which is a backup of the CUSTOMERS table. Here IN operator was used to check whether there were any values returned by the inner subquery.

Finally, SQL Subqueries Example | Subqueries In SQL Tutorial is over.

The post SQL Subqueries Example | Subqueries In SQL Tutorial appeared first on AppDividend.


Viewing all articles
Browse latest Browse all 18800

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>