SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

What are single row and multiple row subqueries?

Introduction

Queries inside another SQL query are called subqueries. For various SQL tasks, they assist us in focusing on certain rows. They're utilized in SQL to SELECT, UPDATE, INSERT, and DELETE records. Single-row, multiple-row, multiple-column, correlated, and nested subqueries are among the several kinds of SQL subqueries. According to the user's requirements, every type performs specific roles and employs the outcomes.

Sub Queries

Queries that are nested inside another SQL query are called subqueries. There can be many queries, one after the other, contained inside a subquery. When certain requirements are met during execution, we can pick particular rows. They can also be referred to as the inner query or inner select, while the outer query or outer select is the query that includes it.

Like SELECT, INSERT, UPDATE, DELETE, or any other subquery, subqueries are inside statements.

Subqueries can be found in the PARENT SQL query's WHERE, FROM, or HAVING clauses. They are employed with operators for comparing and reasoning, such as >, \, >=, \>, \=, SOME, ANY, ALL, and IN.

They finish the expression by running ahead of the outer query at run time and passing the outcome. Subqueries determine whether any row is picked by comparing an expression to the output.

A WHERE clause may contain up to 255 subquery levels. When using nested subqueries, the FROM clause is unrestricted. We come across a maximum of five subqueries in the real world. For this reason, 255 cannot be used as a limit.

Syntax:

( SELECT [DISTINCT] subquery_select_statement

  FROM {table_name | view_name}

               {table_name | view_name} ...

  [WHERE condition]

  [GROUP BY column_name [,column_name ] ...]

  [HAVING condition] )

Types of Subqueries

Let's imagine we have given tables in our database as below:

Table name: Art_galleries

create table Art_galleries(id int ,state varchar(20));   

insert into Art_galleries values(1,'Telangana');

insert into Art_galleries values(2,'Kerala');

insert into Art_galleries values(3,'Tamil Nadu');

What are single row and multiple row subqueries?

Table name: Arts

create table Arts(id int ,name varchar(20),gallery_id int,cost int);

insert into Arts(1,'Nature',2,6000);

insert into Arts values(2,'Music',1,7000);

insert into Arts values(3,'House',1,4000);

insert into Arts values(4,'Lady',3,8000);

insert into Arts values(5,'Love',2,6500);
What are single row and multiple row subqueries?

Table name: sellers

create table sellers(id int ,last_name varchar(20),first_name varchar(20),gallery_id int,selling_fee int);

insert into sellers values(1,'Nigh','Kala',2,1900);

insert into sellers values(2,'Latha','Nivda',3,2100);

insert into sellers values(3,'Krithi','Leven',2,1500);

insert into sellers values(4,'Naina','Tenin',1,4500);

insert into sellers values(5,'Shrada','Das',3,4300);
What are single row and multiple row subqueries?

Table name: managers

create table Managers(id int ,gallery_id varchar(20));

insert into managers values(1,2);

insert into managers values(2,3);

insert into managers values(3,1);
What are single row and multiple row subqueries?

Single Row Subquery

Single-row subqueries give a single row to be returned to the parent query. Single-row subqueries are utilized with a FROM condition and a comparison operator, a WHERE phrase, a HAVING phrase, or a SQL SELECT statement. The SELECT statement employs single-row subqueries. Use an example to better understand it.

Query:

select name AS Art,cost,(select AVG(cost) from Arts) AS avg_cost from Arts;

Output:

What are single row and multiple row subqueries?

Note: When the inner query executes independently of the outer query, it yields a significant outcome.

To further filter the output of the outer query, single-row subqueries can be used with the WHERE clause in the SELECT statement. Now, let's look at an example.

Query:

select * from sellers where selling_fee>(select AVG(selling_fee) from sellers);

The SQL mentioned above query displays the seller records whose selling fee exceeds the total fee average. The statement's subquery determines the average selling fee, which is 2860. After that, the parent statement filters out the data

related to the sellers with selling fees greater than regular using the returned value. The outcome appears in the following table:

Output:

What are single row and multiple row subqueries?

Multiple Row Subquery

Multiple row subqueries give multiple rows return to the parent query. An SQL SELECT statement that includes a FROM clause, a WHERE clause, and a HAVING clause, and the logical operators (ALL, IN, NOT IN, and ANY) can take advantage of multiple-row subqueries. Let's examine it with an illustration.

Query:

select AVG(selling_fee) from sellers where id not in (select id from

managers);

The average seller charge for each seller who is not a manager is determined using the SQL above query. The subquery returns the list of all managers' IDs. Subsequently, the outside query refines the dataset to identify the information of sellers who are not managers and computes the average seller fee for such sellers. It gives back the selling fee as a single average amount. The result appears as follows:

Output:

What are single row and multiple row subqueries?