
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What are single row and multiple row subqueries?
Single Row Sub Query
A single-row subquery is used when the outer query's results are based on a single, unknown value. Although this query type is formally called "single-row," the name implies that the query returns multiple columns-but only one row of results. However, a single-row subquery can return only one row of results consisting of only one column to the outer query.
In the below SELECT query, inner MySQL returns only one row i.e. the minimum salary for the company. It, in turn, uses this value to compare the salary of all the employees and displays only those, whose salary is equal to minimum salary.
SELECT first_name, salary, department_id FROM employees WHERE salary = (SELECT MIN (salary) FROM employees);
A HAVING clause is used when the group results of a query need to be restricted based on some condition. If a subquery's result must be compared with a group function, you must nest the inner query in the outer query's HAVING clause.
SELECT department_id, MIN (salary) FROM employees GROUP BY department_id HAVING MIN (salary) < (SELECT AVG (salary) FROM employees)
Multiple Row Sub Query
Multiple-row subqueries are nested queries that can return more than one row of results to the parent query. Multiple-row subqueries are used most commonly in WHERE and HAVING clauses. Since it returns multiple rows, it must be handled by set comparison operators (IN, ALL, ANY).While IN operator holds the same meaning as discussed in the earlier chapter, ANY operator compares a specified value to each value returned by the subquery while ALL compares a value to every value returned by a subquery. The below query will show the error because single-row subquery returns multiple rows.
SELECT first_name, department_id FROM employees WHERE department_id = (SELECT department_id FROM employees WHERE LOCATION_ID = 100)
- Related Articles
- Update multiple columns of a single row MySQL?
- Concatenate multiple rows and columns in a single row with MySQL
- How to multiply row values in a data frame having multiple rows with single row data frame in R?
- How to use COUNT(*) to return a single row instead of multiple?
- Single-Row Keyboard in python
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- Selecting a single row in MySQL?
- MySQL LIMIT to select a single row
- How to Copy and Insert Row Multiple Times or Duplicate the Row X Times in Excel?
- How to multiply corresponding row values in a matrix with single row matrix in R?
- What are connected and disconnected Row Sets in JDBC?
- Sum values of a single row in MySQL?
- Java Program to set the height of only a single row in a JTable with multiple rows
- How to multiply corresponding row values in a data.table object with single row data.table object in R?
- MySQL query to return multiple row records with AND & OR operator
