
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















โช A subquery in MySQL is a query, which is nested into another SQL query and embedded with SELECT, INSERT, UPDATE or DELETE statement along with the various operators. โช We can also nest the subquery with another subquery. A subquery is known as the inner query , and the query that contains subquery is known as the outer query. โช The innerquery executed first gives the result to the outer query, and then the main/outer que ry willbe performed. โช MySQL allows us to use subquery anywhere, but it must be closed within parenthesis.
The following are the advantages of using subqueries:
MySQL Subquery with Comparison Operator A comparison operator is an operator used to compare values and returns the result, either true or false. The following comparison operators are used in MySQL <, >, =, <>, <=>, etc. We can use the subquery before or after the comparison operators that return a single value. The returned value can be the arithmetic expression or a column function. After that, SQL compares the subquery results with the value on the other side of the comparison operator. Find an employee detail whose income is more than 350000 with the help of subquery:
SELECT * FROM employees WHERE emp_id IN ( SELECT emp_id FROM employees WHERE income > 350000);
If the subquery produces more than one value, we need to use the IN or NOT IN operator with the WHERE clause. Suppose we have a table named "Student" and "Student2" that contains the following data: Table: Student Table: Student
Find student detail who does not belong to Los Angeles City SELECT Name , City FROM student WHERE City NOT IN ( SELECT City FROM student2 WHERE City='Los Angeles' ); After execution, we can see that the result contains the student details not belonging to Los Angeles City.
MySQL Correlated Subqueries A correlated subquery in MySQL is a subquery that depends on the outer query. It uses the data from the outer query or contains a reference to a parent query that also appears in the outer query. MySQL evaluates it once from each row in the outer query. SELECT emp_name, city, income FROM employees emp WHERE income > ( SELECT AVG(income) FROM employees WHERE city = emp.city); In the above query, we select an employee name and city whose income is higher than the average income of all employees in each city.
MySQL Subqueries with EXISTS or NOT EXISTS โ The EXISTS operator is a Boolean operator that returns either true or false result. โ It is used with a subquery and checks the existence of data in a subquery. โ If a subquery returns any record at all, this operator returns true. Otherwise, it will return false. โ The NOT EXISTS operator used for negation that gives true value when the subquery does not return any row. Otherwise, it returns false. โ Both EXISTS and NOT EXISTS used with correlated subqueries.
It is a subquery that returns a single row where we can get more than one column values. We can use the following operators for comparing row subqueries =, >, <, >=, <=, <>, !=, <=>. SELECT * FROM customer C WHERE ROW(cust_id, occupation) = ( SELECT order_id, order_date FROM Orders O WHERE C.cust_id = O.cust_id); If given row has cust_id, occupation values equal to the order_id, order_date values of any rows in the first table, the WHERE expression is TRUE, and each query returns those first table rows. Otherwise, the expression is FALSE, and the query produces an empty set